23
C++ facts From www.geeksforgeeks.org

C++ facts From . What all is inherited from parent class in C++? Following are the things which a derived class inherits from its

Embed Size (px)

Citation preview

Page 1: C++ facts From . What all is inherited from parent class in C++? Following are the things which a derived class inherits from its

C++ facts

From www.geeksforgeeks.org

Page 2: C++ facts From . What all is inherited from parent class in C++? Following are the things which a derived class inherits from its

What all is inherited from parent class in C++?

• Following are the things which a derived class inherits from its parent:– 1) Every data member defined in the parent class

(although such members may not always beaccessible in the derived class!)

– 2) Every ordinary member function of the parent class (although such members may not always beaccessible in the derived class!)

– 3) The same initial data layout as the base class.

Page 3: C++ facts From . What all is inherited from parent class in C++? Following are the things which a derived class inherits from its

Following are the things which a derived class does NOT inherit from its parent :– 1) The base class’s constructors and destructor.

– 2) The base class’s friends

Page 4: C++ facts From . What all is inherited from parent class in C++? Following are the things which a derived class inherits from its

Virtual Functions and Runtime Polymorphism in C++ | Set 1 (Introduction)

Consider the following simple program which is an example of runtime polymorphism.(given in next slide):

Page 5: C++ facts From . What all is inherited from parent class in C++? Following are the things which a derived class inherits from its

#include<iostream>using namespace std; class Base{public: virtual void show() { cout<<" In Base \n"; }};

Page 6: C++ facts From . What all is inherited from parent class in C++? Following are the things which a derived class inherits from its

class Derived: public Base{public: void show() { cout<<"In Derived \n"; }};

Page 7: C++ facts From . What all is inherited from parent class in C++? Following are the things which a derived class inherits from its

int main(void){ Base *bp = new Derived; bp->show(); // RUN-TIME POLYMORPHISM return 0;}

Page 8: C++ facts From . What all is inherited from parent class in C++? Following are the things which a derived class inherits from its

Output:

Page 9: C++ facts From . What all is inherited from parent class in C++? Following are the things which a derived class inherits from its

• Output:

In Derived

Page 10: C++ facts From . What all is inherited from parent class in C++? Following are the things which a derived class inherits from its

• The main thing to note about the above program is, derived class function is called using a base class pointer.

• The idea is, virtual functions are called according to the type of object pointed or referred, not according to the type of pointer or reference.

• In other words, virtual functions are resolved late, at runtime.

Page 11: C++ facts From . What all is inherited from parent class in C++? Following are the things which a derived class inherits from its

What is the use?

• Virtual functions allow us to create a list of base class pointers and call methods of any of the derived classes without even knowing kind of derived class object.

Page 12: C++ facts From . What all is inherited from parent class in C++? Following are the things which a derived class inherits from its

What is the use?

• For example, consider a employee management software for an organization, let the code has a simple base classEmployee , the class contains virtual functions like raiseSalary(), transfer(), promote(),.. etc.

• Different types of employees like Manager, Engineer, ..etc may have their own implementations of the virtual functions present in base class Employee..

Page 13: C++ facts From . What all is inherited from parent class in C++? Following are the things which a derived class inherits from its

What is the use?

• In our complete software, we just need to pass a list of employees everywhere and call appropriate functions without even knowing the type of employee.

• For example, we can easily raise salary of all employees by iterating through list of employees.

Page 14: C++ facts From . What all is inherited from parent class in C++? Following are the things which a derived class inherits from its

What is the use?

• Every type of employee may have its own logic in its class, we don’t need to worry because if raiseSalary() is present for a specific employee type, only that function would be called.

Page 15: C++ facts From . What all is inherited from parent class in C++? Following are the things which a derived class inherits from its

class Employee{public: virtual void raiseSalary() { /* common raise salary code */ } virtual void promote() { /* common promote code */ }};

Page 16: C++ facts From . What all is inherited from parent class in C++? Following are the things which a derived class inherits from its

class Manager: public Employee { virtual void raiseSalary() { /* Manager specific raise salary code, may

contain increment of manager specific incentives*/ } virtual void promote() { /* Manager specific promote */ }};

Page 17: C++ facts From . What all is inherited from parent class in C++? Following are the things which a derived class inherits from its

// Similarly, there may be other types of employees // We need a very simple function to increment salary of all

//employees// Note that emp[] is an array of pointers and actual pointed

//objects can// be any type of employees. This function should ideally be in

a //class // like Organization, we have made it global to keep things

//simple

Page 18: C++ facts From . What all is inherited from parent class in C++? Following are the things which a derived class inherits from its

void globalRaiseSalary(Employee *emp[], int n){ for (int i = 0; i < n; i++) emp[i]->raiseSalary(); // Polymorphic Call: Calls

raiseSalary() // according to the actual object, not // according to the type of

pointer }

Page 19: C++ facts From . What all is inherited from parent class in C++? Following are the things which a derived class inherits from its

• like globalRaiseSalary(), there can be many other operations that can be appropriately done on a list of employees without even knowing the type of actual object.Virtual functions are so useful that later languages like Java keep all methods as virtual by default.

Page 20: C++ facts From . What all is inherited from parent class in C++? Following are the things which a derived class inherits from its

How does compiler do this magic of late resolution?

• Compiler maintains two things to this magic:– vtable: A table of function pointers. It is

maintained per class.– vptr: A pointer to vtable. It is maintained per

object.

Page 21: C++ facts From . What all is inherited from parent class in C++? Following are the things which a derived class inherits from its
Page 22: C++ facts From . What all is inherited from parent class in C++? Following are the things which a derived class inherits from its

• Compiler adds additional code at two places to maintain and use vptr.– 1) Code in every constructor. This code sets vptr of the object

being created. This code sets vptr to point to vtable of the class.– 2) Code with polymorphic function call (e.g. bp->show() in above

code). Wherever a polymorphic call is made, compiler inserts code to first look for vptr using base class pointer or reference (In the above example, since pointed or referred object is of derived type, vptr of derived class is accessed). Once vptr is fetched, vtable of derived class can be accessed. Using vtable, address of derived derived class function show() is accessed and called.

Page 23: C++ facts From . What all is inherited from parent class in C++? Following are the things which a derived class inherits from its

Is this a standard way for implementation of run-time polymorphism in C++?

• The C++ standards do not mandate exactly how runtime polymorphism must be implemented, but compilers generally use minor variations on the same basic model.