Write a class LocalPhone that contains an attribute phone to store a local telephone
number. The class contains member functions to input and display phone number. Write a child class NatPhone for national phone numbers that inherits LocPhone class. It additionally contains an attribute to store city code. It also contains member functions to input and show the city code. Write another class IntPhone for international phone numbers that inherit NatPhone class. It additionally contains an attribute to store country code. It also contains member functions to input and show the country code. Test these classes from main() by creating objects of derived classes and testing functions in a way that clear concept of multi-level Inheritance.
#include <iostream>
using namespace std;
class LocPhone
{
private:
string phone;
public:
void input()
{
cin >> phone;
}
void display()
{
cout << phone;
}
};
class NatPhone : public LocPhone
{
private:
string cityCode;
public:
void input()
{
cin >> cityCode;
}
void display()
{
cout << cityCode;
}
};
class IntPhone : public NatPhone
{
private:
string countryCode;
public:
void input()
{
cin >> countryCode;
}
void display()
{
cout << countryCode;
}
};
int main()
{
LocPhone* localPhone = new LocPhone();
cout << "Enter phone: ";
localPhone->input();
cout << "Local phone: ";
localPhone->display();
cout << endl << endl;
LocPhone* nationalPhone = new NatPhone();
cout << "Enter city code: ";
nationalPhone->input();
cout << "City code: ";
nationalPhone->display();
cout << endl << endl;
LocPhone* internationalPhone = new IntPhone();
cout << "Enter country code: ";
internationalPhone->input();
cout << "Country code: ";
internationalPhone->display();
cout << endl << endl;
delete localPhone;
delete nationalPhone;
delete internationalPhone;
return 0;
}
Comments
Leave a comment