Object Oriented Programming in C++.
Published: 2022-04-02 03:49:57

- Object Oriented Programing is a set, rules or ideas or a standard in programming which is used to solve the different real world problems.

## CLASS -

- Classes are the building blocks of 0bject Oriented programming.
- It is a user defined datatype.
- Class should end with a semicolon else you get compile time error.
- It is just like a blueprint by using which we can create different instances of class also known as the objects.
- Below figure shows class in C++.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b9j8wdwuztoqgjkrmvyo.png)

**_Access modifiers-_There are three types of access modifiers in C++ public, private and protected.** 

**1-Private-** Whatever is private to your class is not going to be accessible outside the the syntax shown in first figure is same as second because class everything inside the class is private by default so we need not to mention that private keyword. class everything inside the class is private by default.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r9l4d5ss7lioujo9hup0.png)
![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vltjru1amznxbzhevl3d.png)

**2-Public-** Whatever is public to your class will be accessible outside the class too.  

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nyb89j2iu8v8x3a3e31e.png)

**3-Protected-** It is similar to private in the sense that it cant be accessed outside the class unless with the help of a friend class, the difference is it can be accessed by any derived class or sub class. 

## OBJECT -

- An instance of a class . When a class is created no memory is allocated but when it is instantiated then memory is allocated.

- Given fig shows an object of employee class with some values for the properties of the class (line 12 – 15).

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9zyp1a5r9fyv4bhmp7hd.png)

## CLASS METHODS -

- Class methods are the functions belongs to the classes.
- It is used to add behavior to the objects.
- There are two ways to define them -:

**Inside class –** In the given figure at line22 we are invoking that method “Introduce Yourself.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9zt4iqdxbi3bkh8ff7uk.png)

**Outside class –** In order to define a method outside the class you need to declare it inside the class then define it outside the class this is done by specifying the name of the class followed by the scope resolution “ : :” operator followed by the name of the method as mentioned in the given figure. 
 
![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bhftk9nmkymo7u2plxxl.png)

## CONSTRUCTORS -

- Constructor is a special member function/method of a class which initializes objects of the class.
- In C++ it is automatically called when object is created.
- Constructor has same name as the class itself.
- Constructor don’t have return type.
- If we don’t specify a constructor, C++ compiler generates a default constructor for us.
- Three types-: **Default, Parameterized, Copy constructer**.
- Figure given below is an example of Parameterized constructor.
- In line 23 – 26 is our objects using constructor.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/faged4dtvuorx0nv7x9y.png)

- The figure given below shows the example of copy constructor is a member method/function that initializes an object using another object of the same class.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ffyqsurp3vnh43dsvf4v.png)
 
 

## ENCAPSULATION -

- In normal terms Encapsulation is defined as wrapping up of data and information under a single unit. In Object Oriented Programming, Encapsulation is defined as binding together the data and the functions that manipulates them here in the given figure we can see that now our properties / data are private (Name, Company and Age) and they can be only accessed and modified by using different methods (Getter and Setter) and figure 2 shows that how we can access and modify our data using getter and setter.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0xtjr7sl7xb3go02ugjy.png)
![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sq3hn97wda3qtxm1w4fy.png)

- Since the properties cant be changed directly the only way is using getters and setters so we can add some validation or some check in our function so that no one can exploit our properties or data this is why encapsulation is done as shown in the figure here age will be only modified if its value meets the specified condition.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8t623ca27ved2uh85fny.png)

## ABSTRACTION-

- Abstraction means displaying only essential information and hiding the details. Data abstraction refers to providing only essential information about the data to the outside world, hiding the background details or implementation consider the pow() method present in math.h header file. Whenever we need to calculate power of a number, we simply call the function pow() present in the math.h header file and pass the numbers as arguments without knowing the underlying algorithm according to which the function is actually calculating power of numbers.

- In the given figures we just initialized our abstract class from line 4-6 we associated that class to our employee class line 8 now employee class can define the behaviour of that abstract class and objects can use it without knowing that what is going behind the scenes.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/habfk25gqexlq475fkm8.png)

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8yf10ftbromy9rnxiqce.png)

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/viri3ekkuhv23hijef4x.png)

## Difference between Encapsulation and Abstraction -

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93dnmz8ktcqqmf1vdgdk.png)
 

## INHERITANCE -

- The capability of a class to derive properties and characteristics from another class is called Inheritance.

- **Sub Class:** The class that inherits properties from another class is called Sub class or Derived Class.

- **Super Class:** The class whose properties are inherited by sub class is called Base Class or Super class.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0lc6q36557nvb0p5mdnd.png)

- In this figure we created a new class “Developer” which is inheriting from the class employee.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7q6ep1fr1pp2uj3f2y9c.png)

- Now when we tried creating the object of class “Developer” then we got an error because we don’t have any constructor for the class “Developer” and default constructor also can’t work here because we inherited from the class employee where we used constructor so some property of developer class has constructor.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dwyn7yem9jw0blxwgddc.png)

- Now we have defined constructor for the Developer class too and passed all the parameters of his own as well as of the parent class.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s2hxy984a44sti4lypcs.png)
 
- Now in this figure we passed the three parameters which belongs to the base class to the base class constructor and Developer constructor just constructed the fourth attribute.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vlrv2hwdnr54otqxewvi.png)

- Now we constructed the object of developer class and passed all the parameters.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5mpw89i5z1fu27afgg2r.png)
  
- In this figure we created a method of class developer here we are using getter function of our employee class in order to access the property of our base class because we can’t access it directly it is Private.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n4ejjb30g7sc46f0t84k.png)

- It throws an error because name attribute of base class is private.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bn0qhkk81c8zc4b575jd.png)

- Now we can access it because it is protected and protected can be accessed by the child class.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yrikst0meagh55lgdrtz.png)

- From the derived class we wont be able to access the abstract class / other extra properties of our base class because it is private by default so in order to make it accessible we need to make our inheritance public as done in line 53 of the above figure.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oq1kxpzoci5hzg1di0w7.png)

## POLYMORPHISM -

- The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form.
- Most common use of polymorphism is when a parent class reference is used to refer to a child class object.
- A real-life example of polymorphism, a person at the same time can have different characteristics. Like a man at the same time is a father, a husband, an employee. so the same person posses different behaviour in different situations. This is called polymorphism.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6qvdzasekdstu2ig5war.png)

- We added a new function work to our employee class.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6sw2a06k1a1dm2thtjpa.png)

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/olmfcpacjyi2h8myd1sr.png) 
  
- Calling the method of base class employee on the objects of different child classes “Developer and Teacher” second figure shows the output we get.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b0i71x20faf41lvqf3mz.png)

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k6gw9w3y126gykdneb7h.png)

- Here added the same method in both of the derived class but with different task in different child classes as compared to parent class.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nbwl2oi3wbh7n6o2ah0o.png)

- After doing the changes shown above we got this output.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lpz7lpnpzjt642f5i0ph.png)

- In the above figure we made some changes instead of directly accessing the work we made the base class to point on the objects of derived class using reference and used pointers to access the method of base class.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ve38wf1nvspyazyiyx88.png)
  
- By doing above changes you can see that our result changed because the pointers are pointing on the method of base class so when we execute our code that method will be called.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iss1vt4bb1deqd081ya2.png)

- In order to make them work perfectly we need to add “virtual” before the return type and the name of our method this virtual will tell the compiler (when it is called) that can you check that is there any other implementation of this method in our derived class if yes implement that else use this implementation as a default value.