Answer to Question #272342 in C++ for Imran

Question #272342

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

1
Expert's answer
2021-11-29T02:46:38-0500
#include<iostream>
#include<string>

using namespace std;

//function to check numbers of phone
bool IsPhone(string Phone)
{
	for(int i=0;i<Phone.size();i++)
	{
		if(!isdigit(Phone[i]))
		{
			cout<<"Incorrect phone!\n";
			return false;
		}
	}
	return true;	
}

class LocPhone
{
	string phone;
public:
	void InputPhone()
	{
		//Suppose, that local phone must have 6 digits
		do
		{
			cout<<"Please, enter a local telephone number with 6 digits: ";
			cin>>phone;	
		}while(phone.size()!=6||!IsPhone(phone));		
	}
	void DisplayPhone()
	{
		cout<<phone;
	}
};

class NatPhone:public LocPhone
{
	string cityCode;
public:
	void InputCityCode()
	{
		//Suppose, that national phone must have 4 digits
		do
		{
			cout<<"Please, enter a national city code with 4 digits: ";
			cin>>cityCode;	
		}while(cityCode.size()!=4||!IsPhone(cityCode));		
	}
	void DisplayCityCode()
	{
		cout<<cityCode;
	}
};

class IntPhone:public NatPhone
{
	string countCode;
public:
	void InputCountCode()
	{
		//Suppose, that international phone must have 3 digits
		do
		{
			cout<<"Please, enter an international country code with 3 digits: ";
			cin>>countCode;	
		}while(countCode.size()!=3||!IsPhone(countCode));		
	}
	void DisplayCountCode()
	{
		cout<<countCode;
	}
};

int main()
{
	LocPhone l;
	l.InputPhone();
	cout<<"Your local phone: ";
	l.DisplayPhone();
	cout<<endl;
	
	NatPhone n;
	n.InputPhone();
	n.InputCityCode();
	cout<<"Your national phone: ";
	n.DisplayCityCode();
	n.DisplayPhone();
	cout<<endl;
	
	IntPhone i;
	i.InputPhone();
	i.InputCityCode();
	i.InputCountCode();
	cout<<"Your international phone: ";
	i.DisplayCountCode();
	i.DisplayCityCode();
	i.DisplayPhone();	
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS