Tutorial C++

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Module 24

Partha Pratim
Das
Module 24: Programming in C++
Objectives &
Outline
Inheritance: Part 4
Example –
Phone
Hierarchy
Partha Pratim Das
Summary

Department of Computer Science and Engineering


Indian Institute of Technology, Kharagpur
[email protected]

Tanwi Mallick
Srijoni Majumdar
Himadri B G S Bhuyan

NPTEL MOOCs Programming in C++ Partha Pratim Das 1


Module Objectives

Module 24

Partha Pratim Model a hierarchy of phones using inheritance


Das

Objectives &
Outline

Example –
Phone
Hierarchy

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 2


Module Outline

Module 24

Partha Pratim ISA Relationship


Das
Inheritance in C++
Objectives &
Outline
Semantics
Example –
Data Members and Object Layout
Phone Member Functions
Hierarchy
Overriding
Summary
Overloading
protected Access
Constructor & Destructor
Object Lifetime
Example – Phone Hierarchy
Inheritance in C++ (private)
Implemented-As Semantics

NPTEL MOOCs Programming in C++ Partha Pratim Das 3


Phone Hierarchy

Module 24 Let us model a hierarchy of phones comprising:


Partha Pratim Land line Phone
Das Mobile Phone
Objectives &
Smart Phone
Outline
We model Helper classes
Example –
Phone
Hierarchy
We model each phone separately
Summary We model the phone hierarchy

NPTEL MOOCs Programming in C++ Partha Pratim Das 4


Helper Classes

Module 24

Partha Pratim
Das
Class Description
Objectives &
Outline class PhoneNumber 12-digit phone number
Example – class Name Subscriber Name (as string)
Phone
Hierarchy class Photo Image & Subscriber Name as alt text
Summary class RingTone Audio & ring tone name
class Contact PhoneNumber, Name, and Photo (op-
tional) of a contact
class AddressBook List of contacts

NPTEL MOOCs Programming in C++ Partha Pratim Das 5


Land line Phone Model
Landline Phone class LandlinePhone {
Module 24 PhoneNumber number_;
Call: By dial / keyboard Name subscriber_;
Partha Pratim Answer RingTone rTone_;
Das
public:
Objectives & LandlinePhone(const char *num,
Outline const char *subs);

Example – void Call(const PhoneNumber *p);


Phone
Hierarchy void Answer();

Summary friend ostream& operator<<(ostream& os,


const LandlinePhone& p);
};

NPTEL MOOCs Programming in C++ Partha Pratim Das 6


Mobile Phone Model
Mobile Phone class MobilePhone {
Module 24 PhoneNumber number_;
Call: By keyboard – Name subscriber_;
Partha Pratim RingTone rTone_;
Das shows number AddressBook aBook_;
By Number PhoneNumber *lastDial_;
void SetLastDialed(const PhoneNumber& p);
Objectives & By Name void ShowNumber();
Outline
Answer
Example – public:
Redial MobilePhone(const char *num,
Phone
Hierarchy Set Ring Tone const char *subs);

Summary
Add Contact void Call(PhoneNumber *p);
Number void Call(const Name& n);
Name void Answer();

void ReDial();
void SetRingTone(RingTone::RINGTONE r);
void AddContact(const char *num = 0,
const char *subs = 0);

friend ostream& operator<<(ostream& os,


const MobilePhone& p);
};

NPTEL MOOCs Programming in C++ Partha Pratim Das 7


Smart Phone Model

Smart Phone class SmartPhone {


Module 24 PhoneNumber number_;
Call: By touchscreen – Name subscriber_;
Partha Pratim RingTone rTone_;
Das shows number & photo AddressBook aBook_;
PhoneNumber *lastDial_;
By Number void SetLastDialed(const PhoneNumber& p);
Objectives & By Name void ShowNumber();
Outline
unsigned int size_;
Answer void DisplayPhoto();
Example –
Phone
Redial
Hierarchy Set Ring Tone public:
SmartPhone(const char *num,
Summary Add Contact const char *subs);
Number
void Call(PhoneNumber *p);
Name void Call(const Name& n);
Photo
void Answer();

void ReDial();
void SetRingTone(RingTone::RINGTONE r);
void AddContact(const char *num = 0,
const char *subs = 0);

friend ostream& operator<<(ostream& os,


const MobilePhone& p);
};

