400 likes | 738 Views
Inheritance. Inheritance – most important and a useful feature of OOPs supported by C++. Inheritance – process of creating new class from the existing class. New class is called the Derived class / Child class /subclass Existing class is called the Base class / Parent class / Super class.
E N D
Inheritance Inheritance – most important and a useful feature of OOPs supported by C++ www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Inheritance – process of creating new class from the existing class. • New class is called the Derived class / Child class /subclass • Existing class is called the Base class / Parent class / Super class. • Derived Class derives or inherits the features of existing class. It contains features of existing class and some of its own features. • Base class is the original class that remains unchanged and features of exising class is fully or partially inherited. www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Derived class inherits all capabilities of base class and add features and refinements of its own. Base class is unchanged by the process. • Advantages – Inheritance permits code reusability. Once a class is written and debugged, it need not be touched again. • Reusing the existing code saves time , money and increases reliability. www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Inheritance also help in the original conceptualization of the problem and overall design of the program. www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Base class FA Inheritance FB FC Derived class FA FB FC FD www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Syntax for derivation : class <name of derived class> : <access specifier> <name of base class> { /*definition of derived class*/ } www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Suppose class A already exists. Then a class B is derived from class A as follows Class B : public A { /*new features of class B*/ }; Public access specifier is used in the foregoing example www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
A pointer from derived class to the base class diagrammatically shows derivation Diagrammatic depiction of Inheritance A B www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Effects of inheritance • Inheritance affects the size and behavior of the derived class objects in 2 ways • An object of derived class contain all data members of the derived class. It contains data members of the base class also. • Hence an object of derived class will always be larger than object of base class. www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
With respect to an object of the derived class, we can call public member functions of the derived class in any global non-member function. However we can call public member functions of the base class also with exceptions. //insize.cpp www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
An object of derived class will contain the data members of the base class and data members of the derived class. • Hence the size of object of derived class = sum of the sizes of data members of the base class + sum of sizes of the data members of the derived class. www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Inheritance implements an ‘is-a’ relationship. • A derived class is a type of the base class like an aircraft (derived class) is a type of vehicle (base class). • A class may contain an object of another class / pointer to datastructure that contain set of objects of another class. Such a class is called container class www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Eg – An aircraft has 1 engine or an array of engines. • Another eg – manager class & employee class. A manager (i.e. an object of the class manager) is an employee (i.e. object of class employee). It has some features that are not possessed by an employee. www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Eg – it may have a pointer to an array of employees that report to him. • Derived class object is also a base class object shown in the code. www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Class Employee { String name; Double basic; Date doj; /*rest of employee class*/ }; www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Class manager : public employee { employee *list; /*rest of the class manager*/ } www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Inheritance in actual practice • In actual practice, the library programmer defines a certain class and member functions. • A programmer to create his applications, then inherits from this class and adds only special data members and the code to handle these additional data members in the derived class. www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Base & Derived class Objects • An object of base class A A1 will occupy 4 bytes containing only x. Whereas an object of class B B1 will occupy a block of 8 bytes containing both x and y.201 • 101 x 4 A1 y 4 B1 Memory layout of base class & Derived class object x www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Accessing members of base class in derived class • Only public members of base class can be accessed in the functions of the derived class protected members of base class can also be accessed. But private members of base class cannot be accessed. • Suppose in base class B::setY() function we write x=y; Compiler will report an error stating that private members of the base class cannot be accessed. Here we are trying to access x in mf of derived class, but x is a private member of the class www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
But we can access A::setX() & A::getX() functions in mfs of derived class because they are public members of the base class. • Private members of the base class remain private w.r.t. members of the derived class. • Following code show this Void B setY(const int q) { Y=q; setX(y); //x=Y } C++ prevents us from accessing private members of the base Class in mfs of derived class to fully implement data security. www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
We may want to create a derived class that suppliments base class by containing those mfs missing in base class. • Eg – Suppose a function String::addchar() is not present in class String. But here, drawback is in base class & bc should be corrected. Inheritance is not used to remove such lacuna. • It provide data & additional code to work upon additional data in derived class. • Inheritance is used to add facilities to an existing class without reprogramming it or recompiling it. It uses code reusability. • Friendship is not inherited www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Base class and Derived class Pointers • A base class pointer can safely point at an object of derived class without typecasting. • However a derived class pointer cannot point at an object of base class. • But a derived class pointer can point at an object of base class only forcibly by type casting with possible runtime errors. www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Exceptions – Compiler will prevent a base class pointer from pointing at an object of derived under certain circumstances Reason – program below. class A { Public: int x; }; Class B : public A { Public: int y; } A derived class & its base class www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
These 2 classes have public member data & no member functions at all. And we will see this with main() void main() { A *APtr; B B1; APtr=&B1; //bc pointer points at dc’s object APtr->x=10; //ok-accessing bc member thru a bc pointer APtr->y=20; //error: y not found in class A } Base class ptr pointing at a derived class object. www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Base class pointer is supposed to point at an object of derived class. Through Aptr is able to access x because there is a member x in class A. • APtr cannot access an area in memory that has not been allocated. • Making a base class pointer point at an object of derived class is a very common C++ programming requirement. • Its unsafe to make a derived class pointer point at objects of the base class without explicit typecasting. www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
void main() { A A1; B *BPtr; BPtr -> &A1; //error: cant convert from B* to A* BPtr -> x=10; //ok : dc ptr accesses bc member BPtr -> y=20; //ok : dc ptr accesses bc member } Listing ADerived class pointer can forcibly made to point at an object of derived class by explicit typecasting www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
void main() { A A1; B *BPtr; //dc ptr BPtr = (*B)&A1; //forcible typecasting to make //dc ptr point at base class object } Forcible typecasting to make dc ptr point at an object of the base class www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Explicit address manipulation like this is dangerous holding for case with classes A & B having x and y data members. class A { int x; public: void setX(const int =0); {/*rest of the function class A*/ } }; www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Class B : public A { int Y; public: void setY(const int=0); /*rest of class B*/ }; www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Mfs in listing A access private data members of their respective classes. void main() { A *APtr; B B1; APtr=&B1; //ok bcptr points at dclass’s object. APtr->setx(10); //ok: accessing bc member thru //bcptr APtr->sety(20); //error: sety() not a mf of class A Bcptr pointing at an object of derived class www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Based upon our knowledge about this pointer , compiler will internally convert the statement B1.setx(10); to setx(&B1,10); The address of B1( a derived class object) is passed as a parameter to the function. But the corresponding formal argument in the A::setx() function is this pointer of type A * const. www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
void setx( A* const this, const int p) { This -> x=p; } This pointer in base class mf points at the derived class invoking object. Obviously this pointer points at B1 i.e. an object of the derived class www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Function Overriding • Member functions of the base class can be overridden in the derived class. • Defining a member function in the derived class such • that its name and signature match those of base class function is known as Function Overriding. • One of them is in base class & other one is in derived class. www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Class A { Public: void show() { cout << “Show() function of class A is called\n”; } }; Class B { Public: void show() { cout << “Show() function of class B is called\n”; } }; //Function Overriding www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Show() function of class B has overridden the show( ) function of class A. If show() is called w.r.t. an object of derived class B, the show() of class B will be called instead of show() of class A void main() { B B1; B1.show(); //B::Show() is called } Calling the overriding function d:\overdn.cpp Whenever a function is called w.r.t. an object of a class, the compiler first searches for the function prototype in the same class. Only if this search fails, the compiler goes up the hierarchy to look up for the function prototype. Overridden function of the base class will be called if it is called with respect to an object of the class. //overbs.cpp //overbs1.cpp www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Function overriding is a form of function overloading & this pointer make this clear. The signatures of overriding function & the overridden functions are only apparently the same. This pointer make this clear. Overridden function can be called from overriding function as follows void B::show() { A::show(); /*rest of B::show() function*/ } Sro is needed to avoid infinite recursion www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Function overriding becomes significant only when the base class function being overridden is virtual. • Base Class Initialization – A derived class object is composed of data members of the derived class as well as those of the base class. • We need to initialize all of these data members while creating an object of derived class. • When an object of derived class is created, the compiler implicitly embeds a call to the base class constructor and then the derived class constructor with respect to the object. www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
Suppose A is the base class and B is its derived class. The statement B B1; Is converted to B B1; B1.A(); Destructors are called in the reverse order. Explicitly calling the constructors & destructors , w.r.t an existing object is prohibited. Unsuccessful initialization of base class members //usi.cpp www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers
www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers