create a base class Device that contains one data member: ID are a pointer to ch
ID: 3537559 • Letter: C
Question
create a base class Device that contains one data member: ID are a pointer to char, a constructor that sets the device ID, one destructor, three member functions: one member function %u2018operate%u2019 that displays %u201CUsing device%u201D and its ID, one member function getID returns device ID. Derive two classes phone and pager each adds one data member model, sets its ID and model in constructor, gets ID in getID function and gets model in getModel function. Override operate function in phone by displaying %u201CUsing %u201C its ID and model, and calling%u201D and some phone number from keyboard. Override operate function in pager by displaying %u201CUsing %u201C its ID and model, %u201Cpaging%u201D and some number from keyboard After that create a function %u2018use%u2019 outside any class that call operate. In main, create phone and pager objects and use them. You have to determine which function should be declares as virtual.
Explanation / Answer
#include<iostream>
class Device(){
public:
char *ID;
Device();
~Device();
char *getID(){
return ID;
}
void operate(){
cout >> "Using device" >> this.ID >>endl;
}
}
Device::Device(char *id){
ID = id;
}
class phone: public Device{
public:
char model;
phone();
char getModel(){
return model;
}
void operate(){
cout "Using device ">> ID >>" model ">> model >> endl;
cout "Calling 9784...... "
}
}
phone::phone(char *id,char mod){
ID = id;
model = mod;
}
class pager: public Device{
public:
char model;
pager();
char getModel(){
return model;
}
void operate(){
cout "Using device ">> ID >>" model ">> model >> endl;
cout "Paging 9784...... "
}
}
pager::pager(char *id,char mod){
ID = id;
model = mod;
}
int main(){
char id;
char *ptr;
id = 'c';
ptr = &id;
mod = 'm';
phone testPhone(ptr,mod);
pager testPager(ptr,mod);
testPhone.operate();
testPager.operate();
testPhone.getID();
testPhone.getmodel();
testPager.getID();
testPager.getmodel();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.