A class may de defined to automatically include the data members and member functions of an already existing class. New data members and member functions.

Slides:



Advertisements
Similar presentations
Chapter 5 Inheritance. Objectives Introduction, effects, and benefits of inheritance Base class and derived class objects Base class and derived class.
Advertisements

Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Copyright 2006 Oxford Consulting, Ltd1 February C++ Inheritance Data Abstraction and Abstract Data Types Abstract Data Type Encapsulated data.
Inheritance In C++  Inheritance is a mechanism for building class types from other class types defining new class types to be a –specialization –augmentation.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
VIRTUAL FUNCTIONS AND DYNAMIC POLYMORPHISM. *Polymorphism refers to the property by which objects belonging to different classes are able to respond to.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 12: Adding Functionality to Your Classes.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Inheritance Inheritance – most important and a useful feature of OOPs supported by C++ | Website for Students | VTU -NOTES -Question Papers.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Chapter 8 More Object Concepts
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
OOP, Virtual Functions and Inheritance
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 10 Inheritance and Polymorphism
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Chapter -6 Polymorphism
Overview of C++ Polymorphism
Classes - Part II (revisited) n Constant objects and member functions n Definition Form of Member Functions n friend functions and friend classes n Constructors.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
7. Inheritance and Polymorphism
Inheritance and Polymorphism
Class A { public : Int x; A()
Object-Oriented Programming
Inheritance and Run time Polymorphism
Polymorphism & Virtual Functions
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Constructors and destructors
| Website for students | VTU NOTES
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
By Muhammad Waris Zargar
Constructors and Destructors
Overview of C++ Polymorphism
Inheriting Multiple Base Classes
Computer Science II for Majors
Presentation transcript:

A class may de defined to automatically include the data members and member functions of an already existing class. New data members and member functions can be included as well. (INHERITANCE) The existing class is referred to as the Base Class/ Parent Class/ Super Class. The new class the is being defined by inheriting from the existing class is referred to as the Derived Class/ Child Class/ Sub Class Syntax: Class : {…… // definition of class …. };

Eg: Class B : Public A {// new features of class B }; Diagrammatic representation of Inheritance: A B

class A { int x; public: void setx(const int=0); int getx() const; }; class B: public A { int y; public: void sety(const int =0); int gety() const; }; void main() { cout<<sizeof(A)<<endl<<sizeof(B); B B1; B1. setx(1); B1.sety(3); cout<<B1.getx(); cout<<endl<<B1.gety(); }

Effects of Inheritance: Inheritance affects the size and behaviour of the derived class in 2 ways: 1. An object of the derived class will contain: Data Members of Derived Class + Data Members of Base Class Thus, An object of the Derived Class is generally larger than an object of the Base Class. Exception???

2. We can call a member function of the Derived Class using an object of the Derived Class…. The same object can be used to call the public members of the Base Class also. Does not happen in some circumstances!!!! ____________________________________________________ Inheritance implements a is-a relationship. The process of Inheritance is also called SPECIALIZATION. Why??? Main benefit of Inheritance???

Container Class: A class that contains an object of another class or a pointer to a data structure that contains a set of objects of another class. Containership implements a has-a relationship.

Base and Derived Class Objects:  An object of the Derived Class is not at all related to another simultaneously existing object of the base class. x x y A1 B

Accessing Members of the Base Class in the Derived Class Only Public members of the Base Class can be accessed in the functions of Derived Class. Eg: if definition is: void B::sety() { x=y; } ERROR!!! Private members of the Base Class cannot be accessed. Eg: ‘A:: setx()’ and ‘A::getx()’ can be accessed in the member functions of the Derived Class. Why???

Private members of the Base Class remain Private with respect to the member functions of the Derived Class. Eg: void B::sety(const int q) { y=q; setx(y) } Thus, Data Security is implemented in C++. Inheritance is used to add facilities to an existing class without reprogramming or recompiling it.

class B; class A { friend class B; int x; }; class B { void fB (A *p) { p->x=0; } }; class C : public B { void fC(A *p) { p->x=0; } }; FRIENDSHIP IS NOT INHERITED!

BASE CLASS AND DERIVED CLASS POINTERS A Base Class Pointer can point to an object of Derived Class. A Derived Class Pointer cannot point to an object of the Base Class. A Base Class Pointer can safely point to an object of a Derived Class without the need foe typecasting. (Exceptions will be considered later) A Derived Class Pointer can be made to point at an object of the Base Class only forcibly by typecasting. (Can result in run-time errors!)

class A {public: int x; }; class B : public A {public : int y; }; void main() { A * Aptr; B B1; Aptr=&B1;// Base class ptr points to derived class object Aptr->x=10;//Access Base class member thru base class ptr Aptr->y=20;//Error! Y not found in class A }

Aptr is of type ‘A *’. It is supposed to point at objects of base class A. Therefore, It cannot access ‘y’. ( There is no member of name ‘y’ in class A). The fact that Aptr points at an object of class B is of no significance. Although ‘Aptr ‘ points at ‘B1’(occupies 4 bytes), it is able to access only the value contained in the first 2 bytes. Thus, Aptr cannot access area in the memory that hasn’t been allocated. A Base class pointer can safely point to an object of the derived class.

x y Aptr B1 201 Base class pointer points to an object of the derived class

void main() { A A1; B * Bptr; Bptr=&A1; // Error!! Cannot convert from B* to A* Bptr->x=10;//ok. Derived class pointer //accesses base class member Bptr->y=20; //ok. Derived class pointer //accesses derived class member }

Bptr is of type ‘B *’. It is supposed to point to objects of derived class B. It can access ‘y’ also. Bptr is pointing to A1(occupies 2 bytes). There is a member of name ‘y’ in class B. Thus, Bptr is able to access memory that has not been allocated. A Derived class Pointer cannot safely point to an object of the base class.

x y Bptr A1 201 Derived class pointer points to an object of the Base class Y would be here in case of derived class objects

A derived class pointer can be made to point at an object of base class by explicit typecasting. void main() { A A1; B * Bptr; // derived class pointer Bptr=(B *)&A1; // Explicit typecasting to make the //derived class pointer point at //base class object } Explicit Address manipulation should be avoided and is dangerous!

Consider the following class definitions: class A { int x; public: void setx(const int =0); // rest of class A }; class B: public A { int y; public: void sety(const int=0); // rest of class B};

void main() { A * Aptr; B B1; Aptr=&B1;// ok Base class ptr points to derived class obj Aptr->setx(10); //ok access base class member thru base //class ptr Aptr->sety(20); //Error! Sety() is not a member of class A }

void main() { A A1; B * Bptr; Bptr=&A1;// Error! Cannot convert A* to B* Bptr->setx(10);//ok derived class pointer accesses base //class member Bptr->sety(20);// ok derived class pointer accesses //derived class member }

 Defining a member function in the derived class with the name and signature matching those of the base class function.  Results in two functions with the same name and signature. ( one in base class + one in derived class)

class A { public: void show() { cout<<“ show function in Class A called”; } }; class B:public A { public: void show() // overriding A::show() { cout<<“ show function in Class B called”; } };

void main() { B B1; B1.show();// B::show() is called A A1; A1.show();// A::show() is called B1.A::show();// calling the overridden function forcibly //with respect to object of the derived class. }

 Whenever a function is called with respect to an object, the compiler first searches for the function prototype in the same class.  Only if the search fails, the compiler goes up the class hierarchy to look for the function prototype.  i.e. The show() function of class A is virtually hidden by the show function of class B (for the first call of show() in prev eg)

 Function Overriding is a form of overloading. Justify! Though the signatures of the overriding function and overridden function are the same, The This pointer brings in the apparent difference. Actual prototype of A::show() becomes void show(A * const); Actual prototype of B::show() becomes void show(B * const);

BASE CLASS INITIALIZATION  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. Suppose A is the base class and B is its derived class: B B1; is converted into B B1; B1.A(); B1.B();  Destructors are called in reverse order.

class A { int x; public: A (const int=0); void setx(const int =0); int getx() const; }; A::A(const int p) { x=p;} void A::setx(const int p) { x=p; } int A::getx()const { return x;} class B:public A { int y; public: B (const int=0); void sety(const int =0); int gety() const; }; B::B(const int q) { y=q;} void B::sety(const int q) { y=q; } int B::gety()const { return y;} void main() { B B1(20); cout<<B1.getx()<<endl<<B1.gety()<< endl;}

B B1(20); gets converted into B B1; // Memory allocated for object B1. A(); //Base class constructor called B1.B(20); //derived class constructor called How should the base class constructor be modified to make proper base class initialization? Class B : public A { public: B(const int=0,const int =0); …….}; B::B(const int p, const int q):A(p) { y=q; }

void main() { B B1(10,20); cout<<B1.getx()<< endl<< B1.gety()<< endl; } B B1 (10,20) gets converted to B B1; B1. A(10); B1. B(20);

 It is not necessary that the first parameter should be passed to the Base class constructor.  Any of the parameter in the list can be passed to the base class constructor.

The PROTECTED access specifier  ‘Protected ‘ members are inaccessible to nonmember functions.  They are accessible to the member functions of their own class and to member functions of the derived classes.

class A { private: int x; protected: int y; public: int z;}; class B: public A { public: void xyz();}; void B::xyz() // member function of derived class { x=1; // Error: Private member of base class y=2; //Ok: Protected member of base class z=3; // Ok: Public member of base class }

void main() { A * Aptr; Aptr->x=10; Aptr->y=20; Aptr->z=30; } // Non member function // Error: Private member // Error: Protected member //Ok: Public member

Deriving by Different Access Specifiers Deriving by the Public Access Specifier - Deriving by the public access specifier retains the access level of the Base class members. Private Members of the Base class: * Members of the derived class cannot access them. * Member functions of the subsequently derived classes cannot access them. * Non-member functions cannot access them.

Deriving by the Public Access Specifier…. Protected Members of the Base class: * Members of the derived class can access them. * Member functions of the subsequently derived classes can also access them. * Non-member functions cannot access them.

Deriving by the Public Access Specifier…. Public Members of the Base class: * Members of the derived class can access them. * Member functions of the subsequently derived classes can access them. * Non-member functions can access them.

class A { private: int x; protected: int y; public : int z; }; class B:public A{//B is public derived class of A public: void f1() { x=1; //ERROR! Private member remains private y=2; //ok. Protected member remains protected z=2; } }; //ok. Public member remains public class c:public B{ public: void f2() { x=1; //ERROR! Private member remains private y=2; //ok. Protected member remains protected z=3;}}; //ok. Public member remains public

void xyz(B *Bptr) // non-member function { Bptr->x=10; //Error! Private member remains private Bptr->y=20; //Error! Protected member remains protected Bptr->z=30; //ok. Public member remains public }

 A base class pointer can point to an object of a derived class that has been derived by using the public access specifier. void xyz() // non member function { B B1; //object of public derived class B1.z=100;// ok. z is public member of class B A *Aptr; //pointer of base class Aptr=&B1; //ok. Can make a base class pointer point at //an object of a public derived class. Aptr->z=100; //ok. Can access inherited public //member of the base class through a //base class pointer. }

Deriving by Different Access Specifiers Deriving by the Protected Access Specifier - Deriving by the protected access specifier reduces the access level of the public Base class members to protected, while the access level of protected and private base class members remain unchanged.

Deriving by the Protected Access Specifier Private Members of the Base class: * Members of the derived class cannot access them. * Member functions of the subsequently derived classes cannot access them. * Non-member functions cannot access them.

Deriving by the Protected Access Specifier…. Protected Members of the Base class: * Members of the derived class can access them. * Member functions of the subsequently derived classes can also access them. * Non-member functions cannot access them.

Deriving by the Protected Access Specifier…. Public Members of the Base class: * Members of the derived class can access them. * Member functions of the subsequently derived classes can access them. * Non-member functions cannot access them.

class A { private: int x; protected: int y; public : int z; }; class B:protected A{//B is a protected derived class of A public: void f1() { x=1; //ERROR! Private member remains private y=2; //ok. Protected member remains protected z=2; } }; //ok. Public member remains public class c:public B{ public: void f2() { x=1; //ERROR! Private member remains private y=2; //ok. Protected member remains protected z=3;}}; //ok. Protected member remains protected

void xyz(B *Bptr) // non-member function { Bptr->x=10; //Error! Private member remains private Bptr->y=20;//Error! Protected member remains protected Bptr->z=30; //Error! Public member becomes protected }

 A base class pointer cannot point to an object of a derived class that has been derived by using the protected access specifier. void xyz() // non member function { B B1; //object of derived class B1.z=100;// ERROR! Cannot access public member of base //class through an object of the protected derived class. A *Aptr; //pointer of base class Aptr=&B1; //ERROR! Cannot make a base class pointer point //at an object of a protected derived class. Aptr->z=100; //ok. Can access inherited public member of the //base class through a base class pointer. }

Deriving by Different Access Specifiers Deriving by the Private Access Specifier - Deriving by the private access specifier reduces the access level of the public and protected Base class members to private, while the access level of private base class members remain unchanged.

Deriving by the Private Access Specifier Private Members of the Base class: * Members of the derived class cannot access them. * Member functions of the subsequently derived classes cannot access them. * Non-member functions cannot access them.

Deriving by the Private Access Specifier…. Protected Members of the Base class: * Members of the derived class cannot (can) access them. * Member functions of the subsequently derived classes cannot access them. * Non-member functions cannot access them.

Deriving by the Private Access Specifier…. Public Members of the Base class: * Members of the derived class cannot (can) access them. * Member functions of the subsequently derived classes cannot access them. * Non-member functions cannot access them.

class A { private: int x; protected: int y; public : int z; }; class B:private A{//B is a private derived class of A public: void f1() { x=1; //ERROR! Private member remains private y=2; //ERROR!Protected member becomes private z=2; } }; //ERROR!Public member becomes private class c:public B{ public: void f2() { x=1; //ERROR! Private member remains private y=2; //ERROR! Private member remains private z=3;}}; // ERROR! Private member remains private

void xyz(B *Bptr) // non-member function { Bptr->x=10; //Error! Private member remains private Bptr->y=20; //Error! Protected member becomes private Bptr->z=30; //Error! Public member becomes private }

 A base class pointer cannot point to an object of a derived class that has been derived by using the private access specifier. void xyz() // non member function { B B1; //object of derived class B1.z=100;// ERROR! Cannot access public member of base //class through an object of the private derived class. A *Aptr; //pointer of base class Aptr=&B1; //ERROR! Cannot make a base class pointer point //at an object of a private derived class. Aptr->z=100; //ok. Can access inherited public //member of the base class through //a base class pointer. }

The following two declarations are equivalent: Class B:private A {…. }; Class B:A {…. };

 Multiple Inheritance  Multi level Inheritance  Hierarchical Inheritance  Hybrid Inheritance

 A class derives from more than one base class. Class C derives from Class A and Class B A B C

The general syntax for multiple inheritance : class :, … { …. };

class A{ int x; public: void setx(const int=0); void getx() const; }; class B{ int y; public: void sety(const int=0); void gety() const;}; class C: public A,public B { int z; public: void setz(const int=0); int getz() const; };

void main() { C c1; cout<<sizeof(c1)<<endl; c1.setx(10); c1.sety(20); c1.setz(30); cout<<c1.getx()<<endl<<c1.gety()<<endl<< c1.getz(); }

 Output:

 An object of a class defined by multiple inheritance contains not only the data members defined in the derived class, but also the data members defined in all the base classes.  Size of an object: sum of sizes of all the data members of all the base classes + sum of the sizes of data members of all the derived classes.  Thus, we can call the member function of the base class using an object of the derived class as well.

Ambiguities in Multiple Inheritance:  Identical members in more than one base class. class A{ public: void show() { cout<<“ show of class A called”; } }; class B{ public: void show() { cout<<“ show of class B called”; } }; class C: public A, public B {}; void main() { C C1; C1.show(); }

Ambiguity can be resolved in 2 ways:  Use the scope resolution operator to specify the class to which the member function belongs.  Override the multiple inherited base class member

Diamond shaped inheritance: B C D A

class A { public: void show(); }; class B: public A {}; class C: public A{}; class D:public B, public C {}; void main() { D D1; D1.show(); } Problems???? Solutions????

 Occurs when a class inherits from a derived class.  It can be extended to any level B C A

class A { public: void FA() { cout<<“FA called”;}}; class B: public A { public: void FB() { cout<<“FB called”;}}; class C: public B { public: void FC() { cout<<“FC called”;}}; Void main() { C C1; C1.FA(); C1.FB(); C1.FC(); }

 A single class serves as a base class for more than one derived class. This gives the best illustration of the virtues of code reusability. The need to duplicate common features in more than one class is eliminated. B C A

class A {public:void FA() { cout<<“FA called”;}}; class B:public A {public:void FB() { cout<<“FB called”;}}; class C:public A {public:void FC() { cout<<“FC called”;}}; void main() { B B1; C C1; B1.FA(); B1. FB(); C1. FA(); C1.FC(); }

 A mixture of all the other kinds of inheritances. B C A D

ORDER OF INVOCATION OF CONSTRUCTORS AND DESTRUCTORS Constructors are invoked in the following order: 1. Virtual base class constructors in the order of inheritance. 2. Non-virtual base class constructors in the order of inheritance. 3. Member objects constructor in the order of declaration. 4. Derived class constructor Destructors are invoked in the reverse order.