Category: 4. Object Oriented

https://cdn3d.iconscout.com/3d/free/thumb/free-c-sharp-3d-icon-download-in-png-blend-fbx-gltf-file-formats–windows-logo-microsoft-visual-studio-coding-lang-pack-logos-icons-7577997.png?f=webp

  • Interfaces in C++ (Abstract Classes)

    An interface describes the behavior or capabilities of a C++ class without committing to a particular implementation of that class.

    The C++ interfaces are implemented using abstract classes and these abstract classes should not be confused with data abstraction which is a concept of keeping implementation details separate from associated data.

    A class is made abstract by declaring at least one of its functions as pure virtual function. A pure virtual function is specified by placing “= 0” in its declaration as follows −

    class Box {
       public:
    
      // pure virtual function
      virtual double getVolume() = 0;
      
    private:
      double length;      // Length of a box
      double breadth;     // Breadth of a box
      double height;      // Height of a box
    };

    The purpose of an abstract class (often referred to as an ABC) is to provide an appropriate base class from which other classes can inherit. Abstract classes cannot be used to instantiate objects and serves only as an interface. Attempting to instantiate an object of an abstract class causes a compilation error.

    Thus, if a subclass of an ABC needs to be instantiated, it has to implement each of the virtual functions, which means that it supports the interface declared by the ABC. Failure to override a pure virtual function in a derived class, then attempting to instantiate objects of that class, is a compilation error.

    Classes that can be used to instantiate objects are called concrete classes.

    Abstract Class Example

    Consider the following example where parent class provides an interface to the base class to implement a function called getArea() −

    #include <iostream>
     
    using namespace std;
     
    // Base class
    class Shape {
       public:
    
      // pure virtual function providing interface framework.
      virtual int getArea() = 0;
      void setWidth(int w) {
         width = w;
      }
      void setHeight(int h) {
         height = h;
      }
    protected:
      int width;
      int height;
    }; // Derived classes class Rectangle: public Shape { public:
      int getArea() { 
         return (width * height); 
      }
    }; class Triangle: public Shape { public:
      int getArea() { 
         return (width * height)/2; 
      }
    }; int main(void) { Rectangle Rect; Triangle Tri; Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object. cout << "Total Rectangle area: " << Rect.getArea() << endl; Tri.setWidth(5); Tri.setHeight(7); // Print the area of the object. cout << "Total Triangle area: " << Tri.getArea() << endl; return 0; }

    When the above code is compiled and executed, it produces the following result −

    Total Rectangle area: 35
    Total Triangle area: 17
    

    You can see how an abstract class defined an interface in terms of getArea() and two other classes implemented same function but with different algorithm to calculate the area specific to the shape.

    Designing Strategy

    An object-oriented system might use an abstract base class to provide a common and standardized interface appropriate for all the external applications. Then, through inheritance from that abstract base class, derived classes are formed that operate similarly.

    The capabilities (i.e., the public functions) offered by the external applications are provided as pure virtual functions in the abstract base class. The implementations of these pure virtual functions are provided in the derived classes that correspond to the specific types of the application.

    This architecture also allows new applications to be added to a system easily, even after the system has been defined.

  • Data Encapsulation in C++

    All C++ programs are composed of the following two fundamental elements −

    • Program statements (code) − This is the part of a program that performs actions and they are called functions.
    • Program data − The data is the information of the program which gets affected by the program functions.

    Encapsulation is an Object Oriented Programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse. Data encapsulation led to the important OOP concept of data hiding.

    Data encapsulation is a mechanism of bundling the data, and the functions that use them and data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user.

    C++ supports the properties of encapsulation and data hiding through the creation of user-defined types, called classes. We already have studied that a class can contain private, protected and public members. By default, all items defined in a class are private. For example −

    class Box {
       public:
    
      double getVolume(void) {
         return length * breadth * height;
      }
    private:
      double length;      // Length of a box
      double breadth;     // Breadth of a box
      double height;      // Height of a box
    };

    The variables length, breadth, and height are private. This means that they can be accessed only by other members of the Box class, and not by any other part of your program. This is one way encapsulation is achieved.

    To make parts of a class public (i.e., accessible to other parts of your program), you must declare them after the public keyword. All variables or functions defined after the public specifier are accessible by all other functions in your program.

    Making one class a friend of another exposes the implementation details and reduces encapsulation. The ideal is to keep as many of the details of each class hidden from all other classes as possible.

    Data Encapsulation Example

    Any C++ program where you implement a class with public and private members is an example of data encapsulation and data abstraction. Consider the following example −

    #include <iostream>
    using namespace std;
    
    class Adder {
       public:
    
      // constructor
      Adder(int i = 0) {
         total = i;
      }
      
      // interface to outside world
      void addNum(int number) {
         total += number;
      }
      
      // interface to outside world
      int getTotal() {
         return total;
      };
    private:
      // hidden data from outside world
      int total;
    }; int main() { Adder a; a.addNum(10); a.addNum(20); a.addNum(30); cout << "Total " << a.getTotal() <<endl; return 0; }

    When the above code is compiled and executed, it produces the following result −

    Total 60
    

    Above class adds numbers together, and returns the sum. The public members addNum and getTotal are the interfaces to the outside world and a user needs to know them to use the class. The private member total is something that is hidden from the outside world, but is needed for the class to operate properly.

    Designing Strategy

    Most of us have learnt to make class members private by default unless we really need to expose them. That’s just good encapsulation.

    This is applied most frequently to data members, but it applies equally to all members, including virtual functions.

  • Data Abstraction in C++

    Data abstraction refers to providing only essential information to the outside world and hiding their background details, i.e., to represent the needed information in program without presenting the details.

    Data abstraction is a programming (and design) technique that relies on the separation of interface and implementation.

    Let’s take one real life example of a TV, which you can turn on and off, change the channel, adjust the volume, and add external components such as speakers, VCRs, and DVD players, BUT you do not know its internal details, that is, you do not know how it receives signals over the air or through a cable, how it translates them, and finally displays them on the screen.

    Thus, we can say a television clearly separates its internal implementation from its external interface and you can play with its interfaces like the power button, channel changer, and volume control without having any knowledge of its internals.

    In C++, classes provides great level of data abstraction. They provide sufficient public methods to the outside world to play with the functionality of the object and to manipulate object data, i.e., state without actually knowing how class has been implemented internally.

    For example, your program can make a call to the sort() function without knowing what algorithm the function actually uses to sort the given values. In fact, the underlying implementation of the sorting functionality could change between releases of the library, and as long as the interface stays the same, your function call will still work.

    In C++, we use classes to define our own abstract data types (ADT). You can use the cout object of class ostream to stream data to standard output like this −

    #include <iostream>
    using namespace std;
    
    int main() {
       cout << "Hello C++" <<endl;
       return 0;
    }

    Here, you don’t need to understand how cout displays the text on the user’s screen. You need to only know the public interface and the underlying implementation of ‘cout’ is free to change.

    Access Labels Enforce Abstraction

    In C++, we use access labels to define the abstract interface to the class. A class may contain zero or more access labels −

    • Members defined with a public label are accessible to all parts of the program. The data-abstraction view of a type is defined by its public members.
    • Members defined with a private label are not accessible to code that uses the class. The private sections hide the implementation from code that uses the type.

    There are no restrictions on how often an access label may appear. Each access label specifies the access level of the succeeding member definitions. The specified access level remains in effect until the next access label is encountered or the closing right brace of the class body is seen.

    Benefits of Data Abstraction

    Data abstraction provides two important advantages −

    • Class internals are protected from inadvertent user-level errors, which might corrupt the state of the object.
    • The class implementation may evolve over time in response to changing requirements or bug reports without requiring change in user-level code.

    By defining data members only in the private section of the class, the class author is free to make changes in the data. If the implementation changes, only the class code needs to be examined to see what affect the change may have. If data is public, then any function that directly access the data members of the old representation might be broken.

    Data Abstraction Example

    Any C++ program where you implement a class with public and private members is an example of data abstraction. Consider the following example −

    #include <iostream>
    using namespace std;
    
    class Adder {
       public:
    
      // constructor
      Adder(int i = 0) {
         total = i;
      }
      
      // interface to outside world
      void addNum(int number) {
         total += number;
      }
      
      // interface to outside world
      int getTotal() {
         return total;
      };
      
    private:
      // hidden data from outside world
      int total;
    }; int main() { Adder a; a.addNum(10); a.addNum(20); a.addNum(30); cout << "Total " << a.getTotal() <<endl; return 0; }

    When the above code is compiled and executed, it produces the following result −

    Total 60
    

    Above class adds numbers together, and returns the sum. The public members – addNum and getTotal are the interfaces to the outside world and a user needs to know them to use the class. The private member total is something that the user doesn’t need to know about, but is needed for the class to operate properly.

    Designing Strategy

    Abstraction separates code into interface and implementation. So while designing your component, you must keep interface independent of the implementation so that if you change underlying implementation then interface would remain intact.

    In this case whatever programs are using these interfaces, they would not be impacted and would just need a recompilation with the latest implementation.

  • Polymorphism 

    The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance.

    C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.

    Consider the following example where a base class has been derived by other two classes −

    Open Compiler

    #include <iostream> usingnamespace std;classShape{protected:int width, height;public:Shape(int a =0,int b =0){
    
         width = a;
         height = b;}intarea(){
         cout &lt;&lt;"Parent class area :"&lt;&lt; width * height &lt;&lt; endl;return width * height;}};classRectangle:public Shape{public:Rectangle(int a =0,int b =0):Shape(a, b){}intarea(){ 
         cout &lt;&lt;"Rectangle class area :"&lt;&lt; width * height &lt;&lt; endl;return(width * height);}};classTriangle:public Shape{public:Triangle(int a =0,int b =0):Shape(a, b){}intarea(){ 
         cout &lt;&lt;"Triangle class area :"&lt;&lt;(width * height)/2&lt;&lt; endl;return(width * height /2);}};// Main function for the programintmain(){
    Shape *shape; Rectangle rec(10,7); Triangle tri(10,5);// store the address of Rectangle shape =&rec;// call rectangle area. shape->area();// store the address of Triangle shape =&tri;// call triangle area. shape->area();return0;}

    When the above code is compiled and executed, it produces the following result −

    Parent class area :70
    Parent class area :50
    

    The reason for the incorrect output is that the call of the function area() is being set once by the compiler as the version defined in the base class. This is called static resolution of the function call, or static linkage – the function call is fixed before the program is executed. This is also sometimes called early binding because the area() function is set during the compilation of the program.

    But now, let’s make a slight modification in our program and precede the declaration of area() in the Shape class with the keyword virtual so that it looks like this −

    Open Compiler

    #include <iostream>usingnamespace std;classShape{protected:int width, height;public:Shape(int a =0,int b =0){
    
         width = a;
         height = b;}virtualintarea(){
         cout &lt;&lt;"Parent class area :"&lt;&lt; width * height &lt;&lt; endl;return width * height;}};classRectangle:public Shape{public:Rectangle(int a =0,int b =0):Shape(a, b){}intarea(){
         cout &lt;&lt;"Rectangle class area :"&lt;&lt; width * height &lt;&lt; endl;return(width * height);}};classTriangle:public Shape{public:Triangle(int a =0,int b =0):Shape(a, b){}intarea(){
         cout &lt;&lt;"Triangle class area :"&lt;&lt;(width * height)/2&lt;&lt; endl;return(width * height /2);}};// Main function for the programintmain(){
    Shape *shape; Rectangle rec(10,7); Triangle tri(10,5);// store the address of Rectangle shape =&rec;// call rectangle area. shape->area();// store the address of Triangle shape =&tri;// call triangle area. shape->area();return0;}

    After this slight modification, when the previous example code is compiled and executed, it produces the following result −

    Rectangle class area :70
    Triangle class area :25
    

    This time, the compiler looks at the contents of the pointer instead of it’s type. Hence, since addresses of objects of tri and rec classes are stored in *shape the respective area() function is called.

    As you can see, each of the child classes has a separate implementation for the function area(). This is how polymorphism is generally used. You have different classes with a function of the same name, and even the same parameters, but with different implementations.

    Virtual Function

    virtual function is a function in a base class that is declared using the keyword virtual. Defining in a base class a virtual function, with another version in a derived class, signals to the compiler that we don’t want static linkage for this function.

    What we do want is the selection of the function to be called at any given point in the program to be based on the kind of object for which it is called. This sort of operation is referred to as dynamic linkage, or late binding.

    Pure Virtual Functions

    It is possible that you want to include a virtual function in a base class so that it may be redefined in a derived class to suit the objects of that class, but that there is no meaningful definition you could give for the function in the base class.

    We can change the virtual function area() in the base class to the following −

    class Shape {
       protected:
    
      int width, height;
    public:
      Shape(int a = 0, int b = 0) {
         width = a;
         height = b;
      }
      
      // pure virtual function
      virtual int area() = 0;
    };

    The = 0 tells the compiler that the function has no body and above virtual function will be called pure virtual function.

  • Overloading 

    C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively.

    An overloaded declaration is a declaration that is declared with the same name as a previously declared declaration in the same scope, except that both declarations have different arguments and obviously different definition (implementation).

    When you call an overloaded function or operator, the compiler determines the most appropriate definition to use, by comparing the argument types you have used to call the function or operator with the parameter types specified in the definitions. The process of selecting the most appropriate overloaded function or operator is called overload resolution.

    Function Overloading in C++

    You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You cannot overload function declarations that differ only by return type.

    Following is the example where same function print() is being used to print different data types −

    Open Compiler

    #include <iostream>usingnamespace std;classprintData{public:voidprint(int i){
    
        cout &lt;&lt;"Printing int: "&lt;&lt; i &lt;&lt; endl;}voidprint(double  f){
        cout &lt;&lt;"Printing float: "&lt;&lt; f &lt;&lt; endl;}voidprint(char* c){
        cout &lt;&lt;"Printing character: "&lt;&lt; c &lt;&lt; endl;}};intmain(void){
    printData pd;// Call print to print integer pd.print(5);// Call print to print float pd.print(500.263);// Call print to print character pd.print("Hello C++");return0;}

    When the above code is compiled and executed, it produces the following result −

    Printing int: 5
    Printing float: 500.263
    Printing character: Hello C++
    

    Operators Overloading in C++

    You can redefine or overload most of the built-in operators available in C++. Thus, a programmer can use operators with user-defined types as well.

    Overloaded operators are functions with special names: the keyword “operator” followed by the symbol for the operator being defined. Like any other function, an overloaded operator has a return type and a parameter list.

    Box operator+(const Box&);

    declares the addition operator that can be used to add two Box objects and returns final Box object. Most overloaded operators may be defined as ordinary non-member functions or as class member functions. In case we define above function as non-member function of a class then we would have to pass two arguments for each operand as follows −

    Box operator+(const Box&,const Box&);

    Following is the example to show the concept of operator over loading using a member function. Here an object is passed as an argument whose properties will be accessed using this object, the object which will call this operator can be accessed using this operator as explained below −

    Open Compiler

    #include <iostream>usingnamespace std;classBox{public:doublegetVolume(void){return length * breadth * height;}voidsetLength(double len ){
    
         length = len;}voidsetBreadth(double bre ){
         breadth = bre;}voidsetHeight(double hei ){
         height = hei;}// Overload + operator to add two Box objects.
      Box operator+(const Box&amp; b){
         Box box;
         box.length =this-&gt;length + b.length;
         box.breadth =this-&gt;breadth + b.breadth;
         box.height =this-&gt;height + b.height;return box;}private:double length;// Length of a boxdouble breadth;// Breadth of a boxdouble height;// Height of a box};// Main function for the programintmain(){
    Box Box1;// Declare Box1 of type Box Box Box2;// Declare Box2 of type Box Box Box3;// Declare Box3 of type Boxdouble volume =0.0;// Store the volume of a box here// box 1 specification Box1.setLength(6.0); Box1.setBreadth(7.0); Box1.setHeight(5.0);// box 2 specification Box2.setLength(12.0); Box2.setBreadth(13.0); Box2.setHeight(10.0);// volume of box 1 volume = Box1.getVolume(); cout <<"Volume of Box1 : "<< volume <<endl;// volume of box 2 volume = Box2.getVolume(); cout <<"Volume of Box2 : "<< volume <<endl;// Add two object as follows: Box3 = Box1 + Box2;// volume of box 3 volume = Box3.getVolume(); cout <<"Volume of Box3 : "<< volume <<endl;return0;}

    When the above code is compiled and executed, it produces the following result −

    Volume of Box1 : 210
    Volume of Box2 : 1560
    Volume of Box3 : 5400
    
    
    

    Overloadable/Non-overloadableOperators

    Following is the list of operators which can be overloaded −

    +*/%^
    &|~!,=
    <><=>=++
    <<>>==!=&&||
    +=-=/=%=^=&=
    |=*=<<=>>=[]()
    ->->*newnew []deletedelete []

    Following is the list of operators, which can not be overloaded −

    ::.*.?:

    Operator Overloading Examples

    Here are various operator overloading examples to help you in understanding the concept.

    Sr.NoOperators & Example
    1Unary Operators Overloading
    2Binary Operators Overloading
    3Relational Operators Overloading
    4Input/Output Operators Overloading
    5++ and — Operators Overloading
    6Assignment Operators Overloading
    7Function call () Operator Overloading
    8Subscripting [] Operator Overloading
    9Class Member Access Operator -> Overloading
  • Inheritance

    One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.

    When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.

    The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.

    Base and Derived Classes

    A class can be derived from more than one classes, which means it can inherit data and functions from multiple base classes. To define a derived class, we use a class derivation list to specify the base class(es). A class derivation list names one or more base classes and has the form −

    class derived-class: access-specifier base-class
    

    Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default.

    Consider a base class Shape and its derived class Rectangle as follows −

    #include <iostream>
     
    using namespace std;
    
    // Base class
    class Shape {
       public:
    
      void setWidth(int w) {
         width = w;
      }
      void setHeight(int h) {
         height = h;
      }
      
    protected:
      int width;
      int height;
    }; // Derived class class Rectangle: public Shape { public:
      int getArea() { 
         return (width * height); 
      }
    }; int main(void) { Rectangle Rect; Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object. cout << "Total area: " << Rect.getArea() << endl; return 0; }

    When the above code is compiled and executed, it produces the following result −

    Total area: 35
    

    Access Control and Inheritance

    A derived class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.

    We can summarize the different access types according to – who can access them in the following way −

    Accesspublicprotectedprivate
    Same classyesyesyes
    Derived classesyesyesno
    Outside classesyesnono

    A derived class inherits all base class methods with the following exceptions −

    • Constructors, destructors and copy constructors of the base class.
    • Overloaded operators of the base class.
    • The friend functions of the base class.

    Type of Inheritance

    When deriving a class from a base class, the base class may be inherited through public, protected or private inheritance. The type of inheritance is specified by the access-specifier as explained above.

    We hardly use protected or private inheritance, but public inheritance is commonly used. While using different type of inheritance, following rules are applied −

    • Public Inheritance − When deriving a class from a public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class. A base class’s private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.
    • Protected Inheritance − When deriving from a protected base class, public and protected members of the base class become protected members of the derived class.
    • Private Inheritance − When deriving from a private base class, public and protected members of the base class become private members of the derived class.

    Multiple Inheritance

    A C++ class can inherit members from more than one class and here is the extended syntax −

    class derived-class: access baseA, access baseB....
    

    Where access is one of public, protected, or private and would be given for every base class and they will be separated by comma as shown above. Let us try the following example −

    #include <iostream>
     
    using namespace std;
    
    // Base class Shape
    class Shape {
       public:
    
      void setWidth(int w) {
         width = w;
      }
      void setHeight(int h) {
         height = h;
      }
      
    protected:
      int width;
      int height;
    }; // Base class PaintCost class PaintCost { public:
      int getCost(int area) {
         return area * 70;
      }
    }; // Derived class class Rectangle: public Shape, public PaintCost { public:
      int getArea() {
         return (width * height); 
      }
    }; int main(void) { Rectangle Rect; int area; Rect.setWidth(5); Rect.setHeight(7); area = Rect.getArea(); // Print the area of the object. cout << "Total area: " << Rect.getArea() << endl; // Print the total cost of painting cout << "Total paint cost: $" << Rect.getCost(area) << endl; return 0; }

    When the above code is compiled and executed, it produces the following result −

    Total area: 35
    Total paint cost: $2450
    
  • Classes and Objects

    The main purpose of C++ programming is to add object orientation to the C programming language and classes are the central feature of C++ that supports object-oriented programming and are often called user-defined types.

    A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class.

    C++ Class Definitions

    When you define a class, you define a blueprint for a data type. This doesn’t actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.

    A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a pair of curly braces. A class definition must be followed either by a semicolon or a list of declarations. For example, we defined the Box data type using the keyword class as follows −

    class Box {
       public:
    
      double length;   // Length of a box
      double breadth;  // Breadth of a box
      double height;   // Height of a box
    };

    The keyword public determines the access attributes of the members of the class that follows it. A public member can be accessed from outside the class anywhere within the scope of the class object. You can also specify the members of a class as private or protected which we will discuss in a sub-section.

    Define C++ Objects

    A class provides the blueprints for objects, so basically an object is created from a class. We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types. Following statements declare two objects of class Box −

    Box Box1;          // Declare Box1 of type Box
    Box Box2;          // Declare Box2 of type Box
    

    Both of the objects Box1 and Box2 will have their own copy of data members.

    Accessing the Data Members

    The public data members of objects of a class can be accessed using the direct member access operator (.). Let us try the following example to make the things clear −

    #include <iostream>
    
    using namespace std;
    
    class Box {
       public:
    
      double length;   // Length of a box
      double breadth;  // Breadth of a box
      double height;   // Height of a box
    }; int main() { Box Box1; // Declare Box1 of type Box Box Box2; // Declare Box2 of type Box double volume = 0.0; // Store the volume of a box here // box 1 specification Box1.height = 5.0; Box1.length = 6.0; Box1.breadth = 7.0; // box 2 specification Box2.height = 10.0; Box2.length = 12.0; Box2.breadth = 13.0; // volume of box 1 volume = Box1.height * Box1.length * Box1.breadth; cout << "Volume of Box1 : " << volume <<endl; // volume of box 2 volume = Box2.height * Box2.length * Box2.breadth; cout << "Volume of Box2 : " << volume <<endl; return 0; }

    When the above code is compiled and executed, it produces the following result −

    Volume of Box1 : 210
    Volume of Box2 : 1560
    

    It is important to note that private and protected members can not be accessed directly using direct member access operator (.). We will learn how private and protected members can be accessed.

    Classes and Objects in Detail

    So far, you have got very basic idea about C++ Classes and Objects. There are further interesting concepts related to C++ Classes and Objects which we will discuss in various sub-sections listed below −

    Sr.NoConcept & Description
    1Class Member FunctionsA member function of a class is a function that has its definition or its prototype within the class definition like any other variable.
    2Class Access ModifiersA class member can be defined as public, private or protected. By default members would be assumed as private.
    3Constructor & DestructorA class constructor is a special function in a class that is called when a new object of the class is created. A destructor is also a special function which is called when created object is deleted.
    4Copy ConstructorThe copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously.
    5Friend FunctionsA friend function is permitted full access to private and protected members of a class.
    6Inline FunctionsWith an inline function, the compiler tries to expand the code in the body of the function in place of a call to the function.
    7this PointerEvery object has a special pointer this which points to the object itself.
    8Pointer to C++ ClassesA pointer to a class is done exactly the same way a pointer to a structure is. In fact a class is really just a structure with functions in it.
    9Static Members of a ClassBoth data members and function members of a class can be declared as static.
  • Object Oriented

    The prime purpose of C++ programming was to add object orientation to the C programming language, which is in itself one of the most powerful programming languages.

    The core of the pure object-oriented programming is to create an object, in code, that has certain properties and methods. While designing C++ modules, we try to see whole world in the form of objects. For example a car is an object which has certain properties such as color, number of doors, and the like. It also has certain methods such as accelerate, brake, and so on.

    There are a few principle concepts that form the foundation of object-oriented programming −

    Object

    This is the basic unit of object oriented programming. That is both data and function that operate on data are bundled as a unit called as object.

    Class

    When you define a class, you define a blueprint for an object. This doesn’t actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.

    Abstraction

    Data abstraction refers to, providing only essential information to the outside world and hiding their background details, i.e., to represent the needed information in program without presenting the details.

    For example, a database system hides certain details of how data is stored and created and maintained. Similar way, C++ classes provides different methods to the outside world without giving internal detail about those methods and data.

    Encapsulation

    Encapsulation is placing the data and the functions that work on that data in the same place. While working with procedural languages, it is not always clear which functions work on which variables but object-oriented programming provides you framework to place the data and the relevant functions together in the same object.

    Inheritance

    One of the most useful aspects of object-oriented programming is code reusability. As the name suggests Inheritance is the process of forming a new class from an existing class that is from the existing class called as base class, new class is formed called as derived class.

    This is a very important concept of object-oriented programming since this feature helps to reduce the code size.

    Polymorphism

    The ability to use an operator or function in different ways in other words giving different meaning or functions to the operators or functions is called polymorphism. Poly refers to many. That is a single function or an operator functioning in many ways different upon the usage is called polymorphism.

    Overloading

    The concept of overloading is also a branch of polymorphism. When the exiting operator or function is made to operate on new data type, it is said to be overloaded.