# Object Oriented Programming in C++ By Robert Lafore - Chapter 4 Complete Solution - C++ OOP

Program 1:

A phone number, such as (212) 767-8900, can be thought of as having three parts: the area code (212), the exchange (767), and the number (8900). Write a program that uses a structure to store these three parts of a phone number separately. Call the structure phone. Create two structure variables of type phone. Initialize one, and have the user input a number for the other one. Then display both numbers. The interchange might look like this:

Enter your area code, exchange, and number: 415 555 1212
My number is (212) 767-8900

Code:
``````#include<iostream>
using namespace std;
// Code By HI

struct phone
{
int country_code, network_code, number;
};

int main()
{
phone phone1, phone2;

phone1.country_code = 92;
phone1.network_code = 318;
phone1.number = 6555198;

cout << "Enter your areacode, exchange, and number: "<<endl;
cin >> phone2.country_code >> phone2.network_code >> phone2.number;

cout << "My number is (" << phone1.country_code << ") " << phone1.network_code << "-" << phone1.number << endl;
cout << "Your number is (" << phone2.country_code << ") " << phone2.network_code << "-" << phone2.number << endl;

return 0;
}
``````

Output:

A point on the two-dimensional plane can be represented by two numbers: an x coordinate and a y coordinate. For example, (4,5) represents a point 4 units to the right of the vertical axis, and 5 units up from the horizontal axis. The sum of two points can be defined as a new point whose x coordinate is the sum of the x coordinates of the two points, and whose y coordinate is the sum of the y coordinates. Write a program that uses a structure called point to model a point. Define three points, and have the user input values to two of them. Then set the third point equal to the sum of the other two, and display the value of the new point.
Interaction with the program might look like this:

Enter coordinates for p1: 3 4
Enter coordinates for p2: 5 7
Coordinates of p1+ p2 are: 8, 11

Code:

``````#include<iostream>
using namespace std;
// Code By HI

struct point
{
int x, y;
};

int main()
{
point p1, p2, p3;

cout << "Enter cordinates for p1: "<<endl;
cin >> p1.x >> p1.y;
cout << "Enter cordinates for p2: "<<endl;
cin >> p2.x >> p2.y;

p3.x = p1.x + p2.x;
p3.y = p1.y + p2.y;

cout << "Coordinates of p1+p2 are: " << p3.x << ", " << p3.y << endl;

return 0;
}
``````

Output:

Program 3:

Create a structure called Volume that uses three variables of type Distance (from the ENGLSTRC example) to model the volume of a room. Initialize a variable of type Volume to specific dimensions, then calculate the volume it represents, and print out the result. To calculate the volume, convert each dimension from a Distance variable to a variable of type float representing feet and fractions of a foot, and then multiply the resulting three numbers.

Code:

``````#include<iostream>
using namespace std;
// Code By HI

struct Distance
{
int feet;
float inch;
};

struct Volume
{
Distance length, width, height;
};

int main()
{
float l, w, h;
Volume room1 = {{13, 2.5}, {11, 5.5}, {5,2.5}};

l = room1.length.feet+ room1.length.inch/12;
w = room1.width.feet+ room1.width.inch/12;
h = room1.height.feet+ room1.height.inch/12;

cout << "Volume = " << l*w*h << " cubic feet" << endl;

return 0;
}

``````

Output:

Program 4:

Create a structure called employee that contains two members: an employee number (type int) and the employee’s compensation (in dollars; type float). Ask the user to fill in this data for three employees, store it in three variables of type struct employee, and then display the information for each employee.

Code:

``````#include<iostream>
using namespace std;
// Code By HI

struct employee
{
int emp_num;
float emp_comp;
};

int main()
{
employee e1, e2, e3;
cout << "Enter employee details: " << endl;

cout <<"\nEmployee 1 \nEmployee number: ";
cin >> e1.emp_num;
cout << "Employee's compensation: ";
cin >> e1.emp_comp;

cout <<"\nEmployee 2 \nEmployee number: ";
cin >> e2.emp_num;
cout << "Employee's compensation: ";
cin >> e2.emp_comp;

cout <<"\nEmployee 3 \nEmployee number: ";
cin >> e3.emp_num;
cout << "Employee's compensation: ";
cin >> e3.emp_comp;

cout <<"\nEmployee Details: ";
cout <<"\n------------------ ";
cout <<"\nEmployee 1 \nEmployee number: " << e1.emp_num << "\nEmployee's compensation: "                  << e1.emp_comp << endl;
cout <<"\nEmployee 2 \nEmployee number: " << e2.emp_num << "\nEmployee's compensation: "                  << e2.emp_comp << endl;
cout <<"\nEmployee 3 \nEmployee number: " << e3.emp_num << "\nEmployee's compensation: "                  << e3.emp_comp << endl;

return 0;
}

