MCQs
We are getting the number and copying it to j and printing it.
Output:
$ g++ obj2.cpp
$ a.out
10
None.
While using private member, you cant access it variable.
None.
Because a class may contain any number of objects according to it’s compliance.
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.
}
We are calculating the area of rectangle by two objects.
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
In the above program, we are calculating the area of the cube by using the cube formula
Output:
$ g++ obj1.cpp
$ a.out
213
Because of the access given to the private and protected, We can’t access them by using
direct member access operator.
Objects in the method can be accessed using direct member access operator which is (.).
In class only all the listed items except class will be declared.