NPTEL MOOCs Programming in C++ Partha Pratim Das 8


Comparison of Phones
Landline Phone Smart Phone
Module 24
Call: By dial / keyboard Call: By touchscreen –
Partha Pratim Answer
Das shows number & photo
Mobile Phone By Number
Objectives &
Outline Call: By keyboard – By Name
shows number Answer
Example –
Phone By Number Redial
Hierarchy
By Name Set Ring Tone
Summary Answer Add Contact
Redial Number
Set Ring Tone Name
Add Contact Photo
Number
Name
• There exists a substantial overlap between the functionality of the phones
• A mobile phone is more capable than a land line phone and can perform (almost)
all its functions
• A smart phone is more capable than a mobile phone and can perform (almost)
all its functions
• These phones belong to a Specialization / Generalization hierarchy
NPTEL MOOCs Programming in C++ Partha Pratim Das 9
Hierarchy of Phones

Module 24

Partha Pratim
Das

Objectives &
Outline

Example –
Phone
Hierarchy MobilePhone ISA LandlinePhone
Summary LandlinePhone is generalization
MobilePhone is specialization
MobilePhone inherits the properties of LandlinePhone
SmartPhone ISA MobilePhone
MobilePhone is generalization
SmartPhone is specialization
SmartPhone inherits the properties of MobilePhone
ISA is transitive

NPTEL MOOCs Programming in C++ Partha Pratim Das 10


Compare LandlinePhone and MobilePhone

Module 24 class LandlinePhone { class MobilePhone {


PhoneNumber number_; PhoneNumber number_;
Partha Pratim Name subscriber_; Name subscriber_;
Das RingTone rTone_; RingTone rTone_;
AddressBook aBook_;
PhoneNumber *lastDial_;
Objectives & void SetLastDialed(const PhoneNumber& p);
Outline void ShowNumber();
public: public:
Example –
LandlinePhone(const char *num, MobilePhone(const char *num,
Phone
const char *subs); const char *subs);
Hierarchy
void Call(const PhoneNumber *p); void Call(PhoneNumber *p);
Summary void Call(const Name& n);
void ReDial();
void Answer(); void Answer();
void SetRingTone(RingTone::RINGTONE r);
void AddContact(const char *num = 0,
const char *subs = 0);

friend ostream& operator<<(ostream& os, friend ostream& operator<<(ostream& os,


const LandlinePhone& p); const MobilePhone& p);
}; };

NPTEL MOOCs Programming in C++ Partha Pratim Das 11


Hierarchy of Phones

Module 24

Partha Pratim
Das

Objectives &
Outline

Example –
Phone
Hierarchy

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 12


MobilePhone ISA LandlinePhone

Module 24 Base Class Derived Class


class LandlinePhone { class MobilePhone : public LandlinePhone {
Partha Pratim protected: protected:
Das PhoneNumber number_; //PhoneNumber number_;
Name subscriber_; //Name subscriber_;
RingTone rTone_; //RingTone rTone_;
Objectives & AddressBook aBook_;
Outline PhoneNumber *lastDial_;
void SetLastDialed(const PhoneNumber& p);
Example –
void ShowNumber();
Phone
public: public:
Hierarchy
LandlinePhone(const char *num, MobilePhone(const char *num,
Summary const char *subs) : const char *subs) :
number_(num), subscriber_(subs), LandlinePhone(num, subs), // Base ctor
rTone_() {} lastDial_(0) {}

void Call(const PhoneNumber *p); void Call(const PhoneNumber *p); // Override


void Call(const Name& n); // Overload
void Answer(); //void Answer();
void ReDial();
void SetRingTone(RingTone::RINGTONE r);
void AddContact(const char *num = 0,
const char *subs = 0);

friend ostream& operator<<(ostream& os, friend ostream& operator<< (ostream& os,


const LandlinePhone& p); const MobilePhone& p);
}; };

NPTEL MOOCs Programming in C++ Partha Pratim Das 13


SmartPhone ISA MobilePhone

Module 24 Base Class Derived Class


class MobilePhone : public LandlinePhone { class SmartPhone : public MobilePhone {
Partha Pratim protected: protected:
Das //PhoneNumber number_; //PhoneNumber number_;
//Name subscriber_; //Name subscriber_;
//RingTone rTone_; //RingTone rTone_;
Objectives & AddressBook aBook_; //AddressBook aBook_;
Outline PhoneNumber *lastDial_; //PhoneNumber *lastDial_;
void SetLastDialed(const PhoneNumber& p); //void SetLastDialed(const PhoneNumber& p);
Example –
void ShowNumber(); //void ShowNumber();
Phone
unsigned int size_;
Hierarchy
void DisplayPhoto()
Summary public: public:
MobilePhone(const char *num, SmartPhone(const char *num,
const char *subs) : const char *subs) :
LandlinePhone(num, subs), // Base ctor MobilePhone(num, subs), // Base ctor
lastDial_(0) {} lastDial_(0) {}

void Call(const PhoneNumber *p); // Override void Call(const PhoneNumber *p); // Override
void Call(const Name& n); // Overload void Call(const Name& n); // Override
//void Answer(); //void Answer();
void ReDial(); void ReDial(); // Override
void SetRingTone(RingTone::RINGTONE r); //void SetRingTone(RingTone::RINGTONE r);
void AddContact(const char *num = 0, //void AddContact(const char *num = 0,
const char *subs = 0); //const char *subs = 0);

friend ostream& operator<< (ostream& os, friend ostream& operator<< (ostream& os,
const MobilePhone& p); const SmartPhone& p);
}; };

NPTEL MOOCs Programming in C++ Partha Pratim Das 14


Phone Hierarchy
class Phone { class MobilePhone : public LandlinePhone {
Module 24 public: protected:
virtual void Call(const PhoneNumber *p) AddressBook aBook_;
Partha Pratim = 0; PhoneNumber *lastDial_;
Das virtual void Answer() = 0; void SetLastDialed(const PhoneNumber& p);
virtual void ReDial() = 0; void ShowNumber();
}; public:
Objectives & class LandlinePhone: public Phone { MobilePhone(const char *num,
Outline void ReDial() const char *subs) :
{ cout << "Not implemented" << endl; } LandlinePhone(num, subs), // Base ctor
Example – lastDial_(0) {}
Phone protected: void Call(const PhoneNumber *p); // Override
Hierarchy PhoneNumber number_; void Call(const Name& n); // Overload
Name subscriber_; void ReDial();
Summary
RingTone rTone_; friend ostream& operator<< (ostream& os,
public: const MobilePhone& p);
LandlinePhone(const char *num, };
const char *subs) : class SmartPhone : public MobilePhone {
number_(num), subscriber_(subs), protected: unsigned int size_;
rTone_() {} void DisplayPhoto()
void Call(const PhoneNumber *p); public:
void Answer(); SmartPhone(const char *num,
friend ostream& operator<<(ostream& os, const char *subs) :
const LandlinePhone& p); MobilePhone(num, subs), // Base ctor
}; lastDial_(0) {}
void Call(const PhoneNumber *p); // Override
void Call(const Name& n); // Override
void ReDial(); // Override
friend ostream& operator<< (ostream& os,
const SmartPhone& p);
};
NPTEL MOOCs Programming in C++ Partha Pratim Das 15
Hierarchy of Phones

Module 24

Partha Pratim
Das

Objectives &
Outline

Example –
Phone
Hierarchy

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 16


Module Summary

Module 24

Partha Pratim Using the Phone Hierarchy as an example analyzed the


Das
design process with inheritance
Objectives &
Outline

Example –
Phone
Hierarchy

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 17


Instructor and TAs

Module 24

Partha Pratim
Das Name Mail Mobile
Objectives &
Partha Pratim Das, Instructor [email protected] 9830030880
Outline Tanwi Mallick, TA [email protected] 9674277774
Example –
Srijoni Majumdar, TA [email protected] 9674474267
Phone Himadri B G S Bhuyan, TA [email protected] 9438911655
Hierarchy

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 18

You might also like