Posts

211: Create a class to represent a mathematical vector with private data members for its components. Define constant objects and constant member functions to perform vector addition, subtraction, and scalar multiplication. Implement getter and setter functions for the vector components

// Create a class to represent a mathematical vector with private data members for // its  components.  Define  constant  objects  and  constant  member  functions  to // perform  vector  addition,  subtraction,  and  scalar  multiplication.  Implement // getter and setter functions for the vector components #include < iostream > using namespace std ; class Vector { private :     float X ;     float Y ;     float Z ; public :     // constructors     Vector () : X ( 0 ), Y ( 0 ), Z ( 0 ) {}     Vector ( float x = 0 , float y = 0 , float z = 0 ) : X ( x ), Y ( y ), Z ( z ) {}     void printVector ( string x ) const { cout << x << ' ( ' << X << " , " << Y << " , " << Z << ' ) ' << endl ; }     // overloaded operators ...

210: Create a class representing a car with private data members for company, model, and year. Implement member functions outside the class to set and get these attributes. Use constructor overloading to allow the creation of car objects with default, partial, or full information

  // Create a class representing a car with private data members for company, model, // and  year.  Implement  member  functions  outside  the  class  to  set  and  get  these // attributes. Use constructor overloading to allow the creation of car objects with // default, partial, or full information #include < iostream > using namespace std ; class Car { private :     const string company , model ;     const unsigned int year ; public :     Car ();     Car ( string , string , int );     void getData (); }; Car :: Car () : company ( "" ), model ( "" ), year ( 0 ) {} Car :: Car ( string x , string y , int z ) : company ( x ), model ( y ), year ( z ) {} void Car :: getData () {     cout << " Company: " << company << endl           << " Model: " << model << endl ...

209: Create a function that takes 2 point objects and computes distance between them

  // Create a function that takes 2 point objects and computes distance between them #include < iostream > #include < cmath > using namespace std ; class Point { private :     int x_coord , y_coord ;     friend float distance ( Point , Point ); public :     Point ( int , int ); }; Point :: Point ( int x , int y ) {     x_coord = x ;     y_coord = y ; } float distance ( Point , Point ); int main () {     Point p1 ( 3 , 4 ), p2 ( 2 , 2 );     cout << " Distance between p1 and p2 is: " << distance ( p1 , p2 ) << endl ;     return 0 ; } float distance ( Point a , Point b ) {     return sqrt ( pow (( b . x_coord - a . x_coord ), 2 ) + pow (( b . y_coord - a . y_coord ), 2 )); }

208: Take a binary value input from the user and check if its valid or not. Then display it before and after ones compliment

  // Take a binary value input from the user and check if its valid or not // Then display it before and after ones compliment #include < iostream > using namespace std ; class Binary { private :     string value ; public :     void read ();     void check ();     void display ();     void ones_compliment (); }; void Binary :: read () {     cout << " Enter the Binary value: " ;     cin >> value ; } void Binary :: check () {     for ( int i = 0 ; i < value . size (); i ++)     {         if ( value [ i ] != ' 0 ' && value [ i ] != ' 1 ' )         {             cout << " Invalid Input \n " ;             exit ( 0 );         }     }     cout << " Your input seems to be valid \n " ; } void ...

207: Classes and OOP

Image
Table of contents: Important points Class Syntax Define the class function outside the class Practice program 1 Static data members and static functions Array of objects Passing objects as function arguments Friend function Constructors Constant keyword and const member function Operator overloading Inheritance Pointers 1. Important points: Classes don't take any memory. Class always ends with  }; Memory is only allocated when an object is created. unsigned int count ;  unsigned int means its value will always be non-negative. If 10 objects are made of a class, memory will be assigned to all 10 objects' variables and only one class' behaviors (functions) and static variables. The rest of the objects will share those behaviors and static variables. By default everything in the class is private. Static data: We define the static variable outside the class because it is a class variable and you don't need an object to access the class variable. Also, as it is a class va...