SlideShare a Scribd company logo
1 of 90
1
Inheritance in C++
Lesson #6
Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek.
2
Content
Base and Derived Classes
Single Inheritance
Declaration of derived classes
 Order of Constructor and Destructor Execution
Inherited member accessibility
Multiple Inheritance
Virtual Base Classes
3
Base and Derived Classes
A base class is a previously defined
class that is used to define new
classes
 A derived class inherits all the data
and function members of a base class
(in addition to its explicitly declared
members.)
4
Single Inheritance
Implement an “is-a” relationship
The derived class only has one base
class.
5
Example 1:
Student
• name
• id
• major
Undergraduate
• year
• minor
• etc.
Graduate
• advisor
• thesis
• research
• etc...
6
Example 2:
Publication:
• publisher
• date
Magazine:
• # of issues per year
• circulation
Book:
• ISBN
• author
7
Example:
Publication
#include “FString.h"
class Publication {
public:
void SetPublisher( const FString & p )
{publisher.Assign(p);};
void SetDate( unsigned long dt )
{date = dt;};
FString GetPublisher(){return publisher;};
unsigned long GetDate(){return date;};
private:
FString publisher;
unsigned long date;
};
8
Publication
class Magazine :public Publication {
public:
void SetIssuesPerYear( unsigned n
){issuesPerYear=n;};
void SetCirculation( unsigned long n ){
circulation=n;};
unsigned GetIssuesPerYear(){return
issuesPerYear;};
unsigned long GetCirculation(){return circulation;};
private:
unsigned issuesPerYear;
unsigned long circulation;
};
9
Publication
class Book :public Publication {
public:
void SetISBN( const FString & s )
{ISBN.Assign(s);};
void SetAuthor( const FString & s )
{author.Assign(s);};
FString GetISBN()
{return ISBN;};
FString GetAuthor()
{return author;};
private:
FString ISBN;
FString author;
};
10
Publication
int main()
{Book B;
B.SetPublisher( "Prentice Hall" );
B.SetDate( 970101L );
B.SetISBN( "0-02-359852-2" );
B.SetAuthor( "Irvine, Kip" );
cout << B.GetPublisher()<<endl
<<B.GetDate()<<endl
<<B.GetISBN().CString()<<endl
<<B.GetAuthor().CString()<<endl;
Magazine M;
M.SetIssuesPerYear( 12 );
M.SetCirculation( 500000L );
cout << M.GetIssuesPerYear()<<endl
<<M.GetCirculation()<<endl;
return 0;
}//ex6pub(Fstring.h, fstring.cpp, ex6pub.cpp)
11
Different Views of an
Employee
Full-time or part-time
Permanent or Temporary
How do you define its base class?
How td you define derived classes
based on this base class?
12
Declaring Derived Classes
Class class_name: access_specifieropt
or base_class { Member_list
}
access_specifier ::=
public|protected|private(default)
Equivalent to :
Subclass ::=<Id, SupeId, Ds, Ops, Intfc>
13
3D Point
 class Point {
 public:
 Point();
 Point( int xv, int yv );
 void SetX( int xv );
 void SetY( int yv );
 private:
 int x;
 int y;
 };
14
3D Point
class Point3D :public Point {
public:
 Point3D();
 Point3D( int xv, int yv, int zv );
 void SetZ( int zv );
private:
 int z;
};
15
3D Point
 int main()
 {
 Point3D P;
 P.SetX( 100 );
 P.SetY( 200 );
 P.SetZ( 300 );
 return 0;
 }
