Solution_CS3201_OOPS_MidSem
Solution_CS3201_OOPS_MidSem
Q2 Assume there are 2.20462 pounds in a kilogram, write a C++ program that asks the user to enter 3
number of pounds, and then display the equivalent in kilogram
int main()
{
float pound, kg;
cout << “\n Enter quantity in pounds: ”;
cin >> pound;
kg = pound / 2.20462;
cout << “\n Equivalent in Kg is: ” << kg;
return 0;
}
3
Q3 Write a C++ program that prints following: 10 20 19. Use an integer constant for the 10, an
arithmetic assignment operator to generate the 20, and a decrement operator to generate the 19.
int main()
{
int var = 10;
cout << var << “ ”;
var *= 2;
cout << var-- << “ ” ;
cout << var << “ ” ;
return 0;
}
Q4 Write a for loop, while loop, and do-while loop using C++ program that displays the numbers 5
from 112 to 123 except 117.
***** For-loop *****
int i = 112;
while(i<=123)
{
if(i==117)
{
i++;
continue;
}
cout<<i<<endl;
i++;
}
int i = 112;
do
{
if(i==117)
{
i++;
continue;
}
cout<<i<<endl;
i++;
}while(i<=123);
3
Q5 Write the syntax for function declaration. What is the significance of empty parentheses in a
function declaration? Also tell how many values can be returned from a function.
***** Function declaration
Q6 Assume the following function definition: int Demo (int a ) { return (a*2); } 3
Write a main () program in C++ that includes everything necessary to call this function.
int main()
{
int Demo (int);
int x = Demo (37);
return 0;
}
Q7 Create a class time that has separate int member data for hours, minutes, and seconds. One 8
constructor should initialize this data to 0, and another should initialize it to fixed values. A member
function should display it, in 11:59:59 format. The final member function should add two objects of
type time passes as arguments. A main () program in C++ should create two initialized time
objects, and one that is not initialized. Then it should add the two initialized values together, leaving
the result in the third time variable. Finally, it should display the value of this third variable.
class time
{
int hours, minutes, seconds;
public:
time()
{
hours = 0; minutes = 0; seconds = 0;
}
time(int h, int m, int s)
{
hours = h; minutes = m; seconds = s;
}
void display()
{
cout<<hours<<":"<<minutes<<":"<<seconds;
}
void add_time(time t1, time t2)
{
seconds = t1.seconds + t2.seconds;
if(seconds > 59)
{
seconds -= 60; minutes++;
}
minutes += t1.minutes + t2.minutes;
if(minutes > 59)
{
minutes -= 60; hours++;
}
hours += t1.hours + t2.hours;
}
};
int main() {
time T1(5,59,59);
time T2(4,30,30);
time T3;
T3.add_time(T1, T2);
cout<<"\n Time = ";
T3.display();
return 0;
}