inheritance mechanism for deriving new classes from existing classes chapter 13

41
Inheritanc e Mechanism for deriving new classes from existing classes Chapter 13 Chapter 13

Upload: denis-douglas

Post on 19-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Inheritance

Mechanism for deriving new classes from existing classes

Chapter 13Chapter 13

Page 2: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Think of a Bicycle

Tandem Bike

Racing Bike Mountain Bike

a Bicycle

Page 3: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Thinking About Bicycles

A tandem bicycle is a kind of bicycle Bicycle with two seats

A mountain bicycle is a kind of bicycle Bicycle with shocks

A racing bicycle is a kind of bicycle Lightweight aerodynamic construction

Tandem, mountain, and racing bicycles are specialized bicycles

Page 4: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

InheritanceInheritance is the object-oriented programming mechanism for specialization

Ability to define new classes of objects using existing classes as a basis

The new class inherits the attributes and behaviors of the parent classes

New class is aspecialized versionof the parent class

is-a relationships

Bicycle

Mountain Bikes

RacingBikes

Tandem

Bikes

Page 5: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Inheritance

A natural way to reuse code Programming by extension rather than reinvention Object-oriented paradigm is well-suited for this style

ofprogramming

Terminology Base class (superclass) Derived class (subclass)

is-a relationships

Bicycle

Mountain Bikes

RacingBikes

Tandem

Bikes

Page 6: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

class RectangleShape {public:

RectangleShape(SimpleWindow &W, float XCoord, float YCoord,

const color &Color, float Width, float Height);

void Draw();

color GetColor () const;void GetPosition(float &x, float &y) const;void GetSize(float &Width, float &Height) const;float GetWidth () const;float GetHeight() const;

SimpleWindow& GetWindow() const;

void SetColor(const color &Color);void SetPosition(float x, float y);void SetSize(float Width, float Height);

private:SimpleWindow &Window;float XCenter;float YCenter;color Color;float Width;float Height;

};

Before Inheritance

Page 7: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

class CircleShape {public:

CircleShape(SimpleWindow &W, float x, float y, const color &Color, float Diameter);

void Draw();

color GetColor() const; void GetPosition(float &x, float &y) const; float GetSize() const;

SimpleWindow& GetWindow() const;

void SetColor(const color &Color); void SetPosition(float x, float y); void SetSize(float Diameter);

private: SimpleWindow &Window; float XCenter; float YCenter; color Color; float Diameter;};

Before Inheritance

Page 8: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Before Inheritance

RectangleShape

LocationWindowColorWidth Height

GetPosition()GetWindow() SetPosition()GetColor()SetColor()Draw()GetWidth()GetHeight()SetSize()

CircleShape

LocationWindowColorDiameter

GetPosition()GetWindow() SetPosition()GetColor()SetColor()Draw()GetSize()SetSize()

Page 9: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Shapes Hierarchy

WindowObject

LocationWindow

GetPosition()GetWindow() SetPosition()

is-a

TriangleShape

SideLength

Draw()GetSideLength()SetSize()

RectangleShape

Width, Height

Draw()GetWidth()GetHeight()SetSize()

is-a

CircleShape

Diameter

Draw()GetSize()SetSize()

is-a

Shape

Color

GetColor()SetColor()

is-a is-a

Label

Page 10: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Class WindowObject

class WindowObject {

public:WindowObject(SimpleWindow &w, const Position

&p);

Position GetPosition() const;SimpleWindow& GetWindow() const;

void SetPosition(const Position &p);

private:

SimpleWindow &Window;Position Location;

};

Page 11: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Implementing of WindowObjectWindowObject::WindowObject(SimpleWindow &w,

const Position &p) : Window(w), Location(p)

{

// No body needed

}

Initialization list and the members are initialized in class definition order

Position WindowObject::GetPosition() const {

return Location;}

void WindowObject::SetPosition(const Position &p) {

Location = p;}

SimpleWindow& WindowObject::GetWindow() const {

return Window;}

Page 12: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Defining a Derived Class

