Here is a listing of C++ programming questions on “Class Hierarchies and Abstract Classes” along with answers, explanations and/or solutions:
1. How many kinds of classes are there in c++?
a) 1
b) 2
c) 3
d) 4
Answer: b
Clarification: There are two kinds of classes in c++. They are absolute class and the concrete class.
2. What is meant by polymorphism?
a) class having many forms
b) class having only single form
c) class having two forms
d) class having four forms
Answer: a
Clarification: Polymorphism is literally meant class having many forms.
3. How many types of inheritance are there in c++?
a) 2
b) 3
c) 4
d) 5
Answer: d
Clarification: There are five types of inheritance in c++. They are single, Multiple, Hierarchical, Multilevel, Hybrid.
4. What will be the output of the following C++ code?
-
#include
-
using namespace std;
-
class stu
-
{
-
protected:
-
int rno;
-
public:
-
void get_no(int a)
-
{
-
rno = a;
-
}
-
void put_no(void)
-
{
-
}
-
};
-
class test:public stu
-
{
-
protected:
-
float part1,part2;
-
public:
-
void get_mark(float x, float y)
-
{
-
part1 = x;
-
part2 = y;
-
}
-
void put_marks()
-
{
-
}
-
};
-
class sports
-
{
-
protected:
-
float score;
-
public:
-
void getscore(float s)
-
{
-
score = s;
-
}
-
void putscore(void)
-
{
-
}
-
};
-
class result: public test, public sports
-
{
-
float total;
-
public:
-
void display(void);
-
};
-
void result::display(void)
-
{
-
total = part1 + part2 + score;
-
put_no();
-
put_marks();
-
putscore();
-
cout << "Total Score=" << total << "n";
-
}
-
int main()
-
{
-
result stu;
-
stu.get_no(123);
-
stu.get_mark(27.5, 33.0);
-
stu.getscore(6.0);
-
stu.display();
-
return 0;
-
}
a) 66.5
b) 64.5
c) 62.5
d) 60.5
Answer: a
Clarification: In this program, We are passing the values by using different methods and totaling the marks to get the result.
Output:
$ g++ class.cpp $ a.out Total Score=66.5
5. What will be the output of the following C++ code?
-
#include
-
using namespace std;
-
class poly
-
{
-
protected:
-
int width, height;
-
public:
-
void set_values(int a, int b)
-
{
-
width = a; height = b;
-
}
-
};
-
class Coutput
-
{
-
public:
-
void output(int i);
-
};
-
void Coutput::output(int i)
-
{
-
cout << i;
-
}
-
class rect:public poly, public Coutput
-
{
-
public:
-
int area()
-
{
-
return(width * height);
-
}
-
};
-
class tri:public poly, public Coutput
-
{
-
public:
-
int area()
-
{
-
return(width * height / 2);
-
}
-
};
-
int main()
-
{
-
rect rect;
-
tri trgl;
-
rect.set_values(3, 4);
-
trgl.set_values(4, 5);
-
rect.output(rect.area());
-
trgl.output(trgl.area());
-
return 0;
-
}
a) 1212
b) 1210
c) 1010
d) 1250
Answer: b
Clarification: In this program, We are calculating the area of rectangle and triangle by using multilevel inheritance.
$ g++ class1.cpp $ a.out 1210