A driver program shows below. Please modify this C++program to demonstrate the u
ID: 667032 • Letter: A
Question
A driver program shows below. Please modify this C++program to demonstrate the use of the copy constructor and the relational operators you added.
1 // This program demonstrates the FeetInches class's overloaded
2 // + and - operators.
3 #include
4 #include "FeetInches.h"
5 using namespace std;
6
7 int main()
8 {
9 int feet, inches; // To hold input for feet and inches
10
11 // Create three FeetInches objects. The default arguments
12 // for the constructor will be used.
13 FeetInches first, second, third;
14
15 // Get a distance from the user.
16 cout << "Enter a distance in feet and inches: ";
17 cin >> feet >> inches;
18
19 // Store the distance in the first object.
20 first.setFeet(feet);
21 first.setInches(inches);
22
23 // Get another distance from the user.
24 cout << "Enter another distance in feet and inches: ";
25 cin >> feet >> inches;
26
27 // Store the distance in second.
28 second.setFeet(feet);
29 second.setInches(inches);
30
31 // Assign first + second to third.
32 third = first + second;
33
34 // Display the result.
35 cout << "first + second = ";
36 cout << third.getFeet() << " feet, ";
37 cout << third.getInches() << " inches. ";
38
39 // Assign first - second to third.
40 third = first - second;
41
42 // Display the result.
43 cout << "first - second = ";
44 cout << third.getFeet() << " feet, ";
45 cout << third.getInches() << " inches. ";
46
47 return 0;
48 }
Explanation / Answer
main.cpp
#include <cstdlib>
#include <iostream>
#include <iomanip>
//Classes
#include "FeetInches.h"
using namespace std;
//Problems Prototypes
void Menu();
int getN();
void def(int);
void problem9(); //Chapter 14 Problem 11
//Begin Execution Here!!!
int main(int argv,char *argc[])
{
int inN;
do{
Menu();
inN=getN();
switch(inN){
case 9: problem9();break; //Chapter 14 Problem 11
default: def(inN);}
}while(inN>=1&&inN<=10);
return 0;
}
void Menu(){
cout<<"9. Gaddis, 8thEd, Chapter 14, Problem 11"<<endl;
cout<<"Press any key and then Enter to exit "<<endl;}
int getN(){
int inN;
cin>>inN;
return inN;}
void def(int inN){
cout<<"You typed "<<inN<<" to exit the program"<<endl;
}
//Chapter 14 Problem 11
void problem9()
{
//Declare variables
int feet, inches; // To hold input for feet and inches
// Create two FeetInches objects
FeetInches first;
// Get a distance from the user.
cout <<" This program uses a copy constructor to create a copy of a class"
"object. Then the program will use the overload * operator to"
"multiply both objects and display the result." << endl << endl;
cout << "Enter a distance in feet and inches: ";
cin >> feet >> inches;
// Store the distance in first. 1
first.setFeet(feet);
first.setInches(inches);
//Creating the copy
FeetInches second(first);
//Multiplying the objects
FeetInches third = first * second;
cout <<" The multiplication of the original object and the copy is: ";
cout << third.getFeet() << " feet " << third.getInches() << " inches";
cout << endl << endl;
}
FeetInches.h
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.