Complete the following class by adding a constructor, two accessors, and two mut
ID: 3577755 • Letter: C
Question
Complete the following class by adding a constructor, two accessors, and
two mutators. The constructor should initialize all objects with firstNumber = 10 and
secondNumber = 15. Each accessor function should return the value of one of the data members,
and the two mutator functions should be capable of altering one of the data members.
// declaration section
class TwoNumbers
{
private
int firstNumber;
int secondNumber;
double average;
// function prototypes
};
// implementation section
b. Include the class written for Exercise 9a in the context of a complete program. The program
should create a single object and display the object’s values and the average of these values.
Explanation / Answer
#include <iostream>
using namespace std;
class TwoNumbers{
private:
int firstNumber;
int secondNumber;
double average;
public:
TwoNumbers(void){
firstNumber = 10;
secondNumber = 15;
}
void setFirstNumber(int x){
firstNumber = x;
}
void setSecondNumber(int y){
secondNumber = y;
}
int getFirstNumber(){
return firstNumber;
}
int getSecondNumber(){
return secondNumber;
}
double getAverage(){
average = (firstNumber + secondNumber)/2.0;
return average;
}
};
int main() {
TwoNumbers t;
cout << t.getFirstNumber() << endl;
cout << t.getSecondNumber() << endl;
cout << t.getAverage() << endl;
}
/* sample output
10
15
12.5
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.