asked 180k views
1 vote
Design a class that holds the following personal data: name, 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 friend's or family member's information.

asked
User Bastl
by
9.2k points

1 Answer

0 votes

Answer:

The program in C++ is as follows:

#include <iostream>

using namespace std;

class MyClass {

public:

string name;

int age;

int phone;};

int main() {

MyClass me;

me.name = "MrRoyal";

me.age = 10;

me.phone = 12345;

MyClass frnd;

frnd.name = "Friend";

frnd.age = 12;

frnd.phone = 34567;

MyClass family;

family.name = "Family";

family.age = 15;

family.phone = 10111;

cout << me.name << " " << me.age<<" "<<me.phone<<endl;

cout << frnd.name << " " << frnd.age<<" "<<frnd.phone<<endl;

cout << family.name << " " << family.age<<" "<<family.phone<<endl;

return 0;}

Step-by-step explanation:

This defines the class

class MyClass {

The access specifier

public:

The next three lines are the attributes of the class

string name;

int age;

int phone;};

The main begins here

int main() {

This creates the class instance for me (you)

MyClass me;

This sets the name

me.name = "MrRoyal";

This sets the age

me.age = 10;

This sets the phone number

me.phone = 12345;

This creates the class instance for your friend

MyClass frnd;

This sets the name

frnd.name = "Friend";

This sets the age

frnd.age = 12;

This sets the phone number

frnd.phone = 34567;

This creates the class instance for a family member

MyClass family;

This sets the name

family.name = "Family";

This sets the age

family.age = 15;

This sets the phone number

family.phone = 10111;

These print the necessary outputs

cout << me.name << " " << me.age<<" "<<me.phone<<endl;

cout << frnd.name << " " << frnd.age<<" "<<frnd.phone<<endl;

cout << family.name << " " << family.age<<" "<<family.phone<<endl;

return 0;}

answered
User RobertyBob
by
8.8k points