Escolar Documentos
Profissional Documentos
Cultura Documentos
Inheritance
Topic #3
CS202 3- 1
Topic #3
CS202 3- 2
Single Inheritance
Introduction to Inheritance
"Using" versus "Containing" Relationships
"Containing" Relationships...through inheritance
Inheritance and Derived Classes
Creating a Derived Class...the syntax for single
inheritance
Creating a Derived Class...constructors &
destructors
Constructor Initialization Lists
What can be Inherited?
CS202 3- 3
CS202 3- 4
CS202 3- 5
CS202 3- 6
CS202 3- 7
Inheritance Hierarchies
CS202 3- 8
Inheritance Hierarchies
CS202 3- 9
Inheritance Hierarchies
Base Class
and
Indirect Base Class
Derived Class
and
Base Class
Derived
Class
CS202 3- 10
Parent Class
and
Ancestor Class
Child Class
and
Parent Class
Child
Class
Inheritance Hierarchies
CS202 3- 11
Inheritance Hierarchies
CS202 3- 12
Single Inheritance
CS202 3- 13
Single Inheritance
Single Inheritance
CS202 3- 15
//base class
class account {
public:
account();
void statement();
private:
char name[32]; //account owner
float balance; //account balance
};
//checking class derived from account
class checking : public class account {
public:
checking();
float get_charges();
private:
float charges; //charges for current month
};
//savings class derived from account
class savings : public account {
public:
savings();
float get_interest();
private:
float interest; //interest for current month
};
CS202 3- 16
Single Inheritance
CS202 3- 17
Single Inheritance
checking
savings
savings::get_interest()
interest
checking object
account::statement()
name[]
balance
checking::get_charges()
charges
CS202 3- 18
Single Inheritance
CS202 3- 19
Single Inheritance
CS202 3- 20
Single Inheritance
CS202 3- 21
Constructors in Hierarchies
CS202 3- 22
Constructors in Hierarchies
CS202 3- 23
Constructors - Page 1 of 2
#include <iostream.h>
class account {
public:
account();
private:
char name[32];
float balance;
};
class checking : public account {
public:
checking();
private:
float charges;
};
class savings : public account {
public:
savings();
private:
float interest;
};
CS202 3- 24
Constructors - Page 2 of 2
#include "account.h"
account::account() : balance(0) {
strncpy(name, "none", 32);
name[31] = '\0';
cout <<"account constructor called" <<endl;
}
checking::checking() : charges(5) {
cout <<"checking constructor called" <<endl;
}
savings::savings() : interest(0) {
cout <<"savings constructor called" <<endl;
}
After the client saying:
checking c;
savings s;
account constructor called
checking constructor called
account constructor called
savings constructor called
CS202 3- 25
Constructors in Hierarchies
Client program:
derived obj(10,20);
derived::derived(int i, int j) {
...
CS202 3- 26
Constructors in Hierarchies
CS202 3- 27
Constructors - Page 1 of 2
class account {
public:
account(const char* name="none", float amount=0);
void statement();
private:
char name[32];
float balance;
};
class checking : public account {
public:
checking(const char* ="none", float=0, float=5);
float get_charges();
private:
float charges;
};
class savings : public account {
public:
savings(const char* ="none", float=0);
float get_interest();
private:
float interest;
};
CS202 3- 28
Constructors - Page 2 of 2
account::account(const char* n, float b) :
balance(b) {
strncpy(name, n, 32); name[31] = '\0';
}
void account::statement() {
cout <<"Account Statement" <<endl;
cout <<" name = " <<name <<endl;
cout <<" balance = " <<balance <<endl;
}
checking::checking(const char* n, float b, float c) :
account(n, b), charges(c) {}
float checking::get_charges() {
return (charges);
}
savings::savings(const char* n, float b) :
account(n, b), interest(0) {}
float savings::get_interest() {
return (interest);
}
What values do the data members have when the client says:
checking c("Sue Smith", 1000.0);
savings s("Jim Jones", 500.0);
CS202 3- 29
Constructors in Hierarchies
CS202 3- 30
Member Hiding
CS202 3- 31
Constructors
are invoked in
the order of the
base class first,
then derived
class
initializers,
followed by the
body of the
derived class
constructor.
balance(b)
{
}
strncpy(name, n, 32);
name[31] = '\0';
account(n, b),
interest(0)
{
}
savings* s;
CS202 3- 32
Destructors in Hierarchies
CS202 3- 33
Introduction to C++
Extending
Behavior
CS202 3- 34
Member Hiding
CS202 3- 35
Member Hiding
CS202 3- 36
CS202 3- 37
Overloaded Members
CS202 3- 38
CS202 3- 39
Copy Constructors
CS202 3- 40
Copy Constructors
CS202 3- 41
Copy Constructors
CS202 3- 42
CS202 3- 43
CS202 3- 45
Using Declarations
CS202 3- 46
Using Declarations
class base {
public:
void fun(int) {
cout <<"base::fun(int)" <<endl;
}
void fun(double) {
cout <<"base::fun(double)" <<endl;
}
};
class derived : public base {
public:
using base::fun; //fun(int) & fun(double) now in scope
void fun(int) { //hides fun(int) brought into scope
cout <<"derived::fun(int)" <<endl;
}
void fun(char*) { //defines new fun(char*)
cout <<"derived::fun(char*)" <<endl;
}
};
CS202 3- 47
Introduction to C++
Multiple
Inheritance
CS202 3- 48
Multiple Inheritance
CS202 3- 49
Multiple Inheritance
savings
The definition cannot be cyclical.
A direct base class cannot be specified
assets
more than once for any derived class.
CS202 3- 50
equity
Multiple Inheritance
CS202 3- 51
Multiple Inheritance
...
CS202 3- 52
Multiple Inheritance
CS202 3- 53
Multiple Inheritance
CS202 3- 54
account
checking
savings
ibc
ibc object
account::get_name()
account::get_balance()
name[]
balance
checking::get_charges()
charges
account::get_name()
account::get_balance()
name[]
balance
savings::get_interest()
interest
CS202 3- 55
ibc::get_minimum()
minimum
Virtual Inheritance
CS202 3- 56
Virtual Inheritance
checking::get_charges()
charges
savings::get_interest()
interest
ibc::get_minimum()
minimum
CS202 3- 57
account::get_name()
account::get_balance()
name[]
balance
Virtual Inheritance
CS202 3- 58
Virtual Inheritance
CS202 3- 59
Virtual Inheritance
CS202 3- 60
Virtual Inheritance
CS202 3- 61
Types of Derivation
CS202 3- 62
Types of Derivation
class derived: public base_1,
private base_2 {...};
Treatment in the
derived class with
public derivation
Treatment in the
derived class with
protected derivation
Treatment in the
derived class with
private derivation
Public
Remains Public
Becomes Protected
Becomes Private
Protected
Remains Protected
Remains Protected
Becomes Private
Private
Remains Private
Remains Private
Remains Private
CS202 3- 63