Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C++ Inheritance & Virtual Functions: Pure, Simple, Non-Virtual, Exams of Object Oriented Programming

The concepts of virtual functions and inheritance in C++ programming. It covers the differences between pure virtual functions, simple virtual functions, and non-virtual functions, and their respective purposes. It also includes a C++ tip about redefining inherited non-virtual functions and the consequences of doing so.

Typology: Exams

2021/2022

Uploaded on 09/27/2022

zylda
zylda 🇬🇧

4.5

(13)

213 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Virtual functions and
Inheritance
pf3
pf4
pf5

Partial preview of the text

Download C++ Inheritance & Virtual Functions: Pure, Simple, Non-Virtual and more Exams Object Oriented Programming in PDF only on Docsity!

Virtual functions and

Inheritance

Virtual functions

• Pure virtual functions

• Simple virtual functions

class Shape { public: virtual void draw() const = 0; virtual void error(const string &msg); … .. } class Rectangle: public Shape { …. };

Non-virtual functions

• The purpose of a non-virtual

function is to have derived

classes inherit a function

interface as well as a

mandatory implementation

C++ Tip

• Never redefine an inherited

non-virtual function

  • violates “is-a” inheritance rule
  • can lead to programming errors class B { public: void mf(); }; class D: public B { …. };

D x; // x is an object of type D B *pB = &x; // pB is pointer to x pB->mf(); // call mf through pointer D *pD = &x; // pD is pointer to x pD->mf(); // call mf through pointer