Write output with explanation.
Int & fun()
{
staticint a = 10;
return a;
}
void main (void)
{
int& y = fun();
y = y + 30;
cout<<fun()<<endl;
}
Output: 40
Description:
In the above program variable “a” and “y” are pointing to same memory location. Here “y” is reference variable and alias with variable “a”. So the output will be 40.
What will be the output of following program?
class Test
{
int a,b;
public:
void getData(int x,int y)
{
a = x;
b = y;
}
void mul() const
{
a = a * b;
cout<<a;
}
};
void main()
{
Test t;
t.getData(2,4);
t.mul();
}
Description:
The above program will generate error. The function mul() is declared as const, so it cannot modify the value of variable. If you remove the keyword const then output will be 8.
Design classes Kilometer and Meter such that they support the following statements.
Kilometer KM1, KM2;
Meter M1, M2;
KM1 = M1;
M2 = KM2;
Solution#include<iostream>
usingnamespacestd;
class Meters
{
double meter;
public:
Meters()
{
Meters(double meter) : meter(meter)
}
void display()
{
cout<< meter <<" meters";
}
doublegetValue()
{
return meter;
}
};
class Kilometers
{
double km;
public:
Kilometers()
{
Kilometers(double km) : km(km)
}
void display()
{
cout<< km <<" KMs";
}
operator Meters()
{
return Meters(km * 1000);
}
Kilometers(Meters meter)
{
km = meter.getValue() / 1000;
}
};
int main(void)
{
doublex,y;
cout<<"Enter kilometer and meter"<<endl;
cin>>x>>y;
// Converting using the conversion function
Kilometers KM1, KM2;
Meters M1, M2;
KM1 = x;
M1 = KM1;
KM1.display();
cout<<" = ";
M1.display();
cout<< endl;
// Converting using the constructor
M2 = y;
KM2 = M2; // same as: Kilometers KM2 = Kilometers(KM2);
M2.display();
cout<<" = ";
KM2.display();
cout<< endl;
return 0;
}
Output:
Enter kilometer and meter
1
1000
1 KMs = 1000 meters
1000 meters = 1 KMs
Design a class employee which includes the following data members:
a) Emp Number
b) Emp Name
c) Basic.
Member functions:
1) To assign initial value
2) To calculate net salary
3) To display Net salary with employee details.
Calculate the net salary using the formulae
Gross salary = Basic + DA + HRA
Net salary = Gross salary – Deductions
The following conditions apply for the calculations
1) DA is 40% of basic salary
2) HRA is 30% of basic salary
3) PF is deduction is 10% of the basic salary.
Solution:#include<stdio.h>
#include<iostream>
usingnamespacestd;
class employee
{
int emp_no;
char_name[20];
float basic, net_salary;
public:
void accept_data();
void calculate_salary();
void display();
};
void employee::accept_data()
{
cout<<"\nEnter Emp No.:: ";
cin>>emp_no;
cout<<"\nEnter Name:: ";
cin>>e_name;
cout<<"\nEnter basic pay:: ";
cin>> basic;
cout<<”\n”;
}
void employee::calculate_salary()
{
float da, hra, gross_sal, pf;
da = 0.4f * basic;
hra = 0.3f * basic;
pf = 0.1f * basic;
gross_sal = basic + da + hra;
net_salary = gross_sal - pf;
}
void employee::display()
{
cout<<"\n\nEmp Id \t Name \t Salary \n";
cout<<"-----------------------------------\n\n";
cout<<emp_no<<"\t"<<e_name<<"\t"<<net_salary;
}
int main()
{
employee e;
e.accept_data();
e.calculate_salary();
e.display();
return 0;
}