this what i got so far, and its not outputting anything . this is just part of the program instructions.
Part II
We are going to define a class called Person. Person class will have 3 private member variables:
string firstName; string lastName; PhoneNumber phoneNumber;
string toString(); // Must return string for firstName, lastName, theAreaCode, and theNumber
// in the following format: "Hakan Haberdar 281-3424617". For example, if I want to print the // information about a person object, I should be able to type:
// cout << personObject.toString(); // THIS IS NOT OPERATOR OVERLOADING
// worth 4 points if implemented as described
If you use cout (or any output function doing something similar) in order to print the information in the function toString(), TA will deduct 6 points for this mistake. If you do not return the string in the format given above, TA will deduct 4 points. The class Person will have only one constructor:
Person(string theFirstName, string theLastName, int theAreaCode, int theNumber); // worth 3 points if implemented as described
If you define a no argument (or any other) constructor, TA will deduct 3 points for this mistake. Please be very careful about how you will create phoneNumber object (which is member variable in the class Person). There is a special way to call the constructor of a member variable that is an object of another class. In the book, there are several examples of this. If you make a mistake in the Person constructor, TA will deduct 2 points.
There is one point that we need to point out here. In the implementation of the member function toString, you are going to notice that you need a function to convert integer value to string (c-string) value. We here provide you the private static member function num2str:
void Person::num2str(int number, char numberStr[]) {
int i=0, j=0;
char tmpNumberStr[128]; if(number<0)// ignore negative values
}
do
{
number=-number;
int lastDigit = number%10; tmpNumberStr[i++]='0'+lastDigit; number= number/10;
}while( number>0); //reverse while(i>0)
numberStr[j++]=tmpNumberStr[--i]; numberStr[j]='\0';
You are going to use the function num2str in toString(). Please carefully look at the function header. It takes 2 arguments. The first argument is integer value, and the second argument is a character array. The function returns c-string (i.e. special character array) representation of the number in the char array numberStr. Therefore, in order to use this function, you first need to declare an array of characters. Please notice that you are supposed to use this function in the function toString() as a helper function. That is why it is private.
When you implement the class Person as exactly described here, you will earn 25 points.
}
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
|
#include "Person.h"
#include "PhoneNumber.h"
#include <string>
using namespace std;
string Person::getFirstName(){
return firstName;
}
string Person::getLastName(){
return lastName;
}
void Person::setFirstName(string theFirstName)
{
firstName=theFirstName;
}
void Person::setLastName(string theLastName)
{
lastName=theLastName;
}
PhoneNumber Person::getPhoneNumber()
{
return phoneNumber;
}
void Person::setPhoneNumber(int theAreaCode, int theNumber)
{
phoneNumber.setAreaCode(theAreaCode);
phoneNumber.setNumber(theNumber);
}
Person::Person(string theFirstName, string theLastName, int theAreaCode, int theNumber)
:firstName(theFirstName), lastName(theLastName), phoneNumber(theAreaCode, theNumber)
{}
void Person::num2str(int number, char numberStr[]) {
int i=0, j=0;
char tmpNumberStr[128];
if(number<0)
number=-number; // ignore negative values
do
{
int lastDigit = number%10;
tmpNumberStr[i++]='0'+lastDigit;
number= number/10;
}while( number>0);
while(i>0)
numberStr[j++]=tmpNumberStr[--i];
numberStr[j]='\0';
}
string Person::toString()
{
char tempArea[100];
char tempNum[100];
return (getFirstName(), getLastName(),
num2str(phoneNumber.getAreaCode(), tempArea), num2str(phoneNumber.getNumber(), tempNum));
}
|