Introduction to Object Oriented Programming Concepts in C++

Updated on     Kisan Patel

OOPS Concepts

Class

  • Class — group of data members & member functions.
  • Like person can be class having data members height and weight and member functions as get_details() and put_details() to manipulate on details.
  • Class is nothing until you create it’s object.

Object

Access to data members & member functions can be done using object only (if they are not static!).

OOPS features are —

  • Encapsulation
  • Data hiding
  • Data reusability
  • Overloading (polymorphism)
  • Overriding

Encapsulation

  • making one group of data members & member functions.
  • Can be done through class
  • Then group of data members & Member functions will be available just by creating object.

Data hiding

  • can be done through access modifiers.
  • Access modifiers are private, public, protected and internal.
  • Private members are member function won’t be available outside class.
  • Public — available all over in program outside class also.
  • Protected — members that are available in class as well as in it’s child class.
  • Protected access modifier comes only when inheritance is in picture.
  • Internal is used with assembly creation.
class employee {
        private:
            char empname[50];
            int empno;
        
        public:
            void getvalue()  
            {
               cout<<"INPUT Employee Name:";
               cin>>empname;
               cout<<"INPUT Employee Number:";
               cin>>empno;  
            }
        void displayvalue()
        {
            cout<<"Employee Name:"<<empname<<endl;
            cout<<"Employee Number:"<<empno<<endl;
        }
};
main()
{
   employee e1;
   e1.getvalue();
   e1.displayvalue();
}

Overloading

  • taking different output of one method or operator based on parameters and return types.
  • Like add() method performs addition and add(int a, int b) performs addition of ‘a’ and ‘b’ passed when calling.
  • Also, + operator performs addition of two numbers as well as concatenation of strings.
class arith
{
   public:
        void calc(int num1)
        {
           cout<<"Square of a given number:"<<num1*num1<<endl;
        }

   void calc(int num1, int num2)
   {
      cout<<Product of two whole numbers:"<<num1*num2<<endl;
   }
};
int main() //begin of main function
{
    arith a;
    a.calc(5);
    a.calc(6, 7);
}
Output
Square of given number: 25
Product of two whole number: 42

Data reusability (inheritance)

  • helps in saving developers time.
  • You can use already created class to create new one.
  • Called inheritance
  • Already existing class is base class and new created is derived class.
  • Base class members can be available in derived class and to access them create object of derived class like from parent to child.
class CPolygon
{
     protected:
         int width, height;
     public:
         void set_values(int a, int b)  {
                width = a; height= b;
         }
};
class CRectangle: public CPolygon
{
     public: int area() {
         return (width * height);     
     }
};
class CTriangle : public CPolygon
{
     public: int area() {
         return (width * height / 2);
     }
};
int main()
{
     CRectangle rect;
     CTriangle trgl;
     rect.set_values(4, 5);
     trgl.set_values(4, 5);
     cout<<trgl.area()<<endl;
     cout<<trgl.area()<<endl;
     return 0;
}

Overriding

  • In C++, overriding is a concept used in inheritance which involves a base class implementation of a method.
  • Then in a subclass, you would make another implemantation of the method. This is overriding. Here is a simple example.
class Base
{
   public:
       virtual void DoSomething() { x = x + 5; }

   private:
       int x;
};
class Derived : public Base
{
   public:
       virtual void DoSomething() { y = y + 5; Base:DoSomething(); }
   private:
       int y;
}

C++

Leave a Reply