Inheritance, friend function, virtual function, polymorphism

21
Inheritance and Its types In OOP, inheritance means assigning characteristics of one class object to another class object.

Transcript of Inheritance, friend function, virtual function, polymorphism

Page 1: Inheritance, friend function, virtual function, polymorphism

Inheritance and Its typesIn OOP, inheritance means assigning characteristics of one class object to another class object.

Page 2: Inheritance, friend function, virtual function, polymorphism

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

Page 3: Inheritance, friend function, virtual function, polymorphism

Base Class

 This existing class is called the base class

class derived-class: access-specifier base-class

Page 4: Inheritance, friend function, virtual function, polymorphism

Derived Class

The new class is the derived class

class derived-class: access-specifier base-class

Page 5: Inheritance, friend function, virtual function, polymorphism

Access Specifier

class derived-class: access-specifier base-class

access-specifier is one the of public, protected, or privateClass members

Page 6: Inheritance, friend function, virtual function, polymorphism

Code Area Access

Access public protected privateSame class

yes yes yes

Derived classes

yes yes no

Outside classes

yes no no

Page 7: Inheritance, friend function, virtual function, polymorphism

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.

Page 8: Inheritance, friend function, virtual function, polymorphism

Example of public inheritance

Page 9: Inheritance, friend function, virtual function, polymorphism

Multiple Inheritance

class derived-class: access baseA, access baseB....

Page 10: Inheritance, friend function, virtual function, polymorphism

Friend FunctionThis function is defined outside the class but is the member of a class

Page 11: Inheritance, friend function, virtual function, polymorphism

• 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 Function1. 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); ...... .... ........ }

Page 12: Inheritance, friend function, virtual function, polymorphism

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

friendreturn 0; }

Page 13: Inheritance, friend function, virtual function, polymorphism

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; }

Page 14: Inheritance, friend function, virtual function, polymorphism

Polymorphism and Virtual KeywordThe word polymorphism means having many forms.

Page 15: Inheritance, friend function, virtual function, polymorphism

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.

Page 16: Inheritance, friend function, virtual function, polymorphism

Virtual Function and Late binding is used for achieving polymorphs

Achieving Polymorphs

Page 17: Inheritance, friend function, virtual function, polymorphism

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

Page 18: Inheritance, friend function, virtual function, polymorphism

When we declare a function but does not assign any definition to it in the parent class.Then define it inside a child classThis sort of operation is referred to as dynamic linkage, or late binding.

Late Binding

Page 19: Inheritance, friend function, virtual function, polymorphism

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

Page 20: Inheritance, friend function, virtual function, polymorphism

Calling in MAIN()

Page 21: Inheritance, friend function, virtual function, polymorphism

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 advanceSuch function is known as virtual Function