DEV Community

Cover image for Object Oriented Programming in C++.
hrb11182
hrb11182

Posted on

Object Oriented Programming in C++.

  • 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

_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
Image

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

Image

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

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

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

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

  • 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

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
Image

  • 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

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

Image

Image

Difference between Encapsulation and Abstraction -

Image

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

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

Image

  • 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

  • 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

  • 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

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

Image

  • 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

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

Image

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

Image

  • 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

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

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

Image

Image

  • 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

Image

  • 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

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

Image

  • 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

  • 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

  • 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.

Top comments (0)