Class - C P P

Theory


  • Data Members
    • Variables in the Class
  • Member Function/Methods
    • Function in the Class
  • Object
    • Instance of a Class/Structure
  • Getter
    • Getters are public functions used to get private data members in the class
    • returnType varF() {return var;} => Getter functions are used to get private data members from a public function
  • Setter
    • Setters are public functions used to set private data members in the class
    • returnType varF(parameter) {statement} => Setters are public functions used to set/declare values of private data members in the class
  • Commands
    • Initialize a Class, By default everything is private, Encapsulation, Statements can include variables and functions
          class varC {
              private: statement;
              public: statement;
              protected: statement;
          };
      
    • varC varO => Create a instance of the Class known as Objects, Scope is local
    • varC varO[] => Create a array of objects
    • varO.var => Access the variables in the class
    • varO.varF() => Call a function inside varC of public
    • returnType varC::varF() {} => Using scope operator to define a varC member function outside varC class

Constructor


  • Gets called when a instance of Class is created
  • Types
    • Default
    • Parameterized
    • Copy
      • Default => Shallow Copy => The object and its copy, point to the same memory address. If you make a change in its copy it gets changed in the main copy as well and vice versa
      • User Defined => Deep Copying => The object and its copy, point to different addresses in the memory. If you make a change in its copy it will not get changed in the main copy and vice versa
  • De-constructor
    • Invoked when the object goes out of scope or is explicitly destroyed by a call to delete
  • Commands
    • varC () {statement;} => Creating a Default Constructor, Without any parameter
    • varC(returnType var): var(value){} => For initializing members of the class and it is valid when using constructors
    • varC (parameter) {statement;} => Creating a Parameterized Constructor, Give default values to use it as a default constructor
    • Creating a Copy Constructor, Copies values of a object into another
          varC (varC &varO) {
              // statement
          }
          // Call the constructor
          varC varO2(varO1)
          varC varO2 = varO1
      
    • ~varC () {statement;} => Creating a De-constructor, Gets called when a object gets destroyed, After exiting the main function

Static


  • The static variables in a class are shared by the objects. There can not be multiple copies of same static variables for different objects. Also because of this reason static variables can not be initialized using constructors
  • Objects
    • Scope of static object is through out the life time of program
  • Functions
    • Static member functions are allowed to access only the static data members or other static member functions
  • Commands
    • Shared by all Objects, Only one copy is created, Needs to declared only once, Can call the Function directly without creating any Object
          static dataType var;
          dataType varC::var = value;
          varC::varF();
      
    • static varC varO => Scope of static object is through out the life time of program
    • static returnType varF() {statement;}

Constant


  • Functions
    • The idea of const functions is not to allow them to modify the object on which they are called. Non-const functions can only be called by non-const objects
  • Objects
    • An object declared as const cannot be modified and hence, can invoke only const member functions as these functions ensure not to modify the object
  • Commands
    • returnType varF() const {return value;} => Inside this you can't modify a data variable, can be called on any type of object
    • const varC varO => An object declared as const cannot be modified

Inheritance


  • Single Inheritance
        class varC2 : accessModifier varC1 {
            // statement
        }
    
  • Multiple Inheritance
        class varC2 : accessModifier varC1, accessModifier varC2 {
            // statement
        }
    

Overloading


  • returnType operator operatorSymbol() {} => Operator overloading Function for unary operator
  • returnType operator operatorSymbol(dataType &var) {} => Operator overloading Function for binary operator
  • friend ostream &operator<<(ostream &output, varC &varO) { output << varO.var1 << varO.var2; return output; } => Output overloading
    • cout << varO; => Call it
  • friend istream &operator>>( istream &input, varC &varO ) { input >> varO.var1 >> varO.var2; return input; } => Input Overloading
    • cin >> varO => Call it
  • returnType varF() {statement;} => Function Overloading
  • returnType varF(dataType &var) {statement;}

Friend


  • Function
    • Friend function can be given a special grant to access private and protected members. A friend function can be
      • A member of another class
      • A global function
  • Class
    • A friend class can access private and protected members of other class in which it is declared as friend
  • Commands
    • friend class varC => Declare a class as friend, Can access non-public members using an object
    • friend returnType varC2::varF(varC1&) => Declare a function of another class as friend which can access even private members though Object
    • Declare a global function as a friend
          friend returnType varF(varC&)
          returnType varF(varC& var) {
              // statement
          }
      

Virtual Function


  • Assigning pointer of class 2, Can be given the object memory of a different class, Calling a function of class 2 even if the memory of object stored stored is from class 1, Calling a function of a class 2, If both class have a same function then function of class 2 gets called by default, To call function of object class, we turn function of pointer class into virtual
        varC2 *ptr = (*ptr).varF()
        ptr -> varF()
        virtual returnType varF(parameter) {
            // statement
        }&varO
    
  • Virtual base class in case of multi-path Inheritance to avoid space wastage and error
        class varC2 : virtual accessModifier varC1 {
            // statement
        }
        class varC2 : accessModifier virtual varC1 {
            // statement
        }
    
  • virtual returnType varF() = 0; => Pure Virtual Function, Object can't be created from an abstract class, Must override in derived class to make it work
Share: