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:

Multiple inheritances in python

Multiple inheritances:

  • Multiple inheritances have a number of base classes and one derived class.
  • It means the numbers of base classes are derived from one derived class.

Syntax:

  1. class a

              //Base class1

    class b

              //Base class2

    class c:public a,public b

              //Derived class

    //Object creation

    Object_name=Derived_class_name();

    object_name.function_name(parameter_list);

Example:

  1. #Multiple inheritance

    class bank:

        def pin_set(self):

            lst=[1111,1234,2222]

            print("\tWelcome to XXX bank")

            return lst

    class getvalue:

        def get_data(self):

            pin=int(input("Enter pin number:"))

            return pin

    class pin_validation(bank,getvalue):

        def verification(self,pin,lst):

           # for i in lst:

            if pin in lst:

                print("Enter your choice:\n1.Cash widrawel\n2.Cash deposite\n3.Mini statement")

            else:

                print("Invalid pin")

    b=pin_validation()

    l=[]

    l=b.pin_set()

    p=b.get_data()

    b.verification(p,l)

Output:

If condition:

Else condition:

Share:

Multilevel inheritance in python

Multilevel inheritance:

  • Multilevel inheritance has derived from the class itself and derives the subclass further.
  • It means one base class derived from intermediate class, that intermediate class derived the derived class.

Syntax:
  1. class a:
             //Base class
    class b: class a
             //Intermediate class

    class c: class b
              //Derived class
    Object creation:
    Object_name=last_class_name() ;
    object_name.function(parameter list);
Example:
  1. #Multi level inheritance

    class bank:

        def pin_set(self):

            lst=[1111,1234,2222]

            print("\tWelcome to XXX bank")

            return lst

    class getvalue(bank):

        def get_data(self):

            pin=int(input("Enter pin number:"))

            return pin

    class pin_validation(getvalue):

        def verification(self,pin,lst):

            if pin in lst:

                print("Enter your choice:\n1.Cash widrawel\n2.Cash deposite\n3.Mini statement")

            else:

                print("Invalid pin")

    b=pin_validation()

    l=[]

    l=b.pin_set()

    p=b.get_data()

    b.verification(p,l)

Output:

If condition:

Else condition:

Share:

Inheritance in python

Inheritance:

  • It is the process of joining two or more classes and we can access the values from both classes.

Types of Inheritance:

1) Single inheritance

2) Multilevel inheritance

3) Multiple inheritances

Benefits of inheritance:

  • Software re-usability
  • Information hiding
  • Code sharing

Syntax:

  1. class class_name1:

        #function definition

    class class_name2(class_name1):

        #function definition

    object_name=class_name(parameter_list)#Object creation

    object_name.function_name(parameter_list)#function calling

Example:

  1. #inheritance with constructor

    class employee:

        def __init__(self,id,name,depart,sal):

            self.id=id

            self.name=name

            self.depart=depart

            self.sal=sal

        def display(self):

            print("\tEmployee details")

            print("ID :",self.id)

            print("Name:",self.name)

            print("Department :",self.depart)

            print("Basic Salary : ",self.sal)

    class salary_cal(employee):

        def sal_cal(self)://access sal from base class

            if self.sal>=15000:

                self.sal+=5000

                print("Increased salary:5000")

                print("Net salary:",self.sal)

            elif self.sal>=1000 and self.sal<15000:

                self.sal+=3000

                print("Increased salary:3000")

                print("Net salary:",self.sal)

            else:

                self.sal+=1000

                print("Increased salary:1000")

                print("Net salary:",self.sal)

    id=input("Enter employee id:")

    name=input("Enter employee name:")

    depart=input("Enter employee department:")

    sal=int(input("Enter basic salary:"))

    #object creation

    emp=salary_cal(id,name,depart,sal)

    #emp=employee(id,name,depart,sal)

    emp.display()

    emp.sal_cal()

Output:

Share:

Single inheritance in python

 1) Single inheritance:

    • If one base class and one derived class in a program called  “Single inheritance”.
      Syntax:
      1. class a:
                  //Base class
        class b: public a
                  //Derived class
        Object creation:
        object_name=Derived_class_name();
        object_name.function(parameter list);
      Example:
      1. class employee:#base class

            def get_data(self):#All function should have the self keyword for joining other fun

                id=input("Enter employee id:")

                name=input("Enter employee name:")

                depart=input("Enter employee department:")

                sal=int(input("Enter basic salary:"))

                return id,name,depart,sal

            def display(self,id,name,depart,sal):

                print("\tEmployee details")

                print("ID :",id)

                print("Name:",name)

                print("Department :",depart)

                print("Basic Salary : ",sal)

        class salary_cal(employee):#derived class

            def sal_cal(self,sal):

                if sal>=15000:

                    sal+=5000

                    print("Increased salary:5000")

                    print("Net salary:",sal)

                elif sal>=1000 and sal<15000:

                    sal+=3000

                    print("Increased salary:3000")

                    print("Net salary:",sal)

                else:

                    sal+=1000

                    print("Increased salary:1000")

                    print("Net salary:",sal)

         

        #Object creation

        emp=salary_cal()

        empno,ename,deptno,sal=emp.get_data()

        emp.display(empno,ename,deptno,sal)

        emp.sal_cal(sal)

      Output:

    Share:

    Object and constructor in python

     Object:

    • An object is used to access the members of the class.
    • It simply said as the short name of the class.

    Constructor:

    • The constructor is used to initialize the variables and sharing the values to functions.
    • Further, it is mainly for object calls.
    • __init__ (double underscore) is the standard function name for initializing the values.
    • Self is not a keyword. It is just variable for connecting function value to constructor.
    • Self is used to pass a value into the function from constructor value.

    Syntax:
    1. import module_name#optional

      class class_name:

          def __init__(self,parameter_list):

              self.parameter_list=variable

          def function_name(self,list_of_patameters):

              #Statements

      object_name=class_name(parameter_list)#Parameter list based on constructor

      object_name.function_name(list_of_patameters)#Function calling through object

    Example program:

    1. class calculator:

          def __init__(self, x, y):

              self.x = x

              self.y = y

          def add(self):

              return self.x+self.y

          def sub(self):

              return self.x-self.y

          def mul(self):

              return self.x*self.y

          def div(self):

              return self.x/self.y

      x,y=input("Enter two values: ").split()

      c=calculator(int(x),int(y))

      print("Add value:",c.add())

      print("Sub value:",c.sub())

      print("Mul value:",c.mul())

      print("Div value:",c.div())

    Output:

    Share:

    Recent Posts

    Service Support

    Need our help to Learn or Post New Concepts Contact me