``````
Output:

Create a structure of type date that contains three members: the month, the day of the month, and the year, all of type int. (Or use day-month-year order if you prefer.) Have the user enter a date in the format 12/31/2001, store it in a variable of type struct date, then retrieve the values from the variable and print them out in the same format.

Code:

``````#include<iostream>
using namespace std;
// Code By HI

struct date
{
int month, day, year;
};

int main()
{
char slash;
date today;
cout << "Enter date (MM/DD/YYYY): "<<endl;
cin >> today.month >> slash >> today.day >> slash >> today.year;
cout <<"\nDate is " << today.month << "/" << today.day << "/" << today.year << endl;

return 0;
}
``````

Output:

Program 6:

We said earlier that C++ I/O statements don’t automatically understand the data types of enumerations. Instead, the (>>) and (<<) operators think of such variables simply as integers. You can overcome this limitation by using switch statements to translate between the user’s way of expressing an enumerated variable and the actual values of the enumerated variable. For example, imagine an enumerated type with values that indicate an employee type within an organization: enum etype { laborer, secretary, manager, accountant, executive, researcher };
Write a program that first allows the user to specify a type by entering its first letter (‘l’, ‘s’, ‘m’, and so on), then stores the type chosen as a value of a variable of type enum etype, and finally displays the complete word for this type.

Enter employee type (first letter only) laborer, secretary, manager, accountant, executive, researcher): a
The employee type is an accountant.

You’ll probably need two switch statements: one for input and one for output.

Code:
``````#include<iostream>
using namespace std;
// Code By HI

enum employee_type {laborer, secretary, manager, accountant, executive, researcher};

int main()
{
employee_type etype;
char type;
cout << "Enter employee type (Just enter the first letter [laborer, secretary, manager, accountant,                           executive, researcher]): ";
cin >> type;

switch (type)
{
case 'l':
etype = laborer;
break;

case 's':
etype = secretary;
break;

case 'm':
etype = manager;
break;

case 'a':
etype = accountant;
break;

case 'e':
etype = executive;
break;

case 'r':
etype = researcher;
break;

default:
break;
}

switch(etype)
{
case laborer:
cout <<"Employee type is laborer";
break;

case secretary:
cout << "Employee type is secretary";
break;

case manager:
cout << "Employee type is a manager";
break;

case accountant:
cout << "Employee type is accountant";
break;

case executive:
cout << "Employee type is executive";
break;

case researcher:
cout << "Employee type is researcher";
break;

default:
cout << "Invalid input";
break;
}

cout << endl;

return 0;
}

``````
Output:

Program 7:

Add a variable of type enum etype (see Exercise 6), and another variable of type struct date (see Exercise 5) to the employee class of Exercise 4. Organize the resulting program so that the user enters four items of information for each of the three employees: an employee number, the employee’s compensation, the employee type, and the date of first employment. The program should store this information in three variables of type employee, and then display their contents.

Code:

``````#include<iostream>
using namespace std;
// Code By HI

enum etype {laborer, secretary, manager, accountant, executive, researcher};

struct date
{
int month, day, year;
};

struct employee
{
int emp_num;
float emp_compen;
date mdy;
etype emp_type;
};

