Classes & Objects
- What is the output of this program?
#include <iostream>
using namespace std;
class Polygon
{
protected:
int W, H;
public:
void set_values(int width, int height)
{
W = width; H = height;
}
};
class Coutput
{
public:
void output(int i);
};
void Coutput::output(int i)
{
cout << i <<" ";
}
class Rectangle:public Polygon, public Coutput
{
public:
int area()
{
return(W * H);
}
};
class Triangle:public Polygon, public Coutput
{
public:
int area()
{
return(W * H / 2);
}
};
int main()
{
Rectangle Rect;
Triangle trgl;
Rect.set_values(5, 2);
trgl.set_values(5, 2);
Rect.output(Rect.area());
trgl.output(trgl.area());
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
In this program, We are calculating the area of rectangle and
triangle by using multilevel inheritance.
- What is meant by container ship?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
class contains objects of other class types as its members
- How many types of the constructor are there in C++?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
There are three types of constructor in C++. They are the Default constructor, Parameterized constructor, Copy constructor.
- How many constructors can present in a class?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
There can be multiple constructors of the same class, provided they have different signatures.
- What should be the name of the constructor?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
same as the class