chapter 9. inheritance - basics inheritance is a mechanism that allows you to base a new class upon...

20
Inheritance Chapter 9

Upload: penelope-patterson

Post on 08-Jan-2018

225 views

Category:

Documents


3 download

DESCRIPTION

Cont’d Classes that are derived from others inherit all the accessible members of the base class. E.g. Base class includes a member A and we derive it to another class with another member called B, the derived class will contain both members A and B. In order to derive a class from another, we use a colon (:) in the declaration of the derived class using the following format: class derived_class_name: public base_class_name { /*...*/ };

TRANSCRIPT

Page 1: Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits

InheritanceChapter 9

Page 2: Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits

Inheritance - BasicsInheritance is a mechanism that allows you to

base a new class upon the definition of a pre-existing class

Subclass inherits all properties of its superclass and can define its own unique properties.

Subclass can redefine inherited methods.

Generalization: process of forming a superclass Specialization: forming a subclass

Page 3: Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits

Cont’dClasses that are derived from others inherit

all the accessible members of the base class. E.g. Base class includes a member A and we

derive it to another class with another member called B, the derived class will contain both members A and B.

In order to derive a class from another, we use a colon (:) in the declaration of the derived class using the following format:

class derived_class_name: public base_class_name

{ /*...*/ };

Page 4: Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits
Page 5: Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits

ReusabilityOnce a class has been written, created and

debugged, it can be distributed to other programs to reuse in their own programs called ”reusability”E.g. Library of functions, third party softwares

Inheritance provides important extention to the idea of reusability

Reduces software development time

Page 6: Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits

Exampleclass CPolygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b;} }; class CRectangle: public CPolygon { public: int area () { return (width * height); } }; class CTriangle: public CPolygon { public: int area () { return (width * height / 2); } };

int main () { CRectangle rect; CTriangle trgl; rect.set_values (4,5); trgl.set_values (4,5); cout << rect.area() << endl; cout << trgl.area() << endl; return 0; }

Page 7: Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits

Access SpecifierAccess public protected privatemembers of the same class yes yes yes

members of derived classes

yes yes no

not members yes no no

Page 8: Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits
Page 9: Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits
Page 10: Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits

What is inherited from the base class?A derived class inherits every member of a

base class except:its constructor and its destructor its operator=() membersits friends

The constructors and destructors of the base class are not inherited themselves

Default constructor (i.e., its constructor with no parameters) and its destructor are always called when a new object of a derived class is created or destroyed

Page 11: Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits

Cont’dAn overloaded constructor can be called

when a new derived object is created as following

derived_constructor_name (parameters) : base_constructor_name (parameters) {...}

Page 12: Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits

Exampleclass mother { public: mother () { cout << "mother: no parameters\n"; } mother (int a) { cout << "mother: int parameter\n"; } }; class daughter : public mother { public: daughter (int a) { cout << "daughter: int parameter\n\n"; } }; class son : public mother { public: son (int a) : mother (a) { cout << "son: int parameter\n\n"; } };

int main () { daughter cynthia (0); son daniel(0); return 0; }

OUTPUTmother: no parameters daughter: int parameter

mother: int parameter son: int parameter

Page 13: Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits

Public and Private InheritanceThe keyword public specifies that objects of

the derived class are able to access public member functions of the base class

The Keyword private specifies that objects of the derived class cannot access public member functions of the base class

Page 14: Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits
Page 15: Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits
Page 16: Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits
Page 17: Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits

Multiple InheritanceA class inherits members from more than one

classThis is done by simply separating the

different base classes with commas in the derived class declaration

class CRectangle: public CPolygon, public COutput; class CTriangle: public CPolygon, public COutput;

Page 18: Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits

Exampleclass CPolygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b;} }; class COutput { public: void output (int i); }; void COutput::output (int i) { cout << i << endl; }class CRectangle: public CPolygon, public COutput { public: int area () { return (width * height); } }; class CTriangle: public CPolygon, public COutput { public: int area () { return (width * height / 2); } };

int main () { CRectangle rect; CTriangle trgl; rect.set_values (4,5); trgl.set_values (4,5); rect.output (rect.area()); trgl.output (trgl.area()); return 0; }

OUTPUT

2010

Page 19: Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits

Ambiguity in Multiple InheritanceTwo base classes have functions with the same name,

how do object of derived class can access the correct base class function

Class A{ public void show(){….}};Class B{ public void show(){….}};Class C: public A, public B{};void main(){ C objC;objC.show(); // ambiguous—will not compileobjC.A::show();// resolved by :: scope resolution operatorobjC.B::show();}

Page 20: Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits

Cont’dIf you derive a class from two classes that are each

derived from the same class (Diamond-shape Inheritance)

Class A{ public void func();};Class B:public A{ };Class C: public A{}Class D: public B, public C{ };Void main(){D objD; objD.func(); // ambiguous .. Will not compile}