Encapsulation in C++

Encapsulation:

  • Data encapsulation is used to hide the implementation details from users.
  • If a data member is private it means it can only be accessed within the same class. 
  • No outside class can access private data member (variable) of other class.

Syntax:

  1. class class_name

    {

    private data_members;

    public: methods()

    {

    }

    };

Example program:
  1. #include<iostream.h>

    #include<conio.h>

    class Account {

        private:

         int account_number;

         int account_balance;

        public:

         void show_Data() {

              account_number=13456;

              account_balance=5000;

              cout<<"\nBefore deposit of account XXXXX"<<account_number<<" number of balance :"<<account_balance;

        }

         void deposit(int a) {

              if (a < 0) {

                  cout<<"\nGiven amount is wrong...";

              }

              else{

                     account_balance = account_balance + a;

                     cout<<"\n\nAfter deposit of current balance :"<<account_balance;

              }

        }

    };

    void main()

        {

              clrscr();

              Account ac;

              ac.show_Data();

              ac.deposit(200);

              getch();

        }

Output:
Share:

Method overloading and overriding in C++

 Overriding:

  • Method overriding is a mechanism in which a subclass inherits the methods of the superclass and sometimes the subclass modifies the implementation of a method defined in the super class.
  • That means if subclass (Derived class) has the same method as declared in the parent class (base class).
  • Method overriding is used for runtime polymorphism.

Rules for Java Method Overriding:

  • The method must have the same name as in the parent class
  • The method must have the same parameter as in the parent class.
  • There must be inherited.
  • A static method cannot be overridden. It can be proved by runtime polymorphism.

Syntax:

  1. class base_class_name

    {

    return_type method_name(parameter_list)

    {

              //Statement

    }

    }

     

    class derived_class_name

    {

    base_class_method(parameter_list)

    {

              //Statement

    }

    }

Example program:

  1. #include<iostream.h>

    #include<conio.h>

    class Shape {

    public:

    double a;

        void area() {

    cout<<"\n\tCalculating area of Rectangle,Circle,Triangle .........";

        }

    };


    class Rectangle:public Shape {

       public:

        void area(float l,float w) {

    cout<<"\n\nEnter length and width of rectangle : ";

    cin>>l>>w;

    a=l*w; //area=length*width;

    cout<<"\nArea of rectangle : "<<a;

        }

    };


    class Circle:public Shape {

    public:

        void area(float r) {

    cout<<"\n\nEnter radius  of circle : ";

    cin>>r;

    a=3.14*r*r; //area=3.14*radius*radius;

    cout<<"\nArea of circle : "<<a;

        }

    };


    void main()

    {

    clrscr();

    Shape s;

    s.area();

    Rectangle r;

    r.area(6,3);

    Circle c;

    c.area(3.2);

    getch();

    }

Output:

Method overloading:

  • Overloading is a mechanism in which we can use many methods having the same function name but can pass the different numbers of parameters or different types of parameters.

Syntax:

  1. class base_class_name

    {

    return_type method_name(parameter_list)

    {

              //Statement

    }

    return_type same_method_name(parameter_list)

    {

              //Statement

    }

    }

Example program:

  1. #include<iostream.h>

    #include<conio.h>

    class Shape {

    public:

    double a;

        void area() {

              cout<<"\n\tCalculating area of Rectangle,Circle,Triangle .........";

        }

    };

     

    class Rectangle: public Shape {

       public:

        void area() {

              float l,w;

              cout<<"\n\nEnter length and width of rectangle : ";

              cin>>l>>w;

              a=l*w; //area=length*width;

              cout<<"\nArea of rectangle : "<<a;

        }

    };

     

    class Circle:public Shape {

    public:

        void area() {

              float r;

              cout<<"\n\nEnter radius  of circle : ";

              cin>>r;

              a=3.14*r*r; //area=3.14*radius*radius;

              cout<<"\nArea of circle : "<<a;

        }

    };

     

    void main()

    {

              clrscr();

              Shape s;

              s.area();

              Rectangle r;

              r.area();

              Circle c;

              c.area();

              getch();

    }

Output:
Share:

Polymorphism in C++

 Polymorphism:

  • Poly means many and prism means form so polymorphism means many forms.
  • It is used to perform a single action in different ways.
  • For example,
  • Action is drawn and it performance maybe draw a line or draw a circle.

Types of polymorphism:

1. Runtime polymorphism

2. Compile time polymorphism

Runtime polymorphism:

  • The runtime polymorphism is also called dynamic polymorphism.
  • It is the best example is method overloading.

Example program:

  1. #include<iostream.h>

    #include<conio.h>

    class Shape {

    public:

    double a;

        void area() {

              cout<<"\n\tCalculating area of Rectangle,Circle,Triangle .........";

        }

    };

     

    class Rectangle: public Shape {

       public:

        void area() {

              float l,w;

              cout<<"\n\nEnter length and width of rectangle : ";

              cin>>l>>w;

              a=l*w; //area=length*width;

              cout<<"\nArea of rectangle : "<<a;

        }

    };

     

    class Circle:public Shape {

    public:

        void area() {

              float r;

              cout<<"\n\nEnter radius  of circle : ";

              cin>>r;

              a=3.14*r*r; //area=3.14*radius*radius;

              cout<<"\nArea of circle : "<<a;

        }

    };

     

    void main()

    {

              clrscr();

              Shape s;

              s.area();

              Rectangle r;

              r.area();

              Circle c;

              c.area();

              getch();

    }

Output:

Compile-time polymorphism:

  • The runtime polymorphism is also called static polymorphism.
  • It is the best example is method overriding.

Example program:

  1. #include<iostream.h>

    #include<conio.h>

    class Shape {

    public:

    double a;

        void area() {

    cout<<"\n\tCalculating area of Rectangle,Circle,Triangle .........";

        }

    };


    class Rectangle:public Shape {

       public:

        void area(float l,float w) {

    //float l,w;

    cout<<"\n\nEnter length and width of rectangle : ";

    cin>>l>>w;

    a=l*w; //area=length*width;

    cout<<"\nArea of rectangle : "<<a;

        }

    };


    class Circle:public Shape {

    public:

        void area(float r) {

    //float r;

    cout<<"\n\nEnter radius  of circle : ";

    cin>>r;

    a=3.14*r*r; //area=3.14*radius*radius;

    cout<<"\nArea of circle : "<<a;

        }

    };


    void main()

    {

    clrscr();

    Shape s;

    s.area();

    Rectangle r;

    r.area(6,3);

    Circle c;

    c.area(3.2);

    getch();

    }

Output:

Note:

  • If you need to detail learning of method overloading and method overriding concepts then please click the below link to helpful.

https://www.whereisstuff.com/2021/02/method-overloading-and-overriding-in-c.html

Share:

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me