In: Computer Science
Write a short and simple C++ program (must be able to have custom inputs):
Design a class that holds the following personal data:
name, address, age, and phone number
Write appropriate accessor and mutator functions. Demonstrate the class by writing a program that creates three instances of it. One instance should hold your information, and the other two should hold your friends' or family members' information.
Thanks!!
Solution:
#include<string>
#include<iostream>
using namespace std;
class Details
{
string name;
string address;
int age;
string phone;
public:
Details()
{
this->name="X";
this->address="Y";
this->age=0;
this->phone="0000000000";
}
Details(string name,string
address,int age,string phone)
{
this->name=name;
this->address=address;
this->age=age;
this->phone=phone;
}
void setName(string name)
{
this->name=name;
}
void setAddress(string
address)
{
this->address=address;
}
void setAge(int age)
{
this->age=age;
}
void setPhone(string phone)
{
this->phone=phone;
}
string getName()
{
return
name;
}
string getAddress()
{
return
address;
}
int getAge()
{
return
age;
}
string getPhone()
{
return
phone;
}
};
void display(Details d)
{
cout<<"\n\nDetails";
cout<<"\nName:"<<d.getName();
cout<<"\nAddress:"<<d.getAddress();
cout<<"\nAge:"<<d.getAge();
cout<<"\nPhone
Number:"<<d.getPhone();
}
int main()
{
string name,address,phone;
int age;
cout<<"Enter your Details\n";
cout<<"Enter your Name:";
cin>>name;
cout<<"Enter your Address:";
cin>>address;
cout<<"Enter your Age:";
cin>>age;
cout<<"Enter your Phone Number:";
cin>>phone;
Details d1(name,address,age,phone);
cout<<"\nEnter Details of friend1/family
member1\n";
cout<<"Enter your Name:";
cin>>name;
cout<<"Enter your Address:";
cin>>address;
cout<<"Enter your Age:";
cin>>age;
cout<<"Enter your Phone Number:";
cin>>phone;
Details d2(name,address,age,phone);
cout<<"\nEnter Details of friend2/family
member2\n";
cout<<"Enter your Name:";
cin>>name;
cout<<"Enter your Address:";
cin>>address;
cout<<"Enter your Age:";
cin>>age;
cout<<"Enter your Phone Number:";
cin>>phone;
Details d3(name,address,age,phone);
display(d1);
display(d2);
display(d3);
return 0;
}
Output: