Académique Documents
Professionnel Documents
Culture Documents
University of Bristol, UK
Introduction to C++
Lecturer:
Dr Tilo Burghardt
( mailto: tilo@cs.bris.ac.uk )
Web Materials:
http://www.cs.bris.ac.uk/Teaching/Resources/COMS12800
LECTURE 3
MULTIPLE
INHERITANCE
&
ACCESS
CONTROL
Tilo Burghardt
public:
typedef class
Card();
~Card();
void showId();
void showBalance();
void payLunch();
PersonalCard :
public
Card {
defining a class
PersonalCard that
has everything a Card
has, and more
//constructor
int id;
private:
int balance;
} Card;
PersonalCard
name
Susann Sample
}
Tilo Burghardt
PersonalCard;
defining functionality
that PersonalCard
has and Card cannot
provide
An object child of
class Child is created
1st
the constructor
Parent::Parent()
is called
2nd its own constructor
Child::Child()
is called
Recap: Overloading
PersonalCard.h
// code fragment for
// REPRESENTING A PERSONALCARD
#include "Card.h"
//include base class
//defining a blueprint for all Cards
typedef class PersonalCard : public Card {
public: //contents anyone can access
PersonalCard();
//constructor 1
PersonalCard(std::string initName);
//constructor 2
PersonalCard(std::string initName, int initId); //constructor 3
//NEW METHODS things a PersonalCard
//can do and Card cannot
void showName(); //print name of cardholder
//NEW PROPERTIES things a PersonalCard
//has and Card has not
std::string name;
//name of cardholder
Tilo Burghardt
Tilo Burghardt
Access Control
when deriving a class, an access specifier
can precede base classes in the base list
this allows the derived class to restrict
access to the members of a base class
Similarly:
members of classes (i.e. properties or methods)
declared with the keyword class are
private by default
members of structures declared with the
keywords struct or union are
public by default
Tilo Burghardt
DataStructure myData;
DataClass myClass;
myData.x = 1; //allowed
// myClass.x will lead
// to error since x is
// private by default
};
Overriding Functions
Method Overridingallows a derived class to provide a specific
(usually different) implementation of a method that is already
provided by one of its parent classes
the derived class overrides (i.e. replaces) the implementation
in the parent class by providing a method that has same name,
same parameters or signature, and same return type as the
method in the parent class
For instance: the payLunch() method in PersonalCard could
override the payLunch() method in Card to change the cost
for a lunch portion for using different cards
Tilo Burghardt
Overriding Example
PersonalCard.h
public:
PersonalCard();
//constructor
}
//NEW PROPERTIES things a PersonalCard
//has and Card has not
std::string name;
//name of cardholder
private: //contents hidden from others
} PersonalCard;
Tilo Burghardt
Multiple Inheritance I
one can derive a class from any number of base classes
deriving a class from more than one direct base class is
called multiple inheritance
// code snippet illustrating multiple
// inheritance
ParentA
ParentB
Child
Multiple Inheritance II
the order of derivation (i.e. which class is listed first after the : )
is only relevant to determine the order of default initialization by
constructors (i.e. same order as in list) and cleanup by
a direct base class cannot appear
Common
Common
in a base list more than once
(no direct inheritance from the
same class twice)
a derived class can inherit an
ParentA
ParentB
indirect base class more than
once
using ParentA::y;
public:
void setAy() {
y = 1; // using the y of Common which is parent of
// parentA
}
};
void main() {
Child child; // has two different ancestors
// of class common with same name
child.setAy();
}
Tilo Burghardt
Virtual Functions
Abstract Classes
Tilo Burghardt