Point3D::Point3D( int xv, int yv, int zv )
{ SetX( xv );
 SetY( yv );
 SetZ( zv );
}
16
Order of Constructor and
Destructor Execution
Base class constructors are always
executed first.
Destructors are executed in exactly
the reverse order of constructors
The following example, shows you the
ordering of constructors.
17
Example
Class Employee{
Public:
Employee();
//…
};
Class SalariedEmployee:public Employee{
Public:
SalariedEmployee();
//…
};
Class ManagementEmployee:public SalariedEmployee{
Public:
ManagementEmployee();
//…
};
ManagementEmployee M;
18
Example
Coordinate
Point
Point3D
Shape
Sphere
1
2
1
19
Example
 #include <iostream.h>
 class Coordinate {
 public:
 Coordinate() { cout << "Coordinate,"; }
 ~Coordinate() { cout << “~Coordinate,"; }
 };
 class Point {
 public:
 Point() { cout << "Point,"; }
 ~Point() { cout << “~Point,"; }
 private:
 Coordinate x;
 Coordinate y;
 };
20
Example
 class Point3D :public Point {
 public:
 Point3D() { cout << "Point3D,"; }
 ~Point3D() { cout << “~Point3D,"; }
 private:
 Coordinate z;
 };
 class Shape {
 public:
 Shape() { cout << "Shape,"; }
 ~Shape() { cout << “~Shape,"; }
 };
21
Example
 class Sphere :public Shape {
 public:
 Sphere() { cout << "Sphere"; }
 ~Sphere() { cout << "Sphere"; }
 private:
 Point3D center;
 unsigned radius;
 };
 int main()
 { Sphere S;
 return 0;
 } //See Ex6-1.cpp
22
23
Overriding
A function in the derived class with
the same function name will override
the function’s variables in the base
class.
You can still retrieve the overridden
functions variables by using the scope
resolution operator ”::”.
24
Overriding
#include <iostream.h>
#include <stdlib.h>
class A
{ int i;
public:
A(){i = 5;};
int get(){return i;};
};
class B: public A
{ int i;
public:
B(){i = 10;};
int get(){return i;};
};
void main()
{ B b;
int x;
cout << b.get()<<endl;
cout << b.A::get()<<endl;
cout << sizeof(x)<<endl;
cout << sizeof(b)<<endl;
}//ex7overriding.cpp
25
Types of Class Members
private
protected
public
26
not
accessible
Private
Public
Protected
Accessible
to derived
classes only
Derived
Class
Accessible to derived
classes and the
instances
Types of Class Members
?
27
Types of Inheritance
public
private
protected
28
Public Inheritance
Public and protected members of the
base class become respectively
public and protected members of the
derived class.
29
Private
Public
Protected
Private
Public
Protected
public
Functions(instances)
Class
30
Example
#include <iostream.h>
#include <assert.h>
class Item {
Item * ptr;
int data;
public:
Item(){data = 0; ptr = NULL;};
Item(int i){data = i; ptr = NULL;
cout <<"Item::Item"<<i <<endl;
};
31
Example
 void setItem(int i){data = i;
 cout <<"Item::setItem"<<i <<endl;
 };
 void setPtr(Item * i){ptr = i;
 cout <<"Item::setPtr"<<endl;
 };
 int getData(){return data;};
 Item * getPtr(){return ptr;};
 };
32
Example
class List {
Item * head, *first, *last;
public:
List(){ head = NULL;
first = head;
last = head; }
Item * RemoveLast();
Item * RemoveFirst();
void PutFirst( Item * I );
void PutLast( Item * I );
protected:
int IsEmpty() const
{return (head==NULL);};
};
33
Example
 Item * List::RemoveFirst()
 { Item * temp;
 temp = first;
 first = first -> getPtr();
 cout <<"List:: RemoveFirst()"<<endl;
 return temp;
 };
 Item * List::RemoveLast()
 { Item * temp;
 temp = last;
 last = last -> getPtr();
 cout <<"List:: RemoveLast()"<<endl;
 return temp;
 };
34
Example
 void List::PutFirst(Item * I)
 { I->setPtr(first);
 first = I;
 cout <<"List::PutFirst"<<I->getData() <<endl;
 };
 void List::PutLast(Item * I)
 { I->setPtr(last);
 first = I;
 };
35
Example
 class Stack :public List {
 public:
 void Push( Item * I );
 Item * Pop();
 };
 void Stack::Push( Item * I )
 {PutFirst( I );
 cout <<"Stack::Push"<<I->getData() <<endl;
 }
 Item * Stack::Pop()
 {cout <<"Stack::Pop()"<<endl;
 return RemoveFirst();
 }
36
Example
 int main()
 {Item anItem(50), *p;
 Stack aStack;
 aStack.Push( &anItem );
 p = aStack.Pop();
 cout <<"aStack.Pop"<< p->getData()<<endl<<endl;
 anItem.setItem(100);
 aStack.Push( &anItem );
 p = aStack.RemoveFirst();
 cout <<"aStack.RemoveFirst"<< p-
>getData()<<endl<<endl;
 return 0;
 }//ex6-2.cpp
37
38
39
Private Inheritance
Public and protected members of the
base class become private members
of the derived class.
40
Private
Public
Protected
Private
Public
Protected
private
Function(instances) Class
41
Example
 class Queue :private List {
 public:
 void Enqueue( Item * I );
 Item * Serve();
 };
 void Queue::Enqueue( Item * I )
 { List::PutFirst( I );
 cout <<"Queue::Enqueue"<<I->getData() <<endl;
 }
 Item * Queue::Serve()
 {cout <<"Queue::Serve"<<endl;
 return List::RemoveFirst();
 }
42
Example
 int main()
 {Item anItem(50), *p;
 Queue aQueue;
 anItem.setItem(60);
 aQueue.Enqueue(&anItem);
 p = aQueue.Serve();
 cout <<"aQueue.Serve"<< p-
>getData()<<endl<<endl;
 anItem.setItem(600);
 aQueue.Enqueue(&anItem);
 p =aQueue.RemoveFirst(); //Unaccessible
 //cout <<"aQueue.RemoveFirst"<< p-
>getData()<<endl;
 return 0;
 }//ex6-3.cpp
43
44
Protected Inheritance
Public and protected members of the
base class become protected
members of the derived class.
45
Private
Public
Protected
Private
Public
Protected
protected
Function(instances) Class
46
Example
 class Stack1 :protected List {
 public:
 void Push( Item * I );
 Item * Pop();
 };
 void Stack1::Push( Item * I )
 {PutFirst( I );
 cout <<"Stack1::Push"<<I->getData() <<endl;
 }
 Item * Stack1::Pop()
 {cout <<"Stack1::Pop()"<<endl;
 return RemoveFirst();
 }
47
Example
 int main()
 {Item anItem(50), *p;
 Stack1 aStack1;
 aStack1.Push( &anItem );
 p = aStack1.Pop();
 cout <<"aStack1.Pop"<< p->getData()<<endl<<endl;
 anItem.setItem(100);
 aStack1.Push( &anItem );
 p = aStack1.RemoveFirst();//Unaccessible!
 cout <<"aStack1.RemoveFirst"<< p->getData()
<<endl<<endl;
 return 0;
 }
48
49
The accessibility of inherited
members in a derived class
derived
member
Public Protected Private
Public X X
Protected X X
Private
50
The accessibility of inherited
members for an instance
Private
Protected
X
Public
Private
Protected
Public
derived
member
51
Constructor- Initializers
Point3D::Point3D(param-list): ctor-
initializer
{// function body
}
ctor-initializer is actually used to
transfer the parameters to the
constructors of the base-class
52
Publication
 #include <iostream.h>
 #include <string.h>
 class Date
 {
 public:
 Date( int mo, int dy, int yr )
 {
 month = mo; day = dy; year = yr;
 cout << "Date constructorn";
 }
 // Ex6pub2.cpp
53
Publication
 Date( Date & D )
 {
 month = D.month; day = D.day; year = D.year;
 cout << "Date copy constructorn";
 }
 ~Date() { cout << "Date destructorn"; }
 private:
 int year;
 int month;
 int day;
 };
54
Publication
 class Publication
 {
 public:
 Publication( char * publshr, Date & aDate )
 : pubDate( aDate )
 {
 strcpy( publisher, publshr);
 cout << "Publication constructorn";
 }
 ~Publication()
 { cout << "Publication destructorn"; }
 private:
 char publisher[30];
 Date pubDate;
 };
55
Publication
 class Magazine :public Publication
 {public:
 Magazine( char * publshr, Date & aDate,
 int issues ): Publication( publshr, aDate )
 {issPYear = issues;
 cout << "Magazine constructorn";
 }
 ~Magazine() { "Magazine destructorn"; }
 private:
 int issPYear; // issues per year
 };
 int main()
 { Magazine aMag( "Zipp", Date(10, 1, 95), 12 );
 return 0;
 }
56
57
Why use the
constructor-initializer?
Without it, the default constructor for
the base class would be called, which
would then have to be followed by
calls to access functions to set
specific data members.
 A constructor initailizer is therefore
more efficient.
58
Constructors in Derived
Classes
When an object of a derived class is
created, the constructor of the object
must first explicitly call the constructor
of the base class.
This is the same as constructor-
initializer.
59
Destructor Function
Destructors are called implicitly
starting with the last derived class and
moving in the direction of the base
class.
60
Compatibility Between
Base and Derived Classes
An object of a derived class can be
treated as an object of its base class.
The reverse is not true.
61
Nested Class Scope
A public or protected base class
member that is hidden from the derived
class can be accessed using the scope
resolution operator ” ::”
For example: base-class::member
The “that” of base class can not access
the members of its derived classes.
62
Example
Class Parent {
public:
void Print() const;
//…
}
class Child: public Parent {
public:
void Print() const{
Parent::print();// scope resolution !!!
cout <<age<<‘n’
<<school<<‘n’;
}
private;
int age;
Fstring school;
}
63
Error
Void Parent::Print() {
cout<<name <<‘n’
<<Child::age <<‘n’;
//base can not access that of derived
}
64
Implicit Conversion of Derived
Pointers to Base Pointers
A base type pointer can point to either
a base object or a derived object.
Point3D center;// Point3D is derived from Point
Point * p = &center;
Point3D *cp = new Point3D;
Point *p;
p = cp;
65
Example
 class FString { };
 class Student {
 public: //...
 private:
 long id;
 };
 class GraduateStudent :public Student {
 public: //...
 private:
 FString thesisTitle;
 };
66
Example
 void CalcTuition( Student & sp )
 { /* sp is a Student or a derived object */}
 void RecordThesis( GraduateStudent * p )
 { /*... */ }
 int main()
 {Student * sp;
 GraduateStudent * gp; //...
 sp = gp; //...
 RecordThesis( (GraduateStudent *) sp );
 return 0;
 }
67
Casting Base Pointers to
Derived Pointers
A base pointer can not be implicitly
converted to a derived pointer.
This conversion is risky, because the
derived object can contain more than
the base object.
68
Example
void DoSomething(const GraduateStudent *
GS)
{ cout << GS->GetThesisTitle();}
Student * sp = new Student;
DoSomething(sp);
DoSomething(GraduateStudent (sp));
Student * sp = new GraduateStudent;
DoSomething(GraduateStudent (sp));
//Error
69
Example
class Item {/* …*/}
class Student: public Item{/* …*/}
class Collection {
public:
void Append (const Item * ip);
Item * Get() const; }
const unsigned Count = 10;
Collection studentList;
Student * p;
for (int i=0; i<Count; i++ )
{p = new Student;
studentList.Append(p);}
p = (student*) studentList.Get();//explicit cast
70
Attention
 Forcing class users to use explicit casting
often leads poor code.
class studentCollection:public Collection{
public:
student * Get() const
{ return (student *) Collection::Get(); }
//…
}
studentCollection studentList;
student *p;
//…
p = studentList.Get(); // no cast required
71
Multiple Inheritance
Student
employee
GraduateAssistant
salaried
72
Example
 #include <iostream.h>
 #include "fstring.h"
 typedef unsigned long ulong;
 class Student {
 public:
 unsigned GetAge() const;
 ulong GetId() const;
 unsigned GetMajor() const;
 void SetAge( unsigned n );
 void SetId( ulong n );
 void SetMajor( unsigned n );
 private:
 ulong id;
 unsigned majorCode;
 unsigned degreeCode;
 float gpa;
 unsigned age;
 };
73
Example
 class Employee {
 public:
 unsigned GetAge() const;
 const FString & GetBenefits() const;
 unsigned GetExemptions() const;
 void SetAge( unsigned n );
 void SetBenefits( const FString & benef );
 void SetExemptions( unsigned n );
 private:
 unsigned age;
 unsigned exemptions;
 FString benefits;
 };
74
Example
 class Salaried :public Employee {
 public:
 float GetSalary() const;
 void SetSalary( float s );
 private:
 float salary;
 };
 class GradAssistant :public Student, public
Salaried {
 public:
 void Display() const;
 };
75
Example
 void GradAssistant::Display() const
 { cout << GetId() << ','
 << GetMajor() << ','
 << GetSalary() << ','
 << GetExemptions() << endl;
 }
 int main()
 { GradAssistant GA;
 GA.SetId(12345);
 // GA.SetAge(22); // error: ambiguous
 GA.Student::SetAge(22); // ok - specific
 GA.SetMajor(108);
 GA.SetExemptions(2);
 GA.SetSalary(10000);
 GA.Display();
 return 0;
 }//ex6mulin.cpp
76
77
78
79
Example
 Question:
 If we want to set a GradAssistant ‘s
age by calling SetAge(), which
SetAge() should we use use?
1. Direct solution: Student::SetAge() or
Salaried::SetAge().
2. Abstract(Virtual) base classes
80
Virtual base classes
Student
employee
Graduate Assistant
salaried
Person
81
Example
 #include <iostream.h>
 #include "fstring.h"
 typedef unsigned long ulong;
 class Person {
 public:
 unsigned GetAge() const;
 const FString & GetSocSecNum() const;
 void SetAge( unsigned n );
 void SetSocSecNum( const FString & ssn );
 private:
 unsigned age;
 FString socSecNum;
 };
82
Example
 class Student :public virtual Person {
 public:
 unsigned GetMajor() const;
 void SetMajor( unsigned n );
 private:
 unsigned majorCode;
 unsigned degreeCode;
 float gpa;
 };
83
Example
 class Employee :public virtual Person {
 public:
 const FString & GetBenefits() const;
 unsigned GetExemptions() const;
 void SetBenefits( const FString & benef );
 void SetExemptions( unsigned n );
 private:
 unsigned exemptions;
 FString benefits;
 };
84
Example
 class Salaried :public Employee {
 public:
 float GetSalary() const;
 void SetSalary( float s );
 private:
 float salary;
 };
 class GradAssistant :public Student, public
Salaried {
 public:
 void Display() const;
 };
85
Example
void GradAssistant::Display() const
{ cout << GetSocSecNum() << ','
<< GetAge() << ',‘
// ambiguous if not virtual
}
int main()
{ GradAssistant GA;
return 0;
}
86
87
Virtual Base Classes
 The function calls in
GradAssistant::Display() are ambiguous
unless Person is inherited as a virtual base
class.
Adding “virtual” lets the compiler
decide which function and which
variable should be accessed.
88
Virtual Base Classes
Student(8)
Employee(22)
GraduateAssistant(0)
Salaried(4)
Person(22)
aGA(78)
89
Virtual Base Classes(end)
Student(8)
Employee(22)
GraduateAssistant(0)
Salaried(4)
Person(22)
aGA(56)
virtual virtual
90
Readings
Readings
Chapter 6 Sections 6.1.3 – 6.5

More Related Content

Similar to Inheritance in C++.ppt (20)

Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
Lab3
Lab3Lab3
Lab3
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
Oops concept
Oops conceptOops concept
Oops concept
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Object oriented programming 2
Object oriented programming 2Object oriented programming 2
Object oriented programming 2
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and Objects
 
201801 CSE240 Lecture 14
201801 CSE240 Lecture 14201801 CSE240 Lecture 14
201801 CSE240 Lecture 14
 
L10
L10L10
L10
 
Unit 2
Unit 2Unit 2
Unit 2
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
C++ prgms 4th unit Inheritance
C++ prgms 4th unit InheritanceC++ prgms 4th unit Inheritance
C++ prgms 4th unit Inheritance
 
麻省理工C++公开教学课程(二)
麻省理工C++公开教学课程(二)麻省理工C++公开教学课程(二)
麻省理工C++公开教学课程(二)
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
 
Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)
 
class and objects
class and objectsclass and objects
class and objects
 
constructors and destructors
constructors and destructorsconstructors and destructors
constructors and destructors
 
Class and object C++.pptx
Class and object C++.pptxClass and object C++.pptx
Class and object C++.pptx
 
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining Classes
 

Recently uploaded

20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdfHuman37
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)jennyeacort
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]📊 Markus Baersch
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAbdelrhman abooda
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfSocial Samosa
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort servicejennyeacort
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改atducpo
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一F La
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998YohFuh
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130Suhani Kapoor
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfgstagge
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一F sss
 

Recently uploaded (20)

20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
 
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
 
Call Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort ServiceCall Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort Service
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdf
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
 

Inheritance in C++.ppt

  • 1. 1 Inheritance in C++ Lesson #6 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek.
  • 2. 2 Content Base and Derived Classes Single Inheritance Declaration of derived classes  Order of Constructor and Destructor Execution Inherited member accessibility Multiple Inheritance Virtual Base Classes
  • 3. 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes  A derived class inherits all the data and function members of a base class (in addition to its explicitly declared members.)
  • 4. 4 Single Inheritance Implement an “is-a” relationship The derived class only has one base class.
  • 5. 5 Example 1: Student • name • id • major Undergraduate • year • minor • etc. Graduate • advisor • thesis • research • etc...
  • 6. 6 Example 2: Publication: • publisher • date Magazine: • # of issues per year • circulation Book: • ISBN • author
  • 7. 7 Example: Publication #include “FString.h" class Publication { public: void SetPublisher( const FString & p ) {publisher.Assign(p);}; void SetDate( unsigned long dt ) {date = dt;}; FString GetPublisher(){return publisher;}; unsigned long GetDate(){return date;}; private: FString publisher; unsigned long date; };
  • 8. 8 Publication class Magazine :public Publication { public: void SetIssuesPerYear( unsigned n ){issuesPerYear=n;}; void SetCirculation( unsigned long n ){ circulation=n;}; unsigned GetIssuesPerYear(){return issuesPerYear;}; unsigned long GetCirculation(){return circulation;}; private: unsigned issuesPerYear; unsigned long circulation; };
  • 9. 9 Publication class Book :public Publication { public: void SetISBN( const FString & s ) {ISBN.Assign(s);}; void SetAuthor( const FString & s ) {author.Assign(s);}; FString GetISBN() {return ISBN;}; FString GetAuthor() {return author;}; private: FString ISBN; FString author; };
  • 10. 10 Publication int main() {Book B; B.SetPublisher( "Prentice Hall" ); B.SetDate( 970101L ); B.SetISBN( "0-02-359852-2" ); B.SetAuthor( "Irvine, Kip" ); cout << B.GetPublisher()<<endl <<B.GetDate()<<endl <<B.GetISBN().CString()<<endl <<B.GetAuthor().CString()<<endl; Magazine M; M.SetIssuesPerYear( 12 ); M.SetCirculation( 500000L ); cout << M.GetIssuesPerYear()<<endl <<M.GetCirculation()<<endl; return 0; }//ex6pub(Fstring.h, fstring.cpp, ex6pub.cpp)
  • 11. 11 Different Views of an Employee Full-time or part-time Permanent or Temporary How do you define its base class? How td you define derived classes based on this base class?
  • 12. 12 Declaring Derived Classes Class class_name: access_specifieropt or base_class { Member_list } access_specifier ::= public|protected|private(default) Equivalent to : Subclass ::=<Id, SupeId, Ds, Ops, Intfc>
  • 13. 13 3D Point  class Point {  public:  Point();  Point( int xv, int yv );  void SetX( int xv );  void SetY( int yv );  private:  int x;  int y;  };
  • 14. 14 3D Point class Point3D :public Point { public:  Point3D();  Point3D( int xv, int yv, int zv );  void SetZ( int zv ); private:  int z; };
  • 15. 15 3D Point  int main()  {  Point3D P;  P.SetX( 100 );  P.SetY( 200 );  P.SetZ( 300 );  return 0;  } Point3D::Point3D( int xv, int yv, int zv ) { SetX( xv );  SetY( yv );  SetZ( zv ); }
  • 16. 16 Order of Constructor and Destructor Execution Base class constructors are always executed first. Destructors are executed in exactly the reverse order of constructors The following example, shows you the ordering of constructors.
  • 17. 17 Example Class Employee{ Public: Employee(); //… }; Class SalariedEmployee:public Employee{ Public: SalariedEmployee(); //… }; Class ManagementEmployee:public SalariedEmployee{ Public: ManagementEmployee(); //… }; ManagementEmployee M;
  • 19. 19 Example  #include <iostream.h>  class Coordinate {  public:  Coordinate() { cout << "Coordinate,"; }  ~Coordinate() { cout << “~Coordinate,"; }  };  class Point {  public:  Point() { cout << "Point,"; }  ~Point() { cout << “~Point,"; }  private:  Coordinate x;  Coordinate y;  };
  • 20. 20 Example  class Point3D :public Point {  public:  Point3D() { cout << "Point3D,"; }  ~Point3D() { cout << “~Point3D,"; }  private:  Coordinate z;  };  class Shape {  public:  Shape() { cout << "Shape,"; }  ~Shape() { cout << “~Shape,"; }  };
  • 21. 21 Example  class Sphere :public Shape {  public:  Sphere() { cout << "Sphere"; }  ~Sphere() { cout << "Sphere"; }  private:  Point3D center;  unsigned radius;  };  int main()  { Sphere S;  return 0;  } //See Ex6-1.cpp
  • 22. 22
  • 23. 23 Overriding A function in the derived class with the same function name will override the function’s variables in the base class. You can still retrieve the overridden functions variables by using the scope resolution operator ”::”.
  • 24. 24 Overriding #include <iostream.h> #include <stdlib.h> class A { int i; public: A(){i = 5;}; int get(){return i;}; }; class B: public A { int i; public: B(){i = 10;}; int get(){return i;}; }; void main() { B b; int x; cout << b.get()<<endl; cout << b.A::get()<<endl; cout << sizeof(x)<<endl; cout << sizeof(b)<<endl; }//ex7overriding.cpp
  • 25. 25 Types of Class Members private protected public
  • 28. 28 Public Inheritance Public and protected members of the base class become respectively public and protected members of the derived class.
  • 30. 30 Example #include <iostream.h> #include <assert.h> class Item { Item * ptr; int data; public: Item(){data = 0; ptr = NULL;}; Item(int i){data = i; ptr = NULL; cout <<"Item::Item"<<i <<endl; };
  • 31. 31 Example  void setItem(int i){data = i;  cout <<"Item::setItem"<<i <<endl;  };  void setPtr(Item * i){ptr = i;  cout <<"Item::setPtr"<<endl;  };  int getData(){return data;};  Item * getPtr(){return ptr;};  };
  • 32. 32 Example class List { Item * head, *first, *last; public: List(){ head = NULL; first = head; last = head; } Item * RemoveLast(); Item * RemoveFirst(); void PutFirst( Item * I ); void PutLast( Item * I ); protected: int IsEmpty() const {return (head==NULL);}; };
  • 33. 33 Example  Item * List::RemoveFirst()  { Item * temp;  temp = first;  first = first -> getPtr();  cout <<"List:: RemoveFirst()"<<endl;  return temp;  };  Item * List::RemoveLast()  { Item * temp;  temp = last;  last = last -> getPtr();  cout <<"List:: RemoveLast()"<<endl;  return temp;  };
  • 34. 34 Example  void List::PutFirst(Item * I)  { I->setPtr(first);  first = I;  cout <<"List::PutFirst"<<I->getData() <<endl;  };  void List::PutLast(Item * I)  { I->setPtr(last);  first = I;  };
  • 35. 35 Example  class Stack :public List {  public:  void Push( Item * I );  Item * Pop();  };  void Stack::Push( Item * I )  {PutFirst( I );  cout <<"Stack::Push"<<I->getData() <<endl;  }  Item * Stack::Pop()  {cout <<"Stack::Pop()"<<endl;  return RemoveFirst();  }
  • 36. 36 Example  int main()  {Item anItem(50), *p;  Stack aStack;  aStack.Push( &anItem );  p = aStack.Pop();  cout <<"aStack.Pop"<< p->getData()<<endl<<endl;  anItem.setItem(100);  aStack.Push( &anItem );  p = aStack.RemoveFirst();  cout <<"aStack.RemoveFirst"<< p- >getData()<<endl<<endl;  return 0;  }//ex6-2.cpp
  • 37. 37
  • 38. 38
  • 39. 39 Private Inheritance Public and protected members of the base class become private members of the derived class.
  • 41. 41 Example  class Queue :private List {  public:  void Enqueue( Item * I );  Item * Serve();  };  void Queue::Enqueue( Item * I )  { List::PutFirst( I );  cout <<"Queue::Enqueue"<<I->getData() <<endl;  }  Item * Queue::Serve()  {cout <<"Queue::Serve"<<endl;  return List::RemoveFirst();  }
  • 42. 42 Example  int main()  {Item anItem(50), *p;  Queue aQueue;  anItem.setItem(60);  aQueue.Enqueue(&anItem);  p = aQueue.Serve();  cout <<"aQueue.Serve"<< p- >getData()<<endl<<endl;  anItem.setItem(600);  aQueue.Enqueue(&anItem);  p =aQueue.RemoveFirst(); //Unaccessible  //cout <<"aQueue.RemoveFirst"<< p- >getData()<<endl;  return 0;  }//ex6-3.cpp
  • 43. 43
  • 44. 44 Protected Inheritance Public and protected members of the base class become protected members of the derived class.
  • 46. 46 Example  class Stack1 :protected List {  public:  void Push( Item * I );  Item * Pop();  };  void Stack1::Push( Item * I )  {PutFirst( I );  cout <<"Stack1::Push"<<I->getData() <<endl;  }  Item * Stack1::Pop()  {cout <<"Stack1::Pop()"<<endl;  return RemoveFirst();  }
  • 47. 47 Example  int main()  {Item anItem(50), *p;  Stack1 aStack1;  aStack1.Push( &anItem );  p = aStack1.Pop();  cout <<"aStack1.Pop"<< p->getData()<<endl<<endl;  anItem.setItem(100);  aStack1.Push( &anItem );  p = aStack1.RemoveFirst();//Unaccessible!  cout <<"aStack1.RemoveFirst"<< p->getData() <<endl<<endl;  return 0;  }
  • 48. 48
  • 49. 49 The accessibility of inherited members in a derived class derived member Public Protected Private Public X X Protected X X Private
  • 50. 50 The accessibility of inherited members for an instance Private Protected X Public Private Protected Public derived member
  • 51. 51 Constructor- Initializers Point3D::Point3D(param-list): ctor- initializer {// function body } ctor-initializer is actually used to transfer the parameters to the constructors of the base-class
  • 52. 52 Publication  #include <iostream.h>  #include <string.h>  class Date  {  public:  Date( int mo, int dy, int yr )  {  month = mo; day = dy; year = yr;  cout << "Date constructorn";  }  // Ex6pub2.cpp
  • 53. 53 Publication  Date( Date & D )  {  month = D.month; day = D.day; year = D.year;  cout << "Date copy constructorn";  }  ~Date() { cout << "Date destructorn"; }  private:  int year;  int month;  int day;  };
  • 54. 54 Publication  class Publication  {  public:  Publication( char * publshr, Date & aDate )  : pubDate( aDate )  {  strcpy( publisher, publshr);  cout << "Publication constructorn";  }  ~Publication()  { cout << "Publication destructorn"; }  private:  char publisher[30];  Date pubDate;  };
  • 55. 55 Publication  class Magazine :public Publication  {public:  Magazine( char * publshr, Date & aDate,  int issues ): Publication( publshr, aDate )  {issPYear = issues;  cout << "Magazine constructorn";  }  ~Magazine() { "Magazine destructorn"; }  private:  int issPYear; // issues per year  };  int main()  { Magazine aMag( "Zipp", Date(10, 1, 95), 12 );  return 0;  }
  • 56. 56
  • 57. 57 Why use the constructor-initializer? Without it, the default constructor for the base class would be called, which would then have to be followed by calls to access functions to set specific data members.  A constructor initailizer is therefore more efficient.
  • 58. 58 Constructors in Derived Classes When an object of a derived class is created, the constructor of the object must first explicitly call the constructor of the base class. This is the same as constructor- initializer.
  • 59. 59 Destructor Function Destructors are called implicitly starting with the last derived class and moving in the direction of the base class.
  • 60. 60 Compatibility Between Base and Derived Classes An object of a derived class can be treated as an object of its base class. The reverse is not true.
  • 61. 61 Nested Class Scope A public or protected base class member that is hidden from the derived class can be accessed using the scope resolution operator ” ::” For example: base-class::member The “that” of base class can not access the members of its derived classes.
  • 62. 62 Example Class Parent { public: void Print() const; //… } class Child: public Parent { public: void Print() const{ Parent::print();// scope resolution !!! cout <<age<<‘n’ <<school<<‘n’; } private; int age; Fstring school; }
  • 63. 63 Error Void Parent::Print() { cout<<name <<‘n’ <<Child::age <<‘n’; //base can not access that of derived }
  • 64. 64 Implicit Conversion of Derived Pointers to Base Pointers A base type pointer can point to either a base object or a derived object. Point3D center;// Point3D is derived from Point Point * p = &center; Point3D *cp = new Point3D; Point *p; p = cp;
  • 65. 65 Example  class FString { };  class Student {  public: //...  private:  long id;  };  class GraduateStudent :public Student {  public: //...  private:  FString thesisTitle;  };
  • 66. 66 Example  void CalcTuition( Student & sp )  { /* sp is a Student or a derived object */}  void RecordThesis( GraduateStudent * p )  { /*... */ }  int main()  {Student * sp;  GraduateStudent * gp; //...  sp = gp; //...  RecordThesis( (GraduateStudent *) sp );  return 0;  }
  • 67. 67 Casting Base Pointers to Derived Pointers A base pointer can not be implicitly converted to a derived pointer. This conversion is risky, because the derived object can contain more than the base object.
  • 68. 68 Example void DoSomething(const GraduateStudent * GS) { cout << GS->GetThesisTitle();} Student * sp = new Student; DoSomething(sp); DoSomething(GraduateStudent (sp)); Student * sp = new GraduateStudent; DoSomething(GraduateStudent (sp)); //Error
  • 69. 69 Example class Item {/* …*/} class Student: public Item{/* …*/} class Collection { public: void Append (const Item * ip); Item * Get() const; } const unsigned Count = 10; Collection studentList; Student * p; for (int i=0; i<Count; i++ ) {p = new Student; studentList.Append(p);} p = (student*) studentList.Get();//explicit cast
  • 70. 70 Attention  Forcing class users to use explicit casting often leads poor code. class studentCollection:public Collection{ public: student * Get() const { return (student *) Collection::Get(); } //… } studentCollection studentList; student *p; //… p = studentList.Get(); // no cast required
  • 72. 72 Example  #include <iostream.h>  #include "fstring.h"  typedef unsigned long ulong;  class Student {  public:  unsigned GetAge() const;  ulong GetId() const;  unsigned GetMajor() const;  void SetAge( unsigned n );  void SetId( ulong n );  void SetMajor( unsigned n );  private:  ulong id;  unsigned majorCode;  unsigned degreeCode;  float gpa;  unsigned age;  };
  • 73. 73 Example  class Employee {  public:  unsigned GetAge() const;  const FString & GetBenefits() const;  unsigned GetExemptions() const;  void SetAge( unsigned n );  void SetBenefits( const FString & benef );  void SetExemptions( unsigned n );  private:  unsigned age;  unsigned exemptions;  FString benefits;  };
  • 74. 74 Example  class Salaried :public Employee {  public:  float GetSalary() const;  void SetSalary( float s );  private:  float salary;  };  class GradAssistant :public Student, public Salaried {  public:  void Display() const;  };
  • 75. 75 Example  void GradAssistant::Display() const  { cout << GetId() << ','  << GetMajor() << ','  << GetSalary() << ','  << GetExemptions() << endl;  }  int main()  { GradAssistant GA;  GA.SetId(12345);  // GA.SetAge(22); // error: ambiguous  GA.Student::SetAge(22); // ok - specific  GA.SetMajor(108);  GA.SetExemptions(2);  GA.SetSalary(10000);  GA.Display();  return 0;  }//ex6mulin.cpp
  • 76. 76
  • 77. 77
  • 78. 78
  • 79. 79 Example  Question:  If we want to set a GradAssistant ‘s age by calling SetAge(), which SetAge() should we use use? 1. Direct solution: Student::SetAge() or Salaried::SetAge(). 2. Abstract(Virtual) base classes
  • 81. 81 Example  #include <iostream.h>  #include "fstring.h"  typedef unsigned long ulong;  class Person {  public:  unsigned GetAge() const;  const FString & GetSocSecNum() const;  void SetAge( unsigned n );  void SetSocSecNum( const FString & ssn );  private:  unsigned age;  FString socSecNum;  };
  • 82. 82 Example  class Student :public virtual Person {  public:  unsigned GetMajor() const;  void SetMajor( unsigned n );  private:  unsigned majorCode;  unsigned degreeCode;  float gpa;  };
  • 83. 83 Example  class Employee :public virtual Person {  public:  const FString & GetBenefits() const;  unsigned GetExemptions() const;  void SetBenefits( const FString & benef );  void SetExemptions( unsigned n );  private:  unsigned exemptions;  FString benefits;  };
  • 84. 84 Example  class Salaried :public Employee {  public:  float GetSalary() const;  void SetSalary( float s );  private:  float salary;  };  class GradAssistant :public Student, public Salaried {  public:  void Display() const;  };
  • 85. 85 Example void GradAssistant::Display() const { cout << GetSocSecNum() << ',' << GetAge() << ',‘ // ambiguous if not virtual } int main() { GradAssistant GA; return 0; }
  • 86. 86
  • 87. 87 Virtual Base Classes  The function calls in GradAssistant::Display() are ambiguous unless Person is inherited as a virtual base class. Adding “virtual” lets the compiler decide which function and which variable should be accessed.