In: Computer Science
directions: use c++
Create a ContactInfo class that contains the following member variables:
The ContactInfo class should have a default constructor that sets name = "", phoneNumber = "", and age = 0.
The ContactInfo class should have a constructor that accepts the name and phone number as parameters and sets name = <value of parameter name>, aAge = 0, and phoneNumber = <value of parameter phone number>.
The ContactInfo class should have accessor and mutator functions for each member variable.
The ContactInfo class should have a function called canVote that returns a boolean. It will return true if age is >= 18. Otherwise, it will return false.
Create three instances of this class in the main function
Write a C++ function (outside of the class) that will display (as shown below in the sample run) an instance of the ContactInfo class. Call the function to display the three instances you created in the main function.
Make sure your program conforms to the following
requirements:
1. This program should be called ContactInfoApp.cpp. The
header file should be called ContactInfo.h and place
member function definitions in ContactInfo.cpp. You must
submit these three files.
Include the basic header in your program. (5 points deduction if missing)
2. Properly set up the ContactInfo class and declare three instances of ContactInfo (45 points)
3. Write a C++ program that will create and display (as shown below in the sample run) three instances of the ContactInfo class. (25 points).
4. Add comments wherever necessary. (5 points)
Sample Run:
NOTE: not all possible runs are shown below.
Name: Kristen Lee Phone Number: 555-2021 Age: 45 Can vote?: Yes Name: Joe Smith Phone Number: 111-9999 Age: 5 Can vote?: No Name: Tom Hanks Phone Number: 111-8888 Age: 75 Can vote?: Yes
General Requirements:
1) Include the header comment with your name and other information on the top of your files.
2. Please make sure that you're conforming to specifications (program name, print statements, expected inputs, and outputs, etc.). Not doing so will result in a loss of points. This is especially important for prompts. They should match mine EXACTLY.
3. If we have listed a specification and allocated a point for it, you will lose points if that particular item is missing from your code, even if it is trivial.
4. No global variables (variables outside of main) unless they are constants.
5. All input and output must be done with streams, using the library iostream
6. You may only use the iostream, iomanip, and string libraries. Including unnecessary libraries will result in a loss of points.
7. NO C style printing is permitted. (Aka, don't use printf). Use cout if you need to print to the screen.
8. When you write source code, it should be readable and well-documented (comments).
9. Make sure you test using G++ (to be sure it reports no compile errors or warnings!) before you submit the program.
10. Testing your program thoroughly is a part of writing good code. We give you sample runs to make sure you match our output requirements and to get a general idea of how we would test your code. Matching your outputs for JUST the sample runs is not a guarantee of a 100. We have several extensive test cases.
11. Program submissions should be done through the Canvas class page, under the assignments tab (if it's not there yet I'll create it soon.) Do not send program submissions through e-mail { e-mail attachments will not be accepted as valid submissions.
12. The four files you will submit via Canvas are ContactInfoApp.cpp, ContactInfo.h, ContactInfo.cpp, and your makefile.
13. Please make sure you've compiled and run your program before you turn it in. Compilation errors can be quite costly. We take 4 points per compiler error for the first 9 errors. The 10th compiler error will result in a grade of 0.
14. Only a file turned in through Canvas counts as a submission. A file on your computer, even if it hasn't been edited after the deadline, does not count. Now that you are familiar with the process of turning in a file through Canvas, we will not accept any excuses.
15. The student is responsible for making sure they have turned in the right file(s). We will not accept any excuses about inadvertently modifying or deleting files, or turning in the wrong files.
16. General Advice - always keep an untouched copy of your finished homework files in your email. These files will have a time-stamp which will show when they were last worked on and will serve as a backup in case you ever have legitimate problems with submitting files through Canvas. Do this for ALL programs.
ContactInfo.h:
Code as text (to copy):
#ifndef _CONTACT_INFO_H_
#define _CONTACT_INFO_H_
#include <iostream>
#include <string>
using namespace std;
class ContactInfo {
private:
string name;
string phoneNumber;
int age;
public:
// default constructor
ContactInfo();
// parameterised constructor
ContactInfo(string _name, string _phoneNumber);
// accessor
string getName();
string getPhoneNumber();
int getAge();
// mutators
void setName(string _name);
void setPhoneNumber(string _phoneNumber);
void setAge(int _age);
// member functions
bool canVote();
};
#endif
ContactInfo.cpp:
Code as text (to copy):
#include "ContactInfo.h"
// Default constructor
ContactInfo::ContactInfo() {
name = "";
phoneNumber = "";
age = 0;
}
// Parameterised constructor
ContactInfo::ContactInfo(string _name, string _phoneNumber) {
name = _name;
phoneNumber = _phoneNumber;
age = 0;
}
// Accessor functions
// function to get name of contact
string ContactInfo::getName() {
return name;
}
// function to get phone number of contact
string ContactInfo::getPhoneNumber() {
return phoneNumber;
}
// function to get age of contact
int ContactInfo::getAge() {
return age;
}
// Mutator functions
// function to set name of contact
void ContactInfo::setName(string _name) {
name = _name;
}
// function to set phone number of contact
void ContactInfo::setPhoneNumber(string _phoneNumber) {
phoneNumber = _phoneNumber;
}
// function to set age of contact
void ContactInfo::setAge(int _age) {
age = _age;
}
// Member functions
// boolean function
// returns true if age >= 18 otherwise return false
bool ContactInfo::canVote() {
return age >= 18;
}
ContactInfoApp.cpp:
Code as text (to copy):
#include <iostream>
#include <string>
#include "ContactInfo.h"
using namespace std;
// function to display an instance of the class ContactInfo
void displayContactInfo(ContactInfo& obj) {
cout << "Name: " << obj.getName() << endl;
cout << "Phone Number: " << obj.getPhoneNumber() << endl;
cout << "Age: " << obj.getAge() << endl;
cout << "Can vote?: ";
if (obj.canVote()) cout << "Yes" << endl;
else cout << "No" << endl;
}
int main() {
// Create three instances of class ContactInfo
ContactInfo instance1("Kristen Lee", "555-2021");
instance1.setAge(45);
ContactInfo instance2, instance3;
instance2.setName("Joe Smith");
instance2.setPhoneNumber("111-9999");
instance2.setAge(5);
instance3.setName("Tom Hanks");
instance3.setPhoneNumber("111-8888");
instance3.setAge(75);
// call function to display details of three instances
displayContactInfo(instance1); cout << endl;
displayContactInfo(instance2); cout << endl;
displayContactInfo(instance3);
return 0;
}
makefile:
makefile as text (to copy):
CXX = g++
CXXFLAGS = -Wall
ContactInfoApp: ContactInfoApp.o ContactInfo.o
$(CXX) $(CXXFLAGS) -o ContactInfoApp ContactInfoApp.o ContactInfo.o
ContactInfoApp.o: ContactInfoApp.cpp
$(CXX) $(CXXFLAGS) -c ContactInfoApp.cpp
ContactInfo.o: ContactInfo.cpp ContactInfo.h
$(CXX) $(CXXFLAGS) -c ContactInfo.cpp
Sample Run:
P.s.> Check screenshots to get proper indentation of code.
ask any doubts in comments and don't forget to rate the answer.