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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
#include <iostream>
#include <cmath>
using namespace std;
class Two_Nums
{
//Declaration of variables as public and private
private:
double num1;
double num2;
public:
Two_Nums(double = 10.5, double = 20.5);
double add();
double avg();
void chg_nums(double, double);
void get_nums(double &, double &);
}
Two_Nums::Two_Nums(double n1, double n2)
{
num1 = n1;
num2 = n2;
}
double Two_Nums::add()
{
return(num1 + num2);
}
double Two_Nums::avg()
{
return((num1 + num2)/2);
}
void Two_Nums::get_nums(double &n1, double &n2)
{
n1 = (this*).num1;
n2 = (this*).num2;
return;
}
void Two_Nums::chg_nums(double n1, double n2)
{
num1 = n1;
num2 = n2;
return;
}
int main()
{
char ch;
double n1, n2;
Two_Nums two1, two2;
two1.get_nums(n1, n2);
cout<<"By default, num1's value = "<<n1<<endl;
cout<<"By default, num2's value = "<<n2<<endl;
cout<<"The sum of num1 & num2 = "<<two1.add()<<endl;
cout<<"The average of num1 & num2 = "<<two1.avg()<<endl<<endl;
two2.chg_nums(3.5, 9.9); //changed values for num1 and num2
two2.get_nums(n1, n2);
cout<<"Changed value for num1 = "<<n1<<endl;
cout<<"Changed value for num2 = "<<n2<<endl;
cout<<"The sum of the new values of num1 & num2 = "<<two2.add()<<endl;
cout<<"The average of the new values of num1 & num2 = "<<two2.avg()<<endl;
cout<<"Press 'e' to exit...";
cin>>ch;
}
|