Objects(C++ Programming ) Questions and Answers
Explanation:-
Answer: Option A. -> 10We are getting the number and copying it to j and printing it.
Output:
$ g++ obj2.cpp
$ a.out
10
Question 6.
1.
#include
2.
using namespace std;
3.
class Rect
4.
{
5.
int x, y;
6.
public:
7.
void set_values (int,int);
8.
int area ()
9.
{
10.
return (x * y);
11.
}
12.
};
13.
void Rect::set_values (int a, int b) {
14.
x = a;
15.
y = b;
16.
}
17.
int main ()
18.
{
19.
Rect recta, rectb;
20.
recta.set_values (5, 6);
21.
rectb.set_values (7, 6);
22.
cout << "recta area: " << recta.area();
23.
cout << "rectb area: " << rectb.area();
24.
return 0;
25.
}
What is the output of the program?
1.
#include
2.
using namespace std;
3.
class Rect
4.
{
5.
int x, y;
6.
public:
7.
void set_values (int,int);
8.
int area ()
9.
{
10.
return (x * y);
11.
}
12.
};
13.
void Rect::set_values (int a, int b) {
14.
x = a;
15.
y = b;
16.
}
17.
int main ()
18.
{
19.
Rect recta, rectb;
20.
recta.set_values (5, 6);
21.
rectb.set_values (7, 6);
22.
cout << "recta area: " << recta.area();
23.
cout << "rectb area: " << rectb.area();
24.
return 0;
25.
}
Explanation:-
Answer: Option A. -> recta area: 30 rectb area: 42We are calculating the area of rectangle by two objects.
Question 7.
1.
#include
2.
using namespace std;
3.
class Box
4.
{
5.
public :
6.
double length;
7.
double breadth;
8.
double height;
9.
};
10.
int main( )
11.
{
12.
Box Box1;
13.
double volume;
14.
Box1.height = 5;
15.
Box1.length = 6;
16.
Box1.breadth = 7.1;
17.
volume = Box1.height * Box1.length * Box1.breadth;
18.
cout
What is the output of the following program?
1.
#include
2.
using namespace std;
3.
class Box
4.
{
5.
public :
6.
double length;
7.
double breadth;
8.
double height;
9.
};
10.
int main( )
11.
{
12.
Box Box1;
13.
double volume;
14.
Box1.height = 5;
15.
Box1.length = 6;
16.
Box1.breadth = 7.1;
17.
volume = Box1.height * Box1.length * Box1.breadth;
18.
cout
Explanation:-
Answer: Option B. -> 213In the above program, we are calculating the area of the cube by using the cube formula
Output:
$ g++ obj1.cpp
$ a.out
213