Design, implement, and test a class that represents a phone number. The number s
ID: 3787341 • Letter: D
Question
Design, implement, and test a class that represents a phone number. The number should be represented by a country code, an area code, a number, and a type. The first three values can be integers. The type member is an enumeration of HOME, OFFICE, FAX, CELL, and PAGER. The class should provide a default constructor that sets all of the integer values to zero and the type to HOME. A constructor that enables all of the values to be set should be provided as well. You also should provide a constructor that takes just the number and type as arguments, and sets the country and area codes to those of your location. The class will have observers that enable each data member to be retrieved, and transformers that allow each data member to be changed. An additional observer should be provided that compares two phone numbers for equality.
This is the question, then for the answers, I have 2 cpp files and 2 header files
//Type.h
#ifndef ENUM
#define ENUM
//enumeration to the phone type
enum Type {Home, Office, Fax, Cell, Pager};
#endif // TYPE_H_
//phonenumber.cpp
#include "phoneNumberhead.h"
#include "Type.h"
#include <iostream>
#include <fstream>
//#include "stdafx.h"
phoneNumber::phoneNumber(){
countryCode = 0;
areaCode = 0;
number = 0;
type = Home;
}
//declaring constructor to initialize the meember variable
phoneNumber::phoneNumber(int cCode, int aCode, int num, Type pType){
countryCode = cCode;
areaCode = aCode;
number = num;
type = pType;
}
//pass valies to the data member
phoneNumber::phoneNumber(int num, Type pType){
countryCode = 11;
areaCode = 31;
number = num;
type = pType;
}
//mutators
void phoneNumber::setcountryCode(int cCode){ //set uo the contry codecvt
countryCode =cCode;
}
void phoneNumber::setareaCode(int aCode){ //set uo the contry codecvt
areaCode =aCode;
}
void phoneNumber::setnumber(int num){ //set uo the contry codecvt
number = num;
}
void phoneNumber::setType(Type pType){ //set uo the contry codecvt
type = pType;
}
//observer class
int phoneNumber::getcountryCode(){ //return ccoude
return countryCode;
}
int phoneNumber::getareaCode(){
return areaCode;
}
int phoneNumber::getnumber(){
return number;
}
//return the type of phoneNumber
Type phoneNumber::getType(){
return type;
}
//type to a string
string phoneNumber::typetostring(Type pType){
string str = ""; //swtich statement to teturn the tyoe of number ti string
switch (pType){
case Home:
str = "Home";
break;
case Office:
str = "Office";
break;
case Fax:
str = "Fax";
break;
case Cell:
str = "cell";
break;
case Pager:
str = "Pager";
break;
}
return str;
}
//method for compare
bool phoneNumber::compare(int cCode, int aCode, int num, Type pType){
//check for equaliity
if ((countryCode==cCode) &&(areaCode==aCode) && (number==num) && (type==pType))
return true;
else
return false;
}
//phonenumberDriver.cpp
#include "phoneNumberhead.h"
#include <iostream>
#include <fstream>
#include "Type.h"
using namespace std;
int main (){
//def contsructor
phoneNumber pNum1(); //def cans.
//print the object
cout << "Object 1, created by the Default Constructor"<<endl;
//using constructor
phoneNumber pNum2(1, 315, 654, Cell); //display the object
cout << "with 4 parameters " << " Phone Numbers: " << endl;
phoneNumber pNum3(789, Office);
cout << "with two parameters " << "phone Number " << endl;
//mutator assign phn numb
int setcountryCode(1);
int setareaCode(315);
int setnumber(654);
int setType(Cell);
cout << "object 1 "<<setcountryCode <<"Phone Number: "<< endl;
cout << "and object 2 ";
cout << "Phone Number in 1 ";
cout << "Phone Number in object 2 " << endl;
//check for eqality
if (pNum3.compare(pNum2.getcountryCode(), pNum3.getareaCode(), pNum3.getnumber(),pNum3.getType()))
cout << "The 2 phone numbers are equal" <<endl;
else
cout << "The 2 phone numbers are not equal" << endl;
//compare object1 and object2
cout << "and object 3 " << "Phone Numbers in Object 2: "<< endl;
cout << "phone number in object 3: " << endl;
//check for equality
if (pNum2.compare(pNum3.getcountryCode(),pNum3.getareaCode(),pNum3.getnumber(), pNum3.getType()))
cout << "The two phone numbers are equal" << endl;
else
cout << "The two phone numbers are not eqaul."<< endl;
//cout << system ("pause"):
// return 0;
}
//phonenumberhead.h
//#include "phoneNumberhead.h"
#include "Type.h"
#include <iostream>
#include <fstream>
using namespace std;
class phoneNumber {
public:
/*default constructor, no parameter,
default to member variable*/
phoneNumber (); //declaring constructor to initialize values
//with the given value to use
phoneNumber (int, int, int, Type); //constructor to the initialize values
phoneNumber(int, Type); //set the countryCode with specific values
void setcountryCode(int); //set country code
void setareaCode(int); //set area code
void setnumber(int); //tyoe the number
void setType(Type); //set the the area code
//method for returning the area, country, type code
int getcountryCode(); //return area code
int getareaCode();
int getnumber();
Type getType(); //return type of a number as a string
//return the type of the name as a string
string typetostring(Type);//comparing two phonenumbers
//compare two phone numbers
bool compare(int, int, int, Type);
private:
//member variable of class defined
int countryCode;
int areaCode;
int number;
Type type;
};
I did not get the desirable output,
This is what it should look like, please tell me what i did wrong and correct it please.
Sample output: Created object1 using default constructor Phone Number: 000 Phone Number Type HOME Created object2 using constructor with four parameters... Phone Number :3287157 Phone Number Type CELL Created object3 using constructor with two parameters... Phone Number 1111 Phone Number Type OFFICE Assigned new phone number to object1... Phone Number :3287157 Phone Number Type CELL Comparing phone number stored in object1 and object2. Phone Number in object 1: CELL-3287157 Phone Number in object 2: CELL-3287157 The two phone numbers are equal. Comparing phone number stored in object2 and object3... Phone Number in object 2: CELL-3287157 Phone Number in object 3: OFFICE-1111876 The two phone numbers are not equal. Press any key to continueExplanation / Answer
//Type.h
#ifndef ENUM
#define ENUM
//enumeration to the phone type
enum Type {Home, Office, Fax, Cell, Pager};
#endif // TYPE_H_
//phoneNumberhead.h
#include "Type.h"
#include <iostream>
#include <fstream>
using namespace std;
class phoneNumber {
public:
/*default constructor, no parameter,
default to member variable*/
phoneNumber (); //declaring constructor to initialize values
//with the given value to use
phoneNumber (int, int, int, Type); //constructor to the initialize values
phoneNumber(int, Type); //set the countryCode with specific values
void setcountryCode(int); //set country code
void setareaCode(int); //set area code
void setnumber(int); //tyoe the number
void setType(Type); //set the the area code
//method for returning the area, country, type code
int getcountryCode(); //return area code
int getareaCode();
int getnumber();
Type getType(); //return type of a number as a string
//return the type of the name as a string
string typetostring(Type);//comparing two phonenumbers
//compare two phone numbers
bool compare(int, int, int, Type);
private:
//member variable of class defined
int countryCode;
int areaCode;
int number;
Type type;
};
//phonenumber.cpp
#include "phoneNumberhead.h"
#include "Type.h"
#include <iostream>
#include <fstream>
//#include "stdafx.h"
phoneNumber::phoneNumber(){
countryCode = 0;
areaCode = 0;
number = 0;
type = Home;
}
//declaring constructor to initialize the meember variable
phoneNumber::phoneNumber(int cCode, int aCode, int num, Type pType){
countryCode = cCode;
areaCode = aCode;
number = num;
type = pType;
}
//pass valies to the data member
phoneNumber::phoneNumber(int num, Type pType){
countryCode = 11;
areaCode = 11;
number = num;
type = pType;
}
//mutators
void phoneNumber::setcountryCode(int cCode){ //set uo the contry codecvt
countryCode =cCode;
}
void phoneNumber::setareaCode(int aCode){ //set uo the contry codecvt
areaCode =aCode;
}
void phoneNumber::setnumber(int num){ //set uo the contry codecvt
number = num;
}
void phoneNumber::setType(Type pType){ //set uo the contry codecvt
type = pType;
}
//observer class
int phoneNumber::getcountryCode(){ //return ccoude
return countryCode;
}
int phoneNumber::getareaCode(){
return areaCode;
}
int phoneNumber::getnumber(){
return number;
}
//return the type of phoneNumber
Type phoneNumber::getType(){
return type;
}
//type to a string
string phoneNumber::typetostring(Type pType){
string str = ""; //swtich statement to teturn the tyoe of number ti string
switch (pType){
case Home:
str = "HOME";
break;
case Office:
str = "OFFICE";
break;
case Fax:
str = "FAX";
break;
case Cell:
str = "CELL";
break;
case Pager:
str = "PAGER";
break;
}
return str;
}
//method for compare
bool phoneNumber::compare(int cCode, int aCode, int num, Type pType){
//check for equaliity
if ((countryCode==cCode) &&(areaCode==aCode) && (number==num) && (type==pType))
return true;
else
return false;
}
//phonenumberDriver.cpp
#include "phoneNumberhead.h"
#include <iostream>
#include <fstream>
#include "Type.h"
using namespace std;
int main (){
//def contsructor
phoneNumber pNum1; //def cans.
//print the object
cout << "Created Object 1 using Default Constructor"<<endl;
cout << "Phone Number: " << pNum1.getcountryCode() << pNum1.getareaCode() << pNum1.getnumber() << endl;
cout << "Phone Number Type: " << pNum1.typetostring(pNum1.getType()) << endl;
cout << "Created Object 2 using constructor with four parameters"<<endl;
//using constructor
phoneNumber pNum2(3, 287, 157, Cell); //display the object
cout << "Phone Number: " << pNum2.getcountryCode() << pNum2.getareaCode() << pNum2.getnumber() << endl;
cout << "Phone Number Type: " << pNum2.typetostring(pNum2.getType()) << endl;
cout << "Created Object 3 using constructor with two parameters"<<endl;
phoneNumber pNum3(876, Office);
cout << "Phone Number: " << pNum3.getcountryCode() << pNum3.getareaCode() << pNum3.getnumber() << endl;
cout << "Phone Number Type: " << pNum3.typetostring(pNum3.getType()) << endl;
cout << "Assigned new phone number to object1 ";
//mutator assign phn numb
pNum1.setcountryCode(3);
pNum1.setareaCode(287);
pNum1.setnumber(157);
pNum1.setType(Cell);
cout << "Comparing phone numbers stored in object1 and object2 ";
cout << "Phone Number in object 1: " << pNum1.typetostring(pNum1.getType()) << "-" << pNum1.getcountryCode() << pNum1.getareaCode() << pNum1.getnumber() << endl;
cout << "Phone Number in object 2: " << pNum2.typetostring(pNum2.getType()) << "-" << pNum2.getcountryCode() << pNum2.getareaCode() << pNum2.getnumber() << endl;
//check for eqality
if (pNum1.compare(pNum2.getcountryCode(), pNum2.getareaCode(), pNum2.getnumber(),pNum2.getType()) == true)
cout << "The two phone numbers are equal" <<endl;
else
cout << "The two phone numbers are not equal" << endl;
cout << "Comparing phone numbers stored in object2 and object3 ";
cout << "Phone Number in object 2: " << pNum2.typetostring(pNum2.getType()) << "-" << pNum2.getcountryCode() << pNum2.getareaCode() << pNum2.getnumber() << endl;
cout << "Phone Number in object 3: " << pNum3.typetostring(pNum3.getType()) << "-" << pNum3.getcountryCode() << pNum3.getareaCode() << pNum3.getnumber() << endl;
//check for eqality
if (pNum3.compare(pNum2.getcountryCode(), pNum2.getareaCode(), pNum2.getnumber(),pNum2.getType()) == true)
cout << "The two phone numbers are equal" <<endl;
else
cout << "The two phone numbers are not equal" << endl;
//cout << system ("pause"):
// return 0;
}
/*
output:
Created Object 1 using Default Constructor
Phone Number: 000
Phone Number Type: HOME
Created Object 2 using constructor with four parameters
Phone Number: 3287157
Phone Number Type: CELL
Created Object 3 using constructor with two parameters
Phone Number: 1111876
Phone Number Type: OFFICE
Assigned new phone number t object1
Comparing phone numbers stored in object1 and object2
Phone Number in object 1: CELL-3287157
Phone Number in object 2: CELL-3287157
The two phone numbers are equal
Comparing phone numbers stored in object2 and object3
Phone Number in object 2: CELL-3287157
Phone Number in object 3: OFFICE-1111876
The two phone numbers are not equal
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.