Write and run a program that creats a class called \"Two_Nums\" that has two pri
ID: 3685426 • Letter: W
Question
Write and run a program that creats a class called "Two_Nums" that has two private double data members called num1 and num2. A constructor must be provided that creates default values of 10.5 for num1 and 20.5 for num2. Other public member functions must be: double addO which returns the sum of num1 and num2, double avgO which returns the average of num1 and num2, void chg_nums which "mutates" the values of num1 and num2 and void get_nums which "accesses" the values of num1 and num2 using reference variables as arguments. Once the class is constructed, instantiate objects twol and two2 of the Two_Nums class in mainO- "cout" the default values of num1 and num2 using twol.get_nums(). Then, "cout" twol.addO and twol.avg. Continue by changing the values of num1 and num2 to 3.5 and 9.9 respectively for object two2 using chg_numO- Use two2.get_nums0 to "cout" num1 and num2. Finally, "cout" two2.add() and two2.avgQ.Explanation / Answer
// Source Code to Copy
#include <iostream>
using namespace std;
class Two_Nums{
private :
double num1; //private Members
double num2;
public :
Two_Nums(){ //Default Constructor
num1 = 10.5;
num2 = 20.5;
}
double add(){
return num1+num2;
}
double avg(){
return(num1+num2)/2;
}
void chg_nums(double* temp1, double* temp2){
this->num1 = *temp1;
this->num2 = *temp2;
}
void get_nums(){
std::cout <<" For two1 : Number 1 : " <<this->num1<<" "<<"Number 2 : " <<this->num2;
}
};
int main() {
Two_Nums two1;
Two_Nums two2;
two1.get_nums();
two2.get_nums();
std::cout <<" For two1 : Add : " <<two1.add()<<" "<<"Avg : " <<two1.avg();
std::cout <<" For two2 : Add : " <<two2.add()<<" "<<"Avg : " <<two2.avg();
double n1 =3.5;
double n2 =9.9;
two2.chg_nums(&n1,&n2); // Changing Numbers
two2.get_nums();
std::cout <<" For two2 : Add : " <<two2.add()<<" "<<"Avg : " <<two2.avg();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.