object oriented software design ii -...

46
Object Oriented Software Design II Inheritance Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant’Anna – Pisa February 29, 2012 G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 1 / 32

Upload: others

Post on 27-May-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Object Oriented Software Design IIInheritance

Giuseppe Liparihttp://retis.sssup.it/~lipari

Scuola Superiore Sant’Anna – Pisa

February 29, 2012

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 1 / 32

Page 2: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Outline

1 Inheritance

2 Virtual functions

3 Virtual Destructors

4 Pure virtual functions

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 2 / 32

Page 3: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Outline

1 Inheritance

2 Virtual functions

3 Virtual Destructors

4 Pure virtual functions

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 3 / 32

Page 4: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Code reuse

In C++ (like in all OO programming), one of the goals is to re-useexisting codeThere are two ways of accomplishing this goal: composition andinheritance

Composition consists defining the object to reuse inside the newobjectComposition can also expressed by relating different objects withpointers each otherInheritance consists in enhancing an existing class with new morespecific code

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 4 / 32

Page 5: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Inheritance

class A {int i;

protected:int j;

public:A() : i(0),j(0) {};~A() {};int get() const {return i;}int f() const {return j;}

};

class B : public A {int i;

public:B() : A(), i(0) {};~B() {};void set(int a) {j = a; i+= j}int g() const {return i;}

};

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 5 / 32

Page 6: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Syntax

How to define the derived class

class B : public A {int i;

public:B() : A(),

i(0){}~B() {}void set(int a) {

j = a;i+= j;

}int g() const {

return i;}

};

class B derives publicly from A

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 6 / 32

Page 7: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Syntax

How to define the derived class

class B : public A {int i;

public:B() : A(),

i(0){}~B() {}void set(int a) {

j = a;i+= j;

}int g() const {

return i;}

};

class B derives publicly from A

Therefore, to construct B, we mustfirst construct A

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 6 / 32

Page 8: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Syntax

How to define the derived class

class B : public A {int i;

public:B() : A(),

i(0){}~B() {}void set(int a) {

j = a;i+= j;

}int g() const {

return i;}

};

class B derives publicly from A

Therefore, to construct B, we mustfirst construct A

j is a member of A declared asprotected; therefore, B can ac-cess it

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 6 / 32

Page 9: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Syntax

How to define the derived class

class B : public A {int i;

public:B() : A(),

i(0){}~B() {}void set(int a) {

j = a;i+= j;

}int g() const {

return i;}

};

class B derives publicly from A

Therefore, to construct B, we mustfirst construct A

j is a member of A declared asprotected; therefore, B can ac-cess it

i instead is a member of B. There ifanother i that is a private mem-ber of A, so it cannot be accessedfrom B

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 6 / 32

Page 10: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Use of Inheritance

Now we can use B as a special version of A

int main(){

B b;cout << b.get() << endl; // calls A::get();b.set(10);cout << b.g() << endl;b.g();A *a = &b; // Automatic type conversion (upcasting)a->f();B *p = new A; // error!

}

See./examples/04.inheritance-examples/example1.cpp

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 7 / 32

Page 11: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Public inheritance

Public inheritance means that the derived class inherits the sameinterface of the base class

All members in the public part of A are also part of the publicpart of BAll members in the protected part of A are part of theprotected part of BAll members in the private part of A are not accessible from B.

This means that if we have an object of type B, we can use allfunctions defined in the public part of B and all functionsdefined in the public part of A.

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 8 / 32

Page 12: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Overloading and hiding

There is no overloading across classes

class A {...

public:int f(int, double);

}

class B : public A {...

public:void f(double);

}

int main(){

B b;b.f(2,3.0);

// ERROR!}

A::f() has been hiddenby B::f()

to get A::f() into scope,the using directive isnecessary

using A::f(int,double);

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 9 / 32

Page 13: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Upcasting

It is possible to use an object of the derived class through apointer to the base class.

class A {public:

void f() { ... }};class B : public A {public:

void g() { ... }};

A* p;

p = new B();

p->f();

p->g();

A pointer to the base class

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 10 / 32

Page 14: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Upcasting

It is possible to use an object of the derived class through apointer to the base class.

class A {public:

void f() { ... }};class B : public A {public:

void g() { ... }};

A* p;

p = new B();

p->f();

p->g();

A pointer to the base class

The pointer now points to an objectof a derived class

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 10 / 32

Page 15: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Upcasting

It is possible to use an object of the derived class through apointer to the base class.

class A {public:

void f() { ... }};class B : public A {public:

void g() { ... }};

A* p;

p = new B();

p->f();

p->g();

A pointer to the base class

The pointer now points to an objectof a derived class

Call a function of the interface ofthe base class: correct

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 10 / 32

Page 16: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Upcasting

It is possible to use an object of the derived class through apointer to the base class.

class A {public:

void f() { ... }};class B : public A {public:

void g() { ... }};

A* p;

p = new B();

p->f();

p->g();

A pointer to the base class

The pointer now points to an objectof a derived class

Call a function of the interface ofthe base class: correct

Error! g() is not in the interfaceof the base class, so it cannot becalled through a pointer to the baseclass!

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 10 / 32

Page 17: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

References

Same thing is possible with references

class A {public:

void f() { ... }};class B : public A {public:

void g() { ... }};

void h(A &){

h.f();h.g();

}

B obj;

h(obj);

Function h takes a reference to thebase class

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 11 / 32

Page 18: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

References

Same thing is possible with references

class A {public:

void f() { ... }};class B : public A {public:

void g() { ... }};

void h(A &){

h.f();h.g();

}

B obj;

h(obj);

Function h takes a reference to thebase class

Of course, it is possible to call functionsin the interface of the base class

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 11 / 32

Page 19: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

References

Same thing is possible with references

class A {public:

void f() { ... }};class B : public A {public:

void g() { ... }};

void h(A &){

h.f();h.g();

}

B obj;

h(obj);

Function h takes a reference to thebase class

Of course, it is possible to call functionsin the interface of the base class

This is an error! g() is not in the in-terface of A

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 11 / 32

Page 20: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

References

Same thing is possible with references

class A {public:

void f() { ... }};class B : public A {public:

void g() { ... }};

void h(A &){

h.f();h.g();

}

B obj;

h(obj);

Function h takes a reference to thebase class

Of course, it is possible to call functionsin the interface of the base class

This is an error! g() is not in the in-terface of A

Calling the function by passing a ref-erence to an object of a derived class:correct.

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 11 / 32

Page 21: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Extension through inheritance

Why this is useful?All functions that take a reference (or a pointer) to A as a parameter,continue to be valid and work correctly when we pass a reference(or a pointer) to BThis means that we can reuse all code that has been written for A,also for BIn addition, we can write additional code specifically for B

Therefore,we can reuse existing code also with the new classWe can extend existing class to implement new functionality

What about modifying (customize, extend, etc.) the behaviour ofexisting code without changing it?

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 12 / 32

Page 22: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Outline

1 Inheritance

2 Virtual functions

3 Virtual Destructors

4 Pure virtual functions

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 13 / 32

Page 23: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Virtual functions

Let’s introduce virtual functions with an example

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 14 / 32

Page 24: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Implementation

class Shape {protected:

double x,y;public:

Shape(double x1, double y2);virtual void draw() = 0;

};

class Circle : public Shape {double r;

public:Circle(double x1, double y1,

double r);virtual void draw();

};

class Rect : public Shape {double a, b;

public:Rect(double x1, double y1,

double a1, double b1);virtual void draw();

};

class Triangle : public Shape {double a, b;

public:Triangle(double x1, double y1,

double a1, double b1);virtual void draw();

};

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 15 / 32

Page 25: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

We would like to collect shapes

Let’s make an array of shapes

Shapes * shapes[3];

shapes[0] = new Circle(2,3,10);shapes[1] = new Rect(10,10,5,4);shapes[2] = new Triangle(0,0,3,2);

// now we want to draw all the shapes ...

for (int i=0; i<3; ++i) shapes[i]->draw();

We would like that the right draw function is called

However, the problem is that Shapes::draw() is called

The solution is to make draw virtual

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 16 / 32

Page 26: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Virtual functions

class Shape {protected:

double x,y;public:

Shape(double xx, double yy);void move(double x, double y);virtual void draw();virtual void resize(double scale);virtual void rotate(double degree);

};

class Circle : public Shape {double r;

public:Circle(double x, double y,

double r);void draw();void resize(double scale);void rotate(double degree);

};

move() is a regularfunction

draw(), resize()and rotate() arevirtual

see shapes/

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 17 / 32

Page 27: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Virtual table

When you put the virtual keyword before a function declaration,the compiler builds a vtable for each class

Circle – vptr

Rect – vptr

Triangle – vptr

void draw()

void resize()

void rotate()

void draw()

void resize()

void rotate()

void draw()

void resize()

void rotate()

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 18 / 32

Page 28: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Calling a virtual function

When the compiler sees a call to a virtual function, it performs alate binding, or dynamic binding

each object of a class derived from Shape has a vptr as firstelement.

It is like a hidden member variable

The virtual function call is translated intoget the vptr (first element of object)move to the right position into the vtable (depending on whichvirtual function we are calling)call the function

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 19 / 32

Page 29: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Dynamic binding vs static binding

Which function are called in the following code?

class A {public:

void f() { cout << "A::f()" << endl; g(); }virtual void g() { cout << "A::g()" << endl; }

};class B : public A {public:

void f() { cout << "B::f()" << endl; g(); }virtual void g() { cout << "B::g()" << endl; }

};...

A *p = new B;p->g();p->f();

B b;A &r = b;r.g();r.f();

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 20 / 32

Page 30: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Overloading and overriding

When you override a virtual function, you cannot change thereturn value

Simply because the compiler will not know which function toactually call

There is only one exception to the previous rule:

if the base class virtual method returns a pointer or a reference toan object of the base class . . .

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 21 / 32

Page 31: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Overloading and overriding

When you override a virtual function, you cannot change thereturn value

Simply because the compiler will not know which function toactually call

There is only one exception to the previous rule:

if the base class virtual method returns a pointer or a reference toan object of the base class . . .. . . the derived class can change the return value to a pointer orreference of the derived class

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 21 / 32

Page 32: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Overload and override

Examples

Correct

class A {public:

virtual A& f();int g();

};

class B: public A {public:

virtual B& f();double g();

};

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 22 / 32

Page 33: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Overload and override

Examples

Correct

class A {public:

virtual A& f();int g();

};

class B: public A {public:

virtual B& f();double g();

};

Wrong

class A {public:

virtual A& f();};

class C: public A {public:

virtual int f();};

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 22 / 32

Page 34: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Overloading and overriding

When you override a virtual function, you cannot change thereturn value

Simply because the compiler will not know which function toactually call

There is only one exception to the previous rule:

if the base class virtual method returns a pointer or a reference toan object of the base class . . .

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 23 / 32

Page 35: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Overloading and overriding

When you override a virtual function, you cannot change thereturn value

Simply because the compiler will not know which function toactually call

There is only one exception to the previous rule:

if the base class virtual method returns a pointer or a reference toan object of the base class . . .. . . the derived class can change the return value to a pointer orreference of the derived class

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 23 / 32

Page 36: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Overload and override

Examples

Correct

class A {public:

virtual A& f();int g();

};

class B: public A {public:

virtual B& f();double g();

};

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 24 / 32

Page 37: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Overload and override

Examples

Correct

class A {public:

virtual A& f();int g();

};

class B: public A {public:

virtual B& f();double g();

};

Wrong

class A {public:

virtual A& f();};

class C: public A {public:

virtual int f();};

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 24 / 32

Page 38: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Outline

1 Inheritance

2 Virtual functions

3 Virtual Destructors

4 Pure virtual functions

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 25 / 32

Page 39: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Destructors

What happens if we try to destruct an object through a pointer tothe base class?

class A {public:

A();~A();

};

class B : public A {public:

B();~B();

};

int main() {A *p;p = new B;// ...delete p;

}

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 26 / 32

Page 40: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Virtual destructor

This is a big mistake!The destructor of the base class is called, which “destroys” onlypart of the objectYou will soon end up with a segmentation fault (or illegal access), ormemory corruption

To solve the problem, we have to declare a virtual destructorIf the destructors are virtual, they are called in the correct order

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 27 / 32

Page 41: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Restrictions

Never call a virtual function inside a destructor!Can you explain why?

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 28 / 32

Page 42: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Restrictions

Never call a virtual function inside a destructor!Can you explain why?

You can not call a virtual function inside a constructorin fact, in the constructor, the object is only half-built, so you couldend up making a wrong thingduring construction, the object is not yet ready! The constructorshould only build the object

Same thing for the destructorduring destruction, the object is half destroyed, so you will probablycall the wrong function

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 28 / 32

Page 43: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Restrictions

Example

class Base {string name;

public:Base(const string &n) : name(n) {}virtual string getName() { return name; }virtual ~Base() { cout << getName() << endl;}

};

class Derived : public Base {string name2;

public:Derived(const string &n) : Base(n), name(n + "2") {}virtual string getName() {return name2;}virtual ~Derived() {}

};

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 29 / 32

Page 44: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Outline

1 Inheritance

2 Virtual functions

3 Virtual Destructors

4 Pure virtual functions

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 30 / 32

Page 45: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Pure virtual functions

A virtual function is pure if no implementation is provided

Example:

class Abs {public:virtual int fun() = 0;virtual ~Abs();

};class Derived public Abs {public:Derived();virtual int fun();virtual ~Derived();

};

This is a pure virtual function. Noobject of Abs can be instantiated.

One of the derived classes must fi-nalize the function to be able to in-stantiate the object.

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 31 / 32

Page 46: Object Oriented Software Design II - Inheritanceretis.sssup.it/~lipari/courses/oosd2011-2/04.inheritance.pdf · inheritance Composition consists defining the object to reuse inside

Interface classes

If a class only provides pure virtual functions, it is an interfaceclass

an interface class is useful when we want to specify that a certainclass conforms to an interfaceUnlike Java, there is no special keyword to indicate an interfaceclassmore examples in section multiple inheritance

G. Lipari (Scuola Superiore Sant’Anna) C++ Intro February 29, 2012 32 / 32