+ All Categories
Home > Documents > Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public...

Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public...

Date post: 13-Mar-2018
Category:
Upload: dinhdat
View: 220 times
Download: 3 times
Share this document with a friend
26
Inheritance CS 310
Transcript
Page 1: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

Inheritance

CS 310

Page 2: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

Inheritance

• In object-oriented languages, classes can beorganized into a hierarchical structure basedon the concept of inheritance

• Inheritance: property that instances of achild class (subclass) can access both thedata and behavior (methods) associated withthe parent class (superclass)

Page 3: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

Examples

• Car is a subclass of Vehicle• Florist is a subclass of Shopkeeper• EditorWindow is a subclass of Window• Window is a subclass of GraphicalObjectInheritance should be used when two classes

exhibit an “is-a” relationship

Page 4: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

Advantages of Inheritance

• Software Reusability• Code Sharing• Rapid Prototyping• Software Components• Polymorphism & Frameworks

Page 5: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

Forms of Inheritance

• Specialization– A car is a vehicle

• Extension– An Extended Queue is a Queue with extra

features

• Construction (Implementation Inheritance)– A Polynomial is implemented in terms of an

Extended Queue

Page 6: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

Forms of Inheritance cont’d

• Specification– inheritance used for abstract classes– Abstract class Shape has subclasses Rectangle

and Circle

• Other forms: Limitation, Generalization,Variation do not meet “is a” relationship

• Multiple Inheritance

Page 7: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

Inheritance in C++: Terminology

• A client is a program or module that uses aclass

• In addition to public and private members aclass can have protected members

• protected members are hidden from clientsof a class but are available to– its own member functions (and friends)– member functions (and friends) of a derived

class

Page 8: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

Inheritance in C++: Terminology

• Membership categories– public members can be used by anyone– private members can be used only by member

functions and friends of the class– protected members can be used only by

member functions and friends of both the classand any derived class

Page 9: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

Kinds of Inheritance in C++• Public inheritance: public and protected members

of base class remain public and protectedmembers of derived class

• Protected inheritance: public and protectedmembers of base class are protected members ofthe derived class

• Private inheritance: public and protectedmembers of base class are private members ofderived class

• Remember: Private members of base class cannotbe accessed by derived classes

Page 10: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

Inheritance in C++

class derived_class: kind base_class {}

where kind is either public, protected, or private

Page 11: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

Inheritance in C++• Derived classes

class derived_class_name: base_class_name {};

class derived_class_name: public base_class_name { };

keyword public makes methods of base class availableto clients of new class

default: if keyword public is left out, private inheritance

Page 12: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

When to use a specific kind ofinheritance

• Public: extension, specialization,specification

• Private: construction (implementationinheritance)

Page 13: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

class sphereClass{public:// constructors sphereClass(); sphereClass(double Initial Radius);

// sphere operations void SetRadius (double NewRadius); double Radius () const; double Diameter () const; double Circumference () const; double Area () const; double Volume () const; double DisplayStatistics () const;

private: double TheRadius; // the sphere's radius};

Page 14: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

• We can define a new class ballClass which inheritsall the members of sphereClass except for theconstructors and destructors.

• sphereClass is called the base class andballClass is the derived class.

We can also• add a new data member(name for the ball)• add new member functions to manipulate the nameand radius• revise the DisplayStatistics routine to show theball's name in addition to the sphere's statistics

Page 15: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

const int MAX_STRING = 15;class ballClass: public sphereClass{public:// constructors ballClass(); ballClass(double Initial Radius, const char InitialName[]);

// additional operations void GetName (char CurrentName[]) const; // get name of ball void SetName (char NewName[]) const; // alter name of existing ball void ResetBall (double NewRadius, const char NewName[]); // alters radius and name of existing ball double DisplayStatistics () const; // displays statistics of a ball

private: char TheName[MAX_STRING+1]; // the ball's name};

Page 16: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

• Can add as many new members to a derivedclass as you like• Cannot revise an ancestor's private datamembers and should not reuse their names• But you can redefine other ancestormembers.

Page 17: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

• ballClass has two data members:• TheRadius (inherited) and• TheName

• Since TheRadius of sphereClass is private, itcan only be referenced within ballClass by usingsphereClass's public member functions:SetRadius and Radius• What does the implementation for the newmembers look like?

Page 18: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

ballClass::ballClass () : sphereClass() { SetName(""); } // default constructor

ballClass::ballClass(double Initial Radius, const char InitialName[]) : sphereClass(InitialRadius) { SetName(InitialName);}

void ballClass::GetName (char CurrentName[]) const { strcpy(CurrentName, TheName);} // get name of ball

void ballClass::SetName (char NewName[]) const { strcpy(NewName, TheName);} // alter name of existing

// ball

void ballClass::ResetBall (double NewRadius, const char NewName[]) { SetRadius(NewRadius); SetName(NewName); } // alters radius and name of

// existing ball

Page 19: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

double ballClass::DisplayStatistics () const { cout << "Statistics for a " << TheName << ":"; sphereClass::DisplayStatistics(); } // displays statistics of a ball

Page 20: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

• The constructors (destructor) for ballClass invoke thecorresponding constructors (destructor) of sphereClass

• Constructor initializer list used to call the base classconstructorderived_class_name::derived_class_name(arglist): base_class_name(arglist2) { }

• Can use the member functions that BallClass inherits fromsphereClass; e.g. see ResetBall

• Objects of a derived class can invoke the public members ofthe base class:

• Example: ballClass Ball(5.0, "Volleyball");

• This means Ball.Diameter() returns Ball's diameter(10.0) using the member function Diameter that is inheritedfrom sphereClass

Page 21: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

• If Sphere is an instance of sphereClass and Ball is an instance ofballClass, then

• Sphere.DisplayStatistics will invoke Displaystatistics fromsphereClass

• Ball.DisplayStatistics will invoke Displaystatistics fromballClass

The compiler will do static binding of these functions, i.e.determine which is which at compilation time.

Page 22: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

Implementation Inheritance

• Used when one class can be implemented interms of an existing class

• Example: polynomial class can beimplemented in terms of an extended queue

• However, a polynomial is not a queue!

Page 23: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

class Polynomial: private Extended_queue {

// Use private inheritance.

public: void read();

void print() const;

void equals_sum(Polynomial p, Polynomial q);

void equals_product(Polynomial p, Polynomial q);

double evaluate(int value) const;

int degree() const;

private: void mult_term(Polynomial p, Term t);

};

Page 24: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

Abstract classes

class Shape {public:

virtual void rotate(int) = 0;virtual void draw() = 0;virtual double Area() = 0;

};Shape s; //error

Page 25: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

Abstract classes

• Can only be used as an interface and basefor other classesclass Circle: public Shape {public: void rotate(int) { };

void draw() ; double Area() { return (PI*radius*radius); }

private radius;}

Page 26: Inheritance - cs.gmu.edusetia/cs310/slides/lec2.pdf · Kinds of Inheritance in C++ • Public inheritance: public and protected members of base class remain public and protected members

Abstract classes

• Important use is to provide an interfacewithout exposing any implementationdetails

• Used in implementing frameworks forspecific application classes


Recommended