Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

C++ class that holds the following personal data name address age and phone number

Personal Information Class
Design a C++ 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.

Answer:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//  Personal Information Class
#include <iostream>
#include <cstring>
using namespace std;
 
// Constants
const int SIZE = 81;
 
// PersonalInfo class declaration
class PersonalInfo
{
private:
   char name[SIZE];
   char address[SIZE];
   int age;
   char phone[SIZE];
 
public:
   // Default constructor
   // Sets all fields to null or 0.
   PersonalInfo()
   { name[0] = '\0';
     address[0] = '\0';
     age = 0;
     phone[0] = '\0';
   }
    
   // Overloaded Constructor
   // Parameters: n is the name
   //   addr is the address
   //   a is the age
   //   p is the phone
   PersonalInfo(char n[], char addr[], int a, char p[])
   {
      setName(n);
      setAddress(addr);
      setAge(a);
      setPhone(p);
   }
    
   // Mutator functions
   void setName(char n[])
   { strncpy(name, n, SIZE);
     name[SIZE-1] = '\0';
   }
 
   void setAddress(char a[])
   { strncpy(address, a, SIZE);
     address[SIZE-1] = '\0';
   }
 
   void setAge(int a)
   { age = a; }
    
   void setPhone(char p[])
   { strncpy(phone, p, SIZE);
     phone[SIZE-1] = '\0';
   }
    
   // Accessor functions
   const char *getName() const
   { return name; }
    
   const char *getAddress() const
   { return address; }
    
   int getAge() const
   { return age; }
    
   const char *getPhone() const
   { return phone; }
};
 
// Function prototye
void displayPersonalInfo(PersonalInfo);
 
// Demo program
int main()
{
   // Create the first instance of PersonalInfo.
   PersonalInfo me("Herb Dorfmann",
                   "27 Technology Drive",
                   25, "555-1212");
    
   // Create an instance for a family member.
   PersonalInfo auntSally("Sally Dorfmann",
                   "48 Friendly Street",
                   50, "555-8484");
 
   // Create an instance for a friend.
   PersonalInfo joe("Joe Looney",
                    "191 Apple Mountain Road",
                    30, "555-2222");
                    
   // Display the data in each object.
   displayPersonalInfo(me);
   displayPersonalInfo(auntSally);
   displayPersonalInfo(joe);
    
   return 0;
}
 
//**************************************
// The displayPersonalInfo function    *
// displays the data in a PersonalInfo *
// object.                             *
//**************************************
void displayPersonalInfo(PersonalInfo obj)
{
   cout << "Name: " << obj.getName()
        << endl;
   cout << "Address: " << obj.getAddress()
        << endl;
   cout << "Age: " << obj.getAge()
        << endl;
   cout << "Phone: " << obj.getPhone()
        << endl << endl;
}

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!