Upload
others
View
8
Download
0
Embed Size (px)
Citation preview
Object-oriented programming Lecture №14
Sergey Aksenov
Object-oriented programming Lecture №14
Multiple inheritance
2
In C++, a class may be derived from more than one base class—a technique known as multiple inheritance in which a derived class inherits the members of two or more base classes. • Multiple inheritance is a powerful capability when used properly.
Multiple inheritance should be used when an “is a” relationship exists between a new type and two or more existing types (i.e., type A “is a” type B and type A “is a” type C).
• Multiple inheritance can introduce complexity into a system. Great care is required in the design of a system to use multiple inheritance properly; it should not be used when single inheritance and/or composition will do the job. H. Daitel and P. Daitel,
C++ How to Program (5th Edition)
Object-oriented programming Lecture №14
Multiple inheritance
3
Example from www.learncpp.com
Person Employee
Teacher
Object-oriented programming Lecture №14
Multiple inheritance
4
Example from www.learncpp.com
//Person.h #include <string> class Person { private: std::string m_strName; int m_nAge; bool m_bIsMale; public: Person(std::string strName, int nAge, bool bIsMale) : m_strName(strName), m_nAge(nAge), m_bIsMale(bIsMale) { } std::string GetName() const { return m_strName; } int GetAge() const { return m_nAge; } bool IsMale() const { return m_bIsMale; } };
Object-oriented programming Lecture №14
Multiple inheritance
5
//Employee.h #include <string> class Employee { private: std::string m_strEmployer; double m_dWage; public: Employee(std::string strEmployer, double dWage) : m_strEmployer(strEmployer), m_dWage(dWage) { } std::string GetEmployer() const { return m_strEmployer; } double GetWage() const { return m_dWage; } };
Object-oriented programming Lecture №14
Multiple inheritance
6
// Teacher.h #include "Employee.h" #include "Person.h" #include <string> // Teacher publicly inherits Person and Employee class Teacher: public Person, public Employee { private: std::string m_strDegree; public: Teacher(std::string strName, int nAge, bool bIsMale, std::string strEmployer, double dWage, std::string strDegree) : Person(strName, nAge, bIsMale), Employee(strEmployer, dWage), m_strDegree(strDegree) { } };
To use multiple inheritance, simply specify each base class (just like in single inheritance), separated by a comma.
www.learncpp.com
Object-oriented programming Lecture №14
Multiple inheritance
7
// inheritance.cpp #include <string> #include <iostream> #include "Teacher.h"
void PrintPerson(const Person &person){ std::cout << person.GetName() << " is " << person.GetAge() << " years old.\n"; } double CalcTax(const Employee &employee){ return employee.GetWage()*0.14; } int main(){ Teacher teacher("Aksenov", 32, true, "MIEM", 1, "Ph.D."); PrintPerson(teacher); std::cout << "Monthly tax is " << CalcTax(teacher) << std::endl; return 0; }
Object-oriented programming Lecture №14
Multiple inheritance
8
Object-oriented programming Lecture №14
Ambiguous access
9
#include "stdafx.h"
#include <iostream>
class Base1{
public:
int f(){return 1;}
};
class Base2{
public:
int f(){return 2;}
};
class Derived: public Base1, public Base2{
};
int main(){
Derived x;
std::cout << x.f() << std::endl;//ERROR:
//ambiguous access of 'f'
return 0;
}
Object-oriented programming Lecture №14
Ambiguous access
10
#include "stdafx.h"
#include <iostream>
class Base1{
public:
int f(){return 1;}
};
class Base2{
public:
int f(){return 2;}
};
class Derived: public Base1, public Base2{
};
int main(){
Derived x;
std::cout << x.Base1::f() << std::endl;//OK!
return 0;
}
Object-oriented programming Lecture №14
Ambiguous access
11
#include "stdafx.h"
#include <iostream>
class Base1{
public:
virtual int f()=0;
};
class Base2{
public:
virtual int f()=0;
};
class Derived: public Base1, public Base2{
int f(){return 0;}
};
int main(){
Derived x;
std::cout << x.f() << std::endl;//OK!
return 0;
}
Object-oriented programming Lecture №14
Multiple inheritance
12
//Person.h #include <string> class Person{ public: virtual std::string GetName()=0; virtual int GetAge()=0; virtual bool IsMale()=0; }; void PrintPerson(const Person &person); . . . __________________________________________________________________ //Person.cpp #include "Person.h" #include <iostream> void PrintPerson(const Person &person){ std::cout << person.GetName() << " is " << person.GetAge() << " years old.\n"; }
Object-oriented programming Lecture №14
Multiple inheritance
13
//Employee.h #include <string> class Employee { virtual std::string GetEmployer() = 0; virtual double GetWage() = 0; }; double CalcTax(const Employee &employee); . . . __________________________________________________________________ // Employee.cpp #include "Employee.h" double CalcTax(const Employee &employee){ return employee.GetWage()*0.14; }
Object-oriented programming Lecture №14
Multiple inheritance
14
// Teacher.h
#include "Employee.h"
#include "Person.h"
class Teacher: public Person, public Employee {
private:
std::string m_strDegree;
std::string m_strName; int m_nAge; bool m_bIsMale;
std::string m_strEmployer; double m_dWage;
public:
Teacher( std::string strName, int nAge, bool bIsMale,
std::string strEmployer, double dWage, std::string strDegree)
: m_strName(strName), m_nAge(nAge), m_bIsMale(bIsMale),
m_strEmployer(strEmployer), m_dWage(dWage), m_strDegree(strDegree){ }
// Virtual functions implementation
std::string GetName() const { return m_strName; }
int GetAge() const { return m_nAge; }
bool IsMale() const { return m_bIsMale; }
std::string GetEmployer() const { return m_strEmployer; }
double GetWage() const { return m_dWage; }
};
Object-oriented programming Lecture №14
Multiple inheritance
15
// inheritance.cpp #include <string> #include <iostream> #include "Teacher.h"
int main(){ Teacher teacher("Aksenov", 32, true, "MIEM", 1, "Ph.D."); PrintPerson(teacher); std::cout << "Monthly tax is " << CalcTax(teacher) << std::endl; return 0; }
Object-oriented programming Lecture №14
Multiple inheritance
16
class Queue {}; class CashierQueue : public Queue {}; class LunchQueue : public Queue {}; class LunchCashierQueue : public LunchQueue, public CashierQueue {};
Object-oriented programming Lecture №14
Multiple inheritance
17
class Queue {}; class CashierQueue : virtual public Queue {}; class LunchQueue : virtual public Queue {}; class LunchCashierQueue : public LunchQueue, public CashierQueue {};
Virtual base class
• The virtual keyword ensures that only one copy of the subobject Queue is included
Object-oriented programming Lecture №14
References
• www.deitel.com
• www.learncpp.com
• msdn.microsoft.com
18