class DerivedClass : public BaseClass{ public: // public section ...

private: // private section ...};

class DerivedClass : public BaseClass{ public: // public section ...

private: // private section ...};

Derived class nameClass name of

base class

Access specifier(usually public)

Page 13: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Declaring a Derived Class

class Shape : public WindowObject {

public:Shape(SimpleWindow &w, const Position

&p, const color &c = Red);

color GetColor() const;void SetColor(const color &c);

private:color Color;

};

Read this as Shape is a kind of WindowObject

Shape inherits WindowObjectmembers Window, Location,GetPosition(), GetWindow(),and SetPosition()

Page 14: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Implementing A Derived Class Constructor

DClass::DClass( PList ) : BClass( BList ), DMbrList { // Body of derived class constructor ... };

DClass::DClass( PList ) : BClass( BList ), DMbrList { // Body of derived class constructor ... };

Derived class name

Derived class constructor parameter list

Base class name

Base class constructor parameter list(sublist of PList)

Derived class data member initialization list (sublist of PList)

Page 15: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Implementing of Shape Class

Shape::Shape(SimpleWindow &w, const Position &p, const color &c) : WindowObject(w, p), Color(c) { // No body needed}

color Shape::GetColor() const {

return Color;}

void Shape::SetColor(const color &c) {

assert(c >= 0 && c < MaxColors); Color = c;

}

Page 16: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Basic Shapes

Width

Height

RectangleShape

SideLength

TriangleShape CircleShape

Diameter

Page 17: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Class TriangleShape

#include "shape.h"

class TriangleShape : public Shape

{

public:

TriangleShape(SimpleWindow &w, const Position &p,

const color &c = Red, float slen = 1);

float GetSideLength() const;

void SetSize( float slen );

void Draw();

private:

float SideLength;

};

Page 18: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Class RectangleShape

#include ”shape.h”

class RectangleShape : public Shape {

public:RectangleShape(SimpleWindow &w, const Position &Center, const color &c =

Red, float Width = 1, float Width = 2);

float GetWidth () const;float GetHeight() const;

void SetSize(float Width, float Height);

void Draw();

private:float Width;float Height;

};

Page 19: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Class CircleShape

#include "shape.h"class CircleShape : public Shape {

public:CircleShape(SimpleWindow &w, const Position

&Center, const color &c = Red, float Diameter = 1);

float GetSize() const;void SetSize(float Diameter);void Draw();

private:float Diameter;

};

Page 20: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

class CircleShape {public:

CircleShape(SimpleWindow &W, float x, float y, const color &Color, float Diameter);

void Draw();

color GetColor() const; void GetPosition(float &x, float &y) const; float GetSize() const;

SimpleWindow& GetWindow() const;

void SetColor(const color &Color); void SetPosition(float x, float y); void SetSize(float Diameter);

private: SimpleWindow &Window; float XCenter; float YCenter; color Color; float Diameter;};

No Inheritance

Page 21: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

TriangleShape::Draw()

void TriangleShape::Draw()

{

const float Pi = 3.1415;

const Position Center = GetPosition();

const float SLength = GetSideLength();

// Compute c, distance from center of triangle

// to the top vertex, and a, the distance from

// the center to the base of the triangle

float c = SLength / (2.0 * cos(30 * Pi / 180.0));

float a = tan(30 * Pi / 180.0) * .5 * SLength;

Page 22: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

TriangleShape::Draw()

// Create an array containing the positions of

// the vertices of the triangle

vector<Position> TrianglePoints(3);

TrianglePoints[0] = Center + Position(0, -c),

TrianglePoints[1] = Center + Position(-0.5*SLength, a);

TrianglePoints[2] = Center + Position( 0.5*SLength, a);

// Draw the triangle

GetWindow().RenderPolygon(TrianglePoints, 3,

GetColor(), HasBorder());

}

Page 23: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Using Shapes#include "rect.h"#include "ellipse.h"#include "triangle.h"

SimpleWindow Window("TestShapes", 17, 7, Position(4, 4));

int ApiMain(){

Window.Open();

TriangleShape T(Window, Position(3.5, 3.5), Red, 3.0); T.Draw();

RectangleShape R(Window, Position(8.5, 3.5), Yellow, 3.0, 2.0);

R.Draw();

EllipseShape E(Window, Position(13.5, 3.5), Green, 3.0, 2.0); E.Draw();

return 0;}

Page 24: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Fun with Shapes

Page 25: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Inheritance and Member Accessclass SomeClass {

public:void MemberFunction();int MyPublicData;

protected:int MyProtectedData;

private:int MyPrivateData;

};

void SomeClass::MemberFunction() {

MyPublicData = 1; // access allowedMyProtectedData = 2; // access allowedMyPrivateData = 3; // access allowed

}

void NonMemberFunction() {

SomeClass C;C.MyPublicData = 1; // access allowedC.MyProtectedData = 2; // illegalC.MyPrivateData = 3; // illegal

}

Page 26: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Inheritance and Member Accessclass BaseClass {

public: int MyPublicData; protected: int MyProtectedData; private: int MyPrivateData;

};

class DerivedClass : public BaseClass {

public: void DerivedClassFunction();

// ...};

void DerivedClass::DerivedClassFunction() {

MyPublicData = 1; // access allowed MyProtectedData = 2; // access allowed MyPrivateData = 3; // illegal

}

Page 27: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Controlling Inheritance

Public inheritance

class DerivedClass : public BaseClass {

public: // ...

protected: // ... private:

// ...};

Page 28: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Controlling Inheritance

public:

private:

BaseClass

protected:

public:

private:

DerivedClass

protected:

Public inheritance

Page 29: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Controlling Inheritance

Private inheritance

class DerivedClass : private BaseClass { public:

// ... protected: // ... private:

// ...};

Page 30: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Controlling Inheritance

public:

private:

BaseClass

protected:

public:

private:

DerivedClass

protected:

Private inheritance

Page 31: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Controlling Inheritance

Protected inheritance

class DerivedClass : protected BaseClass { public:

// ... protected: // ... private:

// ...};

Page 32: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Controlling Inheritance

public:

private:

BaseClass

protected:

public:

private:

DerivedClass

protected:

Public inheritance

Page 33: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Controlling Inheritance

Inheritance Type

Base classmember access

Derived class member access

public privateprivate protected private

private inaccessible

public protectedprotected protected protected

private inaccessible

public publicpublic protected protected

private inaccessible

Page 34: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Multiple InheritanceBank account inheritance hierarchy

BasicAcount

is-a

Loan

Interest

is-a

Brokerage

is-a

Checking

is-a

InterestChecking

is-a

is-a

BrokerageChecking

is-a

is-a

Page 35: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Multiple Inheritance

class DClass: public BClass1, public BClass2{ public: // public section ... protected: // protected section ... private: // private section ...};

class DClass: public BClass1, public BClass2{ public: // public section ... protected: // protected section ... private: // private section ...};

Derived class name

Class name ofbase classes

Access specifier(usually public)

Page 36: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Class Label

class Label : public WindowObject {

public:Label(SimpleWindow &w,

const Position &p, const string &Text, const color &c = Red);

color GetColor() const; void SetColor(const color

&c); void Draw();

private: color Color; string Text;};

WindowObject

is-a

Label

ColorText

Draw()GetColor()SetColor()

Page 37: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Class Label#include “lable.h”

Label::Lable(SimpleWindow &w, const Position &p, const string &t, const color &c = Red): WindowObject(w, p), Text(t), Color(c){}color Label::GetColor() const{ return Color;}void Label::SetColor(const color &c){ Color = c;} void Label::Draw(){ Position Center = GetPosition(); Position UpperLeft = Center + Position(-2.0, -2.0); Position LowerRight= Center + Position( 2.0, 2.0); GetWindow().RenderText(UpperLeft, LowerRight, Text, Color);}

Page 38: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Class LabeledEllipseShape

WindowObject

Shape

EllipseShape

is-a

is-a

Label

is-a

is-a

LabeledEllipseShape

is-a

Page 39: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Declaring a LabeledEllipseShape

class LabeledEllipseShape : public Label, public EllipseShape { public:

LabeledEllipseShape(SimpleWindow &w, const Position &p, const color &c = Red, const string &Text=“R”, float Width = 1.0, float Height = 2.0);

void Draw();};

Page 40: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Implementing LabeledEllipseShape

LabeledEllipseShape::LabeledEllipseShape(SimpleWindow &w, const Position &p, const color &c, const string &Text, float Width, float Height) :EllipseShape(w, p, c, Width, Height), Label(w, p, Text, c){ //No body needed}

void LabeledEllipseShape::Draw(){ EllipseShape::Draw(); Label::Draw();}

Page 41: Inheritance Mechanism for deriving new classes from existing classes Chapter 13

End of Chapter End of Chapter 1313

Exercises 13.16Exercises 13.17Exercises Read the section 13.7 of textbook

(Page743~756) carefully and rewrite the program Prettier Kaleidoscope

Home worksHome works