Inheritance and Its types
In OOP, inheritance means assigning characteristics of one
class object to another class object.
How we do it?
When creating a class, instead of writing
completely new data members and member
functions, the programmer can designate that the
new class should inherit the members of an
existing class.
class derived-class: access-specifier base-class
Base Class
This existing class is called the base class
class derived-class: access-specifier base-class
Derived Class
The new class is the derived class
class derived-class: access-specifier base-class
Access Specifier
class derived-class: access-specifier base-class
access-specifier is one the of public, protected, or private
Class members
Code Area Access
Access public protected private
Same class yes yes yes
Derived
classes
yes yes no
Outside
classes
yes no no
Conversion of Public, Private and protected base members (IMPORTANT)
Public Inheritance: When deriving a class from a public base class,
public members of the base class become public members of the
derived class and protected members of the base class become
protected members of the derived class. A base class's private
members are never accessible directly from a derived class, but can
be accessed through calls to the public and protected members of
the base class.
Protected Inheritance: When deriving from a protected base class,
public and protected members of the base class become protected
members of the derived class.
Private Inheritance: When deriving from a private base class,
public and protected members of the base class become private
members of the derived class.
Example of public inheritance
Multiple Inheritance
class derived-class: access baseA, access baseB....
Friend Function
This function is defined outside the class but is the member of a class
• If the keyword friend is written with a function prototype
inside a class, then
1. It is the member of the same class
2. It is defined outside the class
3. It can be called with creating object of the same class
4. It can take another class object as its input argument
Example:
Friend Function
1. When an ordinary class function is declared inside a class, then
2. The keyword friend is not written with it.
3. It can only be accessed from an object of this same class
4. It is defined inside class or class is specified for it while defining it outside
class
For Example, if class name is BOOK
And its member function is void pages ()
The we will either define it inside class or it can be defined outside class as
void BOOK::pages(){//remaining definition}
class class_name {
...... .... ........
friend return_type function_name(argument/s);
...... .... ........ }
Friend Function declaration
#include <iostream>
using namespace std;
class Distance {
private:
int meter;
public:
Distance(): meter(0){ }
friend int func(Distance); //friend function
};
int func(Distance d) //function definition
{ d.meter=5; //accessing private data from non-member function
return d.meter;
}
int main()
{ Distance D;
cout<<"Distance: "<<func(D); //no need of object with friend
return 0;
}
More Explanation
#include <iostream>
using namespace std;
class B; // forward declaration
class A {
private:
int data;
public:
A(): data(12){ }
friend int func(A , B); //friend function Declaration
};
class B {
private:
int data;
public:
B(): data(1){ }
friend int func(A , B); //friend function Declaration
};
int func(A d1,B d2)
/*Function func() is the friend function of both classes A and B. So,
the private data of both class can be accessed from this function.*/
{
return (d1.data+d2.data);
}
int main()
{ A a;
B b;
cout<<"Data: "<<func(a,b);
return 0;
}
Polymorphism and Virtual Keyword
The word polymorphism means having many forms.
What is Polymorphism in C++
C++ polymorphism means that a call to a member
function will cause a different function to be
executed depending on the type of object that
invokes the function.
Virtual Function and Late binding is used for achieving polymorphs
Achieving Polymorphs
A virtual function is a function in a base class that is declared using the
keyword virtual.
Defining in a base class a virtual function, with another version in a derived class,
signals to the compiler that we don't want static linkage for this function
Virtual Function
When we declare a function but does not assign any definition to it
in the parent class.
Then define it inside a child class
This sort of operation is referred to as dynamic linkage, or late
binding.
Late Binding
If we define same function in two different classes such
that
• Both classes are inherited from a single parent class
• Then, the parent function can call the child function
from any line of the code.
• For this purpose we load each child object to the parent
object and the call the function
• We use virtual keyword in the parent class
• It is different from overloading in the sense that in
overloading we have different function parameters
(input/return type)
More About Polymorphism
Calling in MAIN()
Modification by using Virtual
Virtual means that
this specific thing
does not exist here.
But, if we want to
place something in
this area, this placed
is reserved for that
thing in advance
Such function is
known as virtual
Function

Inheritance, friend function, virtual function, polymorphism

  • 1.
    Inheritance and Itstypes In OOP, inheritance means assigning characteristics of one class object to another class object.
  • 2.
    How we doit? When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. class derived-class: access-specifier base-class
  • 3.
    Base Class This existingclass is called the base class class derived-class: access-specifier base-class
  • 4.
    Derived Class The newclass is the derived class class derived-class: access-specifier base-class
  • 5.
    Access Specifier class derived-class:access-specifier base-class access-specifier is one the of public, protected, or private Class members
  • 6.
    Code Area Access Accesspublic protected private Same class yes yes yes Derived classes yes yes no Outside classes yes no no
  • 7.
    Conversion of Public,Private and protected base members (IMPORTANT) Public Inheritance: When deriving a class from a public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class. A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class. Protected Inheritance: When deriving from a protected base class, public and protected members of the base class become protected members of the derived class. Private Inheritance: When deriving from a private base class, public and protected members of the base class become private members of the derived class.
  • 8.
    Example of publicinheritance
  • 9.
    Multiple Inheritance class derived-class:access baseA, access baseB....
  • 10.
    Friend Function This functionis defined outside the class but is the member of a class
  • 11.
    • If thekeyword friend is written with a function prototype inside a class, then 1. It is the member of the same class 2. It is defined outside the class 3. It can be called with creating object of the same class 4. It can take another class object as its input argument Example: Friend Function 1. When an ordinary class function is declared inside a class, then 2. The keyword friend is not written with it. 3. It can only be accessed from an object of this same class 4. It is defined inside class or class is specified for it while defining it outside class For Example, if class name is BOOK And its member function is void pages () The we will either define it inside class or it can be defined outside class as void BOOK::pages(){//remaining definition} class class_name { ...... .... ........ friend return_type function_name(argument/s); ...... .... ........ }
  • 12.
    Friend Function declaration #include<iostream> using namespace std; class Distance { private: int meter; public: Distance(): meter(0){ } friend int func(Distance); //friend function }; int func(Distance d) //function definition { d.meter=5; //accessing private data from non-member function return d.meter; } int main() { Distance D; cout<<"Distance: "<<func(D); //no need of object with friend return 0; }
  • 13.
    More Explanation #include <iostream> usingnamespace std; class B; // forward declaration class A { private: int data; public: A(): data(12){ } friend int func(A , B); //friend function Declaration }; class B { private: int data; public: B(): data(1){ } friend int func(A , B); //friend function Declaration }; int func(A d1,B d2) /*Function func() is the friend function of both classes A and B. So, the private data of both class can be accessed from this function.*/ { return (d1.data+d2.data); } int main() { A a; B b; cout<<"Data: "<<func(a,b); return 0; }
  • 14.
    Polymorphism and VirtualKeyword The word polymorphism means having many forms.
  • 15.
    What is Polymorphismin C++ C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.
  • 16.
    Virtual Function andLate binding is used for achieving polymorphs Achieving Polymorphs
  • 17.
    A virtual functionis a function in a base class that is declared using the keyword virtual. Defining in a base class a virtual function, with another version in a derived class, signals to the compiler that we don't want static linkage for this function Virtual Function
  • 18.
    When we declarea function but does not assign any definition to it in the parent class. Then define it inside a child class This sort of operation is referred to as dynamic linkage, or late binding. Late Binding
  • 19.
    If we definesame function in two different classes such that • Both classes are inherited from a single parent class • Then, the parent function can call the child function from any line of the code. • For this purpose we load each child object to the parent object and the call the function • We use virtual keyword in the parent class • It is different from overloading in the sense that in overloading we have different function parameters (input/return type) More About Polymorphism
  • 20.
  • 21.
    Modification by usingVirtual Virtual means that this specific thing does not exist here. But, if we want to place something in this area, this placed is reserved for that thing in advance Such function is known as virtual Function