int main()
{
employee emp1, emp2, emp3;
char e_type;
char slash;
cout <<"\nEnter the first employee details: ";
cout <<"\nEnter employee number: ";
cin >>emp1.emp_num;
cout <<"Enter employee compensation: ";
cin >>emp1.emp_compen;
cout <<"Enter employee type(l, s, m, a, e, r): ";
cin >>e_type;

switch(e_type)
{
case 'l':
emp1.emp_type = laborer;
break;

case 's':
emp1.emp_type = secretary;
break;

case 'm':
emp1.emp_type = manager;
break;

case 'a':
emp1.emp_type = accountant;
break;

case 'e':
emp1.emp_type = executive;
break;

case 'r':
emp1.emp_type = researcher;
break;

default:
cout <<"Invalid Input!";
}

cout <<"Enter date of first employment (MM/DD/YY): ";
cin >>emp1.mdy.month >> slash >> emp1.mdy.day >> slash >> emp1.mdy.year;

cout <<"\nEnter the second employee details: ";
cout <<"\nEnter employee number: ";
cin >>emp2.emp_num;
cout <<"Enter employee compensation: ";
cin >>emp2.emp_compen;
cout <<"Enter employee type(l, s, m, a, e, r): ";
cin >>e_type;

switch(e_type)
{
case 'l':
emp2.emp_type = laborer;
break;

case 's':
emp2.emp_type = secretary;
break;

case 'm':
emp2.emp_type = manager;
break;

case 'a':
emp2.emp_type = accountant;
break;

case 'e':
emp2.emp_type = executive;
break;

case 'r':
emp2.emp_type = researcher;
break;

default:
cout <<"Invalid Input!";
}
cout <<"Enter date of first employment (MM/DD/YY): ";
cin >>emp2.mdy.month >> slash >> emp2.mdy.day >> slash >> emp2.mdy.year;

cout <<"\nEnter the third employee details: ";
cout <<"\nEnter employee number: ";
cin >>emp3.emp_num;
cout <<"Enter employee compensation: ";
cin >>emp3.emp_compen;
cout <<"Enter employee type(l, s, m, a, e, r): ";
cin >>e_type;

switch(e_type)
{
case 'l':
emp3.emp_type = laborer;
break;

case 's':
emp3.emp_type = secretary;
break;

case 'm':
emp3.emp_type = manager;
break;

case 'a':
emp3.emp_type = accountant;
break;

case 'e':
emp3.emp_type = executive;
break;

case 'r':
emp3.emp_type = researcher;
break;

default:
cout <<"Invalid Input!";
}

cout <<"Enter date of first employment (MM/DD/YY): ";
cin >>emp3.mdy.month >> slash >> emp3.mdy.day >> slash >> emp3.mdy.year;

cout<<"\n----------------------------------------\n";

cout <<"\nDetails of first employee: ";
cout <<"\nEmployee number: " <<emp1.emp_num;
cout <<"\nEmployee compensation: " <<emp1.emp_compen;
cout <<"\nEmployee type is: ";

switch(emp1.emp_type)
{
case laborer:
cout <<"Laborer";
break;

case secretary:
cout <<"Secretary";
break;

case manager:
cout <<"Manager";
break;

case accountant:
cout <<"Accountant";
break;

case executive:
cout <<"Executive";
break;

case researcher:
cout <<"Researcher";
break;

default:
cout <<"No match found!";
}

cout <<"\nEmployee date of first employment: " <<emp1.mdy.month <<"/" <<emp1.mdy.day <<"/" <<emp1.mdy.year <<endl;

cout <<"\nDetails of second employee: ";
cout <<"\nEmployee number: " <<emp2.emp_num;
cout <<"\nEmployee compensation: " <<emp2.emp_compen;
cout <<"\nEmployee type is: ";

switch(emp2.emp_type)
{
case laborer:
cout <<"Laborer";
break;

case secretary:
cout <<"Secretary";
break;

case manager:
cout <<"Manager";
break;

case accountant:
cout <<"Accountant";
break;

case executive:
cout <<"Executive";
break;

case researcher:
cout <<"Researcher";
break;

default:
cout <<"No match found!";
}

cout <<"\nEmployee date of first employment: " <<emp2.mdy.month <<"/" <<emp2.mdy.day <<"/" <<emp2.mdy.year <<endl;

cout <<"\nDetails of first employee: ";
cout <<"\nEmployee number: " <<emp3.emp_num;
cout <<"\nEmployee compensation: " <<emp3.emp_compen;
cout <<"\nEmployee type is: ";

switch(emp3.emp_type)
{
case laborer:
cout <<"Laborer";
break;

case secretary:
cout <<"Secretary";
break;

case manager:
cout <<"Manager";
break;

case accountant:
cout <<"Accountant";
break;

case executive:
cout <<"Executive";
break;

case researcher:
cout <<"Researcher";
break;

default:
cout <<"No match found!";
}

cout <<"\nEmployee date of first employment: " <<emp3.mdy.month <<"/" <<emp3.mdy.day <<"/" <<emp3.mdy.year <<endl;

return 0;
}

``````
Output:

Program 8:

Start with the fraction-adding program of Exercise 9 in Chapter 2, “C++ Programming Basics.” This program stores the numerator and denominator of two fractions before adding them, and may also store the answer, which is also a fraction. Modify the program so that all fractions are stored in variables of type struct fraction, whose two members are the fraction’s numerator and denominator (both type int). All fraction-related data should be stored in structures of this type.

Code:

``````#include<iostream>
using namespace std;
// Code By HI

struct fraction
{
int num, den;
};

int main()
{
fraction num1, num2, num3;
char slash;

cout << "Enter first fraction: "<<endl;
cin >> num1.num >> slash >> num1.den;
cout << "Enter second fraction: "<<endl;
cin >> num2.num >> slash >> num2.den;

num3.num = (num1.num * num2.den) + (num1.den * num2.num);
num3.den = num1.den * num2.den;
cout << "Sum is: " << num3.num << "/" << num3.den << endl;

return 0;
}

``````

Output:

Program 9:

Create a structure called time. Its three members, all type int, should be called hours, minutes, and seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds. This can be in 12:59:59 format, or each number can be entered at a separate prompt (“Enter hours:”, and so forth). The program should then store the time in a variable of type struct time, and finally print out the total number of seconds represented by this time value:

long total secs = t1.hours*3600 + t1.minutes*60 + t1.seconds.

Code:

``````#include<iostream>
using namespace std;
// Code By HI

struct time
{
int hours, mins, secs;
};

int main()
{
time now;
char slash;

cout << "Enter time in format (HH:MM:SS): ";
cin >> now.hours >> slash >> now.mins >> slash >> now.secs;

int total_secs = (now.hours * 3600) + (now.mins * 60) + (now.secs);

cout << "Total seconds in " << now.hours << ":" << now.mins << ":"
<< now.secs << " are: " << total_secs << endl;

return 0;
}

``````

Output:

Create a structure called sterling that stores money amounts in the old-style British system discussed in Exercises 8 and 11 in Chapter 3, “Loops and Decisions.” The members could be called pounds, shillings, and pence, all of the type int. The program should ask the user to enter a money amount in new-style decimal pounds (type double), convert it to the old-style system, store it in a variable of type struct sterling, and then display this amount in pounds-shillings-pence format.

Code:

``````#include<iostream>
using namespace std;
// Code By HI

struct currency
{
int pounds, shillings, pence;
};

int main()
{
currency amount;
float dPounds;
cout << "Enter Decimal pounds: ";
cin >> dPounds;

amount.pounds = static_cast<int>(dPounds);
float fracPounds = dPounds - amount.pounds;

float dShillings =  fracPounds * 20;
amount.shillings = static_cast<int>(dShillings);
float fracShillings = dShillings - amount.shillings;

amount.pence = static_cast<int>(fracShillings * 12);

cout << "Equivalent in old notation: " << amount.pounds << "." << amount.shillings << "." << amount.pence << endl;

return 0;
}
``````

Output:

Program 11:

Use the time structure from Exercise 9, and write a program that obtains two-time values from the user in 12:59:59 format, stores them in struct time variables, converts each one to seconds (type int), adds these quantities, converts the result back to hours minutes-seconds, stores the result in a time structure, and finally displays the result in 12:59:59 format.

Code:

``````#include<iostream>
using namespace std;
// Code By HI

struct time
{
int hours, mins, secs;
};

int main()
{
time time1, time2, time3;
char slash;

cout << "Enter first time in format (HH:MM:SS): ";
cin >> time1.hours >> slash >> time1.mins >> slash >> time1.secs;
cout << "Enter second time in format (HH:MM:SS): ";
cin >> time2.hours >> slash >> time2.mins >> slash >> time2.secs;

int total_secs1 = (time1.hours * 3600) + (time1.mins * 60) + (time1.secs);
int total_secs2 = (time2.hours * 3600) + (time2.mins * 60) + (time2.secs);

int total_secs = total_secs1 + total_secs2;

time3.hours = total_secs/3600;
time3.mins = time3.hours%3600;
time3.secs = total_secs%60;

cout << "Addition of above input time is: " << time3.hours << ":"
<< time3.mins << ":" << time3.secs << endl;

return 0;
}
``````

Output:

Revise the four-function fraction calculator program of Exercise 12 in Chapter 3 so that each fraction is stored internally as a variable of type struct fraction, as discussed in Exercise 8 in this chapter.

Code:

``````#include<iostream>
using namespace std;
// Code By HI

struct fraction
{
int num, den;
};

int main()
{
char ch;
fraction num1, num2, num3;

do{
char slash, op;

cout << "Enter first fraction: ";
cin >> num1.num >> slash >> num1.den;
cout << "Operation: ";
cin >> op;
cout << "Enter second fraction: ";
cin >> num2.num >> slash >> num2.den;

switch(op)
{
case '+':
num3.num = (num1.num * num2.den) + (num1.den * num2.num);
num3.den = num1.den * num2.den;
cout << "Sum is: " << num3.num << "/" << num3.den << endl;
break;

case '-':
num3.num = (num1.num * num2.den) - (num1.den * num2.num);
num3.den = num1.den * num2.den;
cout << "On Subtraction: " << num3.num << "/" << num3.den << endl;
break;

case '*':
num3.num = num1.num * num2.num;
num3.den = num1.den * num2.den;
cout << "Multiplication is: " << num3.num << "/" << num3.den << endl;
break;

case '/':
num3.num = num1.num * num2.den;
num3.den = num1.den * num2.num;
cout << "Division is: " << num3.num << "/" << num3.den << endl;
break;

default:
cout << "Invalid Operator";
break;

}

cout << "Do you want to continue (y/n): ";
cin >> ch;

}

while(ch == 'y');

return 0;
}

``````

Output: