Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of...

27
Cours de C++ Héritage Cécile Braunstein [email protected] Cours de C++ 1 / 21

Transcript of Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of...

Page 1: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

Cours de C++

Héritage

Cécile [email protected]

Cours de C++ 1 / 21

Page 2: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

Basic casessi

de

B

A

Cwidth

hei

gh

t

rad

ius

Cours de C++ 2 / 21

Page 3: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

Basic casessi

de

B

A

Cwidth

hei

gh

t

rad

ius

Circle

_center

_radius

GetCenter()

Draw()

Erase()

Square

_center

_side

GetCenter()

Draw()

Erase()

Rectangle

_center

_width

_height

GetCenter()

Draw()

Erase()

Triangle

_center

_pointA

_pointB

_pointC

GetCenter()

Draw()

Erase()

Cours de C++ 2 / 21

Page 4: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

Look more closer

Circle

_center

_radius

GetCenter()

Draw()

Erase()

Square

_center

_side

GetCenter()

Draw()

Erase()

Rectangle

_center

_width

_height

GetCenter()

Draw()

Erase()

Triangle

_center

_pointA

_pointB

_pointC

GetCenter()

Draw()

Erase()

Cours de C++ 3 / 21

Page 5: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

Look more closer

Circle

_center

_radius

GetCenter()

Draw()

Erase()

Square

_center

_side

GetCenter()

Draw()

Erase()

Rectangle

_center

_width

_height

GetCenter()

Draw()

Erase()

Triangle

_center

_pointA

_pointB

_pointC

GetCenter()

Draw()

Erase()

Cours de C++ 3 / 21

Page 6: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

Look more closer

Circle

_center

_radius

GetCenter()

Draw()

Erase()

Square

_center

_side

GetCenter()

Draw()

Erase()

Rectangle

_center

_width

_height

GetCenter()

Draw()

Erase()

Triangle

_center

_pointA

_pointB

_pointC

GetCenter()

Draw()

Erase()

Cours de C++ 3 / 21

Page 7: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

Class hierarchyFigure

_center

GetCenter()

Draw()

Erase()

Polygon

_Draw()

Square

Draw()

_side

Rectangle

_height

_width

Draw()

Circle

Draw()

_radius

Triangle

_pointB

_pointA

_pointC

Draw()

Cours de C++ 4 / 21

Page 8: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

Protection revisited

�class Figure {private :Point _center;

public :Figure(Point& center);

Point& GetCenter();void Draw();void Erase();

};� �Cours de C++ 5 / 21

Page 9: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

Protection revisited

�class Figure {protected :Point _center;

public :Figure(Point& center);

Point& GetCenter();void Draw();void Erase();

};� �Cours de C++ 5 / 21

Page 10: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

More about protection

Let B and C be two classes such that C derived from B publicaly.

private and public

• private members of B : Only class B may access to thesemembers

• public members of B : Everyone may access to these members

protected• B and C have access to these members• They are still part of the interface• Users of class C can not have direct access to these members

Cours de C++ 6 / 21

Page 11: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

Composition of protection

3 types of inheritance• public : Like the definition of a sub-type.• private or protected : Hide details of the implementation

Change access to the class membersMembers of the base class

public protected private

Der

ived

clas

s

publ

ic

public protected no access

prot

ecte

d

protected protected no access

priv

ate

private private no access

Cours de C++ 7 / 21

Page 12: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

Constructor

Run of the constructor for derived object1 Allocating memory space for the entire object (base-class +

derived-class members)2 Calling the base-class constructor to initialize the base-class part

of the object3 Initializing the members of the derived class as directed by the

constructor initializer4 Executing the body of the constructor, if any

Constructors of the base-class are always called.

Cours de C++ 8 / 21

Page 13: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

Destructor

Run of the destructor for derived object1 Executing the body of the destructor, if any2 Destroying the members of the derived class as directed by the

destructor in the opposite order3 Calling the base-class destructor4 Deallocating memory space for the entire object (base-class +

derived-class members)

Destructors of the base-class are always called.

Cours de C++ 9 / 21

Page 14: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

Example - base class

�class student{protected:double final, partiel;std::vector<double> homework;public:student();student(std::istream&);std::string name() const;std::istream& read(std::istream&);double grade();};

bool compare(const student& s1, const student& s2);bool compare_grade(const student& s1, const student& s2)

;� �Cours de C++ 10 / 21

Page 15: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

Example - derived class

�class stagiaire: public student{private:double stage;

public:stagiaire();stagiaire(std::istream&);double grade() const;

std::istream& read(std::istream&);

};� �

Cours de C++ 11 / 21

Page 16: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

Example - constructor

student constructor�student::student():final(0):midterm(0){}

student::student(std::istream& in){read(in);

}� �stagiaire constructor�stagiaire::stagiaire():stage(0){}

stagiaire::stagiaire(std::istream in){read(in);

}� �Cours de C++ 12 / 21

Page 17: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

Example - constructor

student constructor�student::student():final(0):midterm(0){}

student::student(std::istream& in){read(in);

}� �stagiaire constructor�stagiaire::stagiaire():stage(0){}

stagiaire::stagiaire(std::istream in){read(in);

}� �Cours de C++ 12 / 21

Page 18: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

Example - constructor

student constructor�student::student():final(0):midterm(0){}

student::student(std::istream& in){read(in);

}� �stagiaire constructor�stagiaire::stagiaire():stage(0){}

stagiaire::stagiaire(std::istream in){read(in);

}� �Cours de C++ 12 / 21

Page 19: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

Example - use

Constructor�stagiare g1(cin);stagiare g2(cin);

student e1(cin);student e2(cin);� �Function call�bool compare(const student& s1, const student& s2){return s1.name() < s2.name();}

compare(g1,g2);compare(e1,e2);compare(g,e);� �

Cours de C++ 13 / 21

Page 20: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

Static cast

Type known at compile time�class A {...} ;class B : public A {...};� �ObjectB y;A x = y;

A

B

y

x APointer and referenceB* y;A* x = y;

A

B

y

x

Cours de C++ 14 / 21

Page 21: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

Dynamic cast 1

Type known at run time�class A {...} ;class B : public A {...};� �Only for references pointers�B x;A y = x;A *ptry = &x;A &refy = x;� �• the static type of *ptry and refx is A• the dynamic type of *ptry and refx is B

Cours de C++ 15 / 21

Page 22: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

Dynamic cast 2

Syntax�dynamic_cast<T*>(p)� �• p is a pointer• Transform the type of p in T

• If it’s not possible returns NULL�dynamic_cast<T&>(p)� �• p is a reference• Transform the type of p in T

• If it’s not possible raise an exception

Cours de C++ 16 / 21

Page 23: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

An other example

Comparing gradeSometimes, we really want to know the real type at run time.�bool compare_grade(const student& s1, const student& s2){return s1.grade() < s2.grade();}

student e1,e2;stagiaire s1,s2;compare_grade(e1,e2);compare_grade(s1,s2);� �How to be sure that the right functiongrade is used ?

Cours de C++ 17 / 21

Page 24: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

Polymorphism

DefinitionPolymorphism defines the notion that a the behavior of an objectdoesn’t have to be known at compile time. The real object type may beknown at run time.

What for ?• Build container with not exactly same type element inside• For the destructor• Re-use the code for an other application

Cours de C++ 18 / 21

Page 25: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

virtual function�class student{public :virtual double grade() const;//...};� �

Virtual functionWhen ?

• Calling a function that depends on the actual of object• Making this decision at run time

How ?• Keyword virtual used only inside the class definition• Do not need to repeat this keyword in the implementation• When it’s inherited, no need to repeat this keyword

Û A destructor has to be virtualCours de C++ 19 / 21

Page 26: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

Multiple inheritance

class X

class B

class A

class C

Derived from many classes�class A { /* ... */ };class B : public A { /* ... */ };class C : public A { /* ... */ };class X : public B, public C { /* ... */ };� �• The order of derivation is relevant only to determine the order of

default initialization by constructors and cleanup by destructors.• A derived class can inherit an indirect base-class more than once

Leads to ambiguitiesCours de C++ 20 / 21

Page 27: Cours de C++ - Héritagececile/cours/2009/heritage.pdf · Cours de C++ 8 / 21. Destructor Run of the destructor for derived object 1 Executing the body of the destructor, if any 2

Resolving ambiguities

Members with same names from different classes• C++ compilers resolves some ambiguities by choosing the

minimal path to a member• Use the scope operator A::function

Two same members from different class• Sometimes it’s the correct behavior• Virtual inheritance�

class A { /* ... */ };class B : public virtual A{ /* ... */ };class C : public virtual A{ /* ... */ };class X : public B, public C { /* ... */ };� �

Cours de C++ 21 / 21