Object Oriented Programming Unit-3c Basic Theory

  • Polymorphism
  • Inheritance

  • The word polymorphism means having many forms.
  • In simple words, polymorphism can be defined as the ability of a message to be displayed in more than one form.
  • Real life example of polymorphism, a person at the same time can have different characteristic.
  • Like a man at the same time is
    a teacher, a doctor, an engineer
  • So, the same person has different behaviour in different situations.
  • This is called polymorphism.
  • Polymorphism is considered as one of the important features of Object Oriented Programming (OOP).

In OOP, Polymorphism is mainly divided into two types:
1. Compile time Polymorphism
2. Runtime Polymorphism

  • This type of polymorphism is achieved by
    o Function overloading or
    o Operator overloading.
  • Function Overloading:
    o These functions are said to be overloaded when there are:
     multiple functions
     with same name
     but different parameters

  • Concept of Function Overloading:
    • Functions can be overloaded by
      a. change in number of arguments or/and change in type of arguments.
      b. should have different type, number or sequence of parameters

    • Two functions have different parameter type:
      a. Avg (int num1, int num2)
      b. Avg (double num1, double num2)

    • Two functions have different number of parameters:
      a. Avg (int num1, int num2)
      b. Avg (int num1, int num2, int num3)

    • Two functions have different sequence of parameters:
      a. Avg (int num1, double num2)
      b. Avg (double num1, int num2)

    • But, This is not allowed as the parameter list is same
      a. int Avg (int, int)
      b. double Avg (int, int)

#include
#include
using namespace std;
class Average
{
public:

int Avg(int num1,int num2)
{ return (num1+num2)/2; }

int Avg (int num1,int num2, int num3)
{ return (num1+num2+num3)/3; }

};

int main(void)
{
Average NumObj;
cout<< NumObj.Avg(30, 20)<<endl;
cout<< NumObj.Avg(50, 100, 150);
return 0;
}

Any expression consist of Operator and Operand.
Operators like : + , -, *, ++, --
Operands like :
Numbers (for example: 9,34,789)
Characters (for example: 'A' , '$' , '+')
Strings ( for example: "Wel", "Come" , "MIG700")

Operators can classified as:
Unary Operator = 1 Operator and 1 Operand
Exp: +5, -6, X++, Y--, ++M, --N

Binary Operator = 1 Operator and 2 Operand
Exp: 5 + 4, 6 < 5

Ternay Operaotr = 2 Operators and 3 Operand
Exp: 5>6 ? 5 : 6

  • Inheritance is one of the most important feature of Object Oriented Programming.
  • In C++, inheritance is a process in which one object acquires all the properties and behaviours of its parent object automatically.
  • In such way, the attributes and behaviours can be
    • reused,
    • extended or
    • modified PClass(BClass) NClass(DClass)
  • which are defined in other class.
  • The derived class is the specialized class for the base class.
  • Inheritance concept can be grouped into two categories:
    • derived class (child) - the class that inherits from another class….?
    • base class (parent) - the class being inherited from….?

• Single Inheritance
• Multilevel Inheritance
• Multiple Inheritance
• Hierarchical Inheritance
• Hybrid Inheritance

In this type of inheritance one derived class inherits from only one base class.
It is the most simplest form of Inheritance.

In this type of inheritance a single derived class may inherit from two or more than two base classes.

In this type of inheritance the derived class inherits from a class, which in turn inherits from some other class.
The Super class for one, is sub class for the other.

In this type of inheritance, multiple derived classes inherits from a single base class.

Hybrid Inheritance is combination of Hierarchical and Multilevel Inheritance.

For creating a sub-class which is inherited from the base class, use below syntax.

Syntax:

//Base Class
class Base_Class_Name
{
//body of superclass/BCLass
};

//Derived Class
class DerivedClass_Name : Access_Mode Base_Class_Name
{
//body of subclass/DCLass
};

In C++, there are three access specifiers:
public –members are accessible from outside the class.
private –members cannot be accessed (or viewed) from outside the class.
protected –members cannot be accessed from outside the class, however, they can be accessed in inherited classes.

  • //I n h e r i t a n c e Types and Syntax
  • //Single Inheritance
    //Syntax BC -> DC
    class Base { ... };
    class Derived : public Base { ... };

  • //Multilevel Inheritance
    //Syntax BC -> DC1 -> DC2
    class Base { ... }; //1st Level
    class Derived1 : public Base { ... }; //2nd Level
    class Derived2 : public Derived1 { ... }; //3rd Level

  • //Multiple Inheritance
    //Syntax BC1 and BC2 => DC
    class Base1 { ... }; //1st Level
    class Base2 { ... }; //1st Level
    class Derived : public Base1, public Base2 { ... }; //2nd Level

  • //Hierachical Inheritance
    //Syntax BC => DCL1 and DCR2
    class Base { ... }; //1st Level
    class DerivedL1: public Base { ... }; //2nd Level
    class DerivedR1: public Base { ... }; //2nd Level

  • //Hybrid Inheritance
    = Hierarchical Inheritance + Multiple Inheritance
    //Syntax BC => DLC and DRC => DC
    class Base { ... };//1st Level
    class DerivedL1: public Base { ... }; //2nd Level
    class DerivedR1: public Base { ... }; //2nd Level
    class Derived : public DerivedL1, public DerivedL2
    { ... }; //3rd Level

//A C++ virtual function is a member function in the base class //that you redefine in a derived class. //It is declared using the virtual keyword. //It is used to tell the compiler to perform dynamic linkage or //late binding on the function.

//A pure virtual function (or abstract function) in C++ is a virtual function
//for which we don’t have implementation, we only declare it.
//A pure virtual function is declared by assigning 0 in declaration

A class is the abstract, if it has at least one pure virtual function.
A pure virtual function in C++ can be stated as the function which is only declared in the base class, and it is defined in the derived classes.
In simpler words, it can be stated as the functions which are only declared, and it’s not implemented in the base class.
Those functions are known as virtual functions.

Abstract classes cannot have objects.
To be an abstract class, it must have a presence of at least one virtual class.
Pointers and references to abstract class type can be used.
Constructors of an abstract class can be created

• In Jave, there is a abstract keyword. Similarly, pure virtual functions can be created to make the class the class abstract.

• In C++, there is no need to add an abstract keyword as the base class, which has at least one pure virtual function is understood to be an abstract class.