B) (10 pts.) Create an Person class that contains the following instance variabl
ID: 3802438 • Letter: B
Question
B) (10 pts.) Create an Person class that contains the following instance variables: name, phone, and address where composition of the Address class is used for the address instance variable. Provide a constuctor that initializes all three instance variables with the data passed to the constructor. Provide methods that allow you to get and set the name and phone instance variables. (Note: You do not need to provide these for the address instance variable because you are using composition). Demonstrate the correctness of your program with a couple sample test cases.
Explanation / Answer
class Person
{
string name;
unsigned int phone;
void setname(){name=s;}
void setphone(){phone=p;}
string getname(){return name;}
unsigned int getphone(){return phone;}
class Address{
string streetname;
string city;
longint pin;
void setaddress()
{
streetname=s;
city=c;
pin=p;
cout<<"Enter Street name: ";
cin>>s;
cout<<"Enter City name";
cin>>c;
cout<<"Enter Pin code:";
cin>>p;
}
void getaddress()
{
cout<<streetname;
cout<<city;
cout<<pin;
}
};
};
int main()
{
Person p1;
Person::Address a1;
cout<<"Enter Name:";
p1.setname();
cout<<"Enter Phone number:";
p1.setphone();
cout<<"Enter Address";
a1.setaddress();
cout<<"Person details are: ";
p1.getname();
p1.getphone();
a1.getaddress();
}
Output:
Enter Person Name: Chegg
Enter person Phone: 9876543210
Enter Address:
Enter Street Name: UK Colony
Enter City Name: New york
Enter Pin code: 543210
Person details are:
chegg
9876543210
UK Colony,Newyork,543210.
Hence solution provide.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.