Vous êtes sur la page 1sur 15

COMS12800

Department of Computer Science

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

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 3 | 1

So faron making classes more re-usable

Recap: Basic Inheritance


PersonalCard.h
typedef class Card {

// code fragment for


// REPRESENTING A PERSONALCARD
#include "Card.h"
//include base class
//defining a blueprint for all Cards

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

public: //contents anyone can access


PersonalCard();

//constructor

//all the methods showId(),


//showBalance(), PayLunch(),
//as well as the variables id and
//balance are automatically part of
//this class because of parent class Card

int id;
private:
int balance;
} Card;

//NEW METHODS things a PersonalCard


//can do and Card cannot
void showName(); //print name of cardholder

PersonalCard
name

Susann Sample

//NEW PROPERTIES things a PersonalCard


//has and Card has not
std::string name;
//name of cardholder
private: //contents hidden from others

}
Tilo Burghardt

PersonalCard;

defining functionality
that PersonalCard
has and Card cannot
provide

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 3 | 2

So faron understanding the start of an objects lifetime

Recap: Recursive Construction


// mini all-in-one program fragment for
// understanding sequences of constructor calls
#include <iostream>
class Parent {
public: Parent () { std::cout << Parent's turn" << std::endl; }
};
class Child : public Parent {
public: Child () { std::cout << Child's turn" << std::endl; }
};
int main()
{
Child child;
}

An object child of
class Child is created
1st

the constructor
Parent::Parent()

is called
2nd its own constructor
Child::Child()

is called

Note: all parent class constructors are called (starting with


the base class first) before the class's own constructor is invoked
Tilo Burghardt

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 3 | 3

So faron making classes more versatile

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

providing different ways for


constructing a PersonalCard
object,

private: //contents hidden from others


} PersonalCard;

Tilo Burghardt

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 3 | 4

So faron when to use inheritance

Recap: Clean use of Inheritance


it is essential that you understand inheritance so you
can use libraries and understand their documentation
inheritance is used too often, when it shouldn't be
because
it can easily lead to tightly coupled classes
when used wrongly it is limiting flexibility rather than
extending it

use the "isa-relationship to check whether inheritance may be


a good idea
For example, a PersonalCard is a Card, a VW is a Car,
a student is a person, a triangle is a shape,
but a PointIn3D IS NOT a PointIn2D
Tilo Burghardt

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 3 | 5

More Details on Inheritance

Inheritance from Data Structures


a class can also be
derived from a struct
it allows to bridge between
C data structures and
C++ class concept
it is of general interest
when only fields
(i.e. no methods) are
to be passed on

Tilo Burghardt

// code snippet showing


// possibility of inheritance
// from pure data structure
struct ParentData {
int x;
int y;
};
// the derived class myClass
// has inherited all fields
// from ParentData
class myClass : public ParentData {
public:
myClass();
~myClass();
int z;
// x and y are implicitly part of
// myClass
};

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 3 | 6

More Details on Inheritance

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

// code snippet showing different


// levels of access control
class Parent {
public:
int x;
private:
int y;
protected: int z;
};
//in A: x is public, y is not
// accessible, z is protected
class A : public Parent {};
//in B: x is private, y is not
// accessible, z is private
class B : private Parent {};
//in C: x is protected, y is not
// accessible, z is protected
class C : protected Parent {};

there are three different access specifiers you can use:


public...public and protected members of the base class remain
public (i.e. fully accessible) and protected (i.e. accessible by children only)
members of the derived class
private...public and protected members of the base class become
private (i.e. accessible by this class only) members of the derived class
protected...public and protected members of the base class are
protected members of the derived class, that is they are only accessible
by classes that are derived from the derived class (i.e. its children)
Tilo Burghardt

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 3 | 7

More Details on Inheritance

Access Control Defaults

if the derived structure is declared with the


keyword class, the default access specifier
(i.e. without explicitly providing one) in its
base list specifiers is private
if the derived structure is declared with the
keyword struct, the default access specifier
in its base list specifiers is public

// code snippet showing


// default access control
// to members
struct DataStructure {
int x; //default: public
};
class DataClass {
int x; //default: private
};
void main() {

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
};

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 3 | 8

More Details on Inheritance

Access Control Details


you can use any number of access specifiers in any order
an access specifier specifies the accessibility of members
that follow it until the next access specifier or until the
end of the class definition
// code snippet showing the rules
// for access control on typedefs
if you later define a class member
class A {
class B { }; // B is private by default
within its class definition,
public:
typedef B C; // C is now public
its access specification must be
};
the same as its declaration
void main() {
if you add access control to a
A::C x; // allowed, since C is public
typedef name, it affects only
// A::B y will lead to a compiler error
// since B is private and the typedef
the typedef name, not its
// only affects the typedef name
}
arguments
:: is also known as scope resolution operator, it specifies
which member (here: member of A) is to be used (here: C)
Tilo Burghardt

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 3 | 9

More Details on Inheritance

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

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 3 | 10

So faron making classes more re-usable

Overriding Example
PersonalCard.h

// code fragment for


// REPRESENTING A PERSONALCARD
#include "Card.h"
//include base class
typedef class Card {

//defining a blueprint for all Cards


typedef class PersonalCard : public Card {

public:

public: //contents anyone can access


Card();
~Card();
void showId();
void showBalance();
void payLunch();
int id;
protected:
int balance;
} Card;

PersonalCard();

//constructor

//all the methods showId(),


//showBalance(),
//as well as the variables id and
//balance are automatically part of
//this class because of parent class Card
//NEW METHODS things a PersonalCard
//can do and Card cannot
void showName(); //print name of cardholder

void payLunch() { //replaces payLunch() of Card


balance--; //lunch is more expensive on PersonalCard than on Card
balance--;

}
//NEW PROPERTIES things a PersonalCard
//has and Card has not
std::string name;
//name of cardholder
private: //contents hidden from others
} PersonalCard;

Tilo Burghardt

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 3 | 11

Extending the Concept of Inheritance

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

// direct base class


class ParentA { /* ... */ };
// direct base class
class ParentB { /* ... */ };
//Child class derived by multiple inheritance

Child

class Child : public ParentA ,


public ParentB
{ /*...*/ };

Example Inheritance Graph:


Child is derived using Multiple Inheritance
having both ParentA and ParentB as direct
base classes
Tilo Burghardt

Example using two classes as


List of direct parents blueprints for the class Child,
known as base list both ParentA and ParentB
act as base classes

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 3 | 12

Extending the Concept of Inheritance

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

This creates ambiguities!


Child
Tilo Burghardt

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 3 | 13

Multiple Inheritance Example

Ambiguity Resolution via using


// code snippet illustrating multiple inheritance
class Common {
int x;
public:
int y;
protected:
int z;
};
class ParentA : public Common {};
class ParentB : protected Common {};
class Child : public ParentA , public ParentB {
// resolve ambiguity by choosing one of the
// y properties and hiding the other

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

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 3 | 14

Outlook to next Lecture

Virtual Functions
Abstract Classes
Tilo Burghardt

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 3 | 15

Vous aimerez peut-être aussi