c ++ 继承
1) Which concept of OOP creates a hierarchy of classes?
1)哪种OOP概念可以创建类的层次结构?
Correct Answer - 3
正确答案-3
Inheritance
遗产
2) What is the syntax of creating Inheritance in C++?
2)在C ++中创建继承的语法是什么?
class <child class name>: access specifier <parent class name>
class <child class name>: <parent class name> access specifier
Correct Answer - 3
正确答案-3
class <child class name>: access specifier <parent class name>
类<子类名称>:访问说明符<父类名称>
3) How many specifiers are used in to derived a class?
3)用于派生一个类的说明符是多少?
Correct Answer - 3
正确答案-3
4 (They are private, protected and public.)
4 (它们是私有的,受保护的和公开的。)
4) What is multiple inheritance?
4)什么是多重继承?
Correct Answer - 2
正确答案-2
Deriving a derived class from more than one base class
从多个基类派生派生类
5) What will be the output of the following code?
5)以下代码的输出是什么?
#include <iostream>
#include <string>
using namespace std;
class A
{
int a, b;
float d;
public:
void change(int i){
a = i;
}
void value_of_a(){
cout<<a;
}
};
class B: private A
{
};
int main(int argc, char const *argv[])
{
B b;
cout<<sizeof(B);
return 0;
}
Correct Answer - 2
正确答案-2
12
12
Explanation:
说明:
Class B is derived from A class and Class A has three members of size 4 bytes then Size of B =4*3=12 Bytes.
B类是从A类派生而来的,A类具有3个大小为4字节的成员,则B的大小为4 * 3 = 12字节。
6) Choose the correct answer (not-output) with the following code.
6)用以下代码选择正确的答案(不输出)。
#include <iostream>
using namespace std;
class polygon {
protected: int width, height;
public: void set_values(int a, int b) {
width = a;
height = b;
}
};
class output1 {
public: void output(int i);
};
void output1::output(int i) {
cout << i << endl;
}
class rectangle: public polygon, public output1 {
public: int area() {
return (width * height);
}
};
class triangle: public polygon, public output1 {
public: int area() {
return (width * height / 2);
}
};
int main() {
rectangle rect;
triangle trgl;
rect.set_values(4, 5);
trgl.set_values(4, 5);
rect.output(rect.area());
trgl.output(trgl.area());
return 0;
}
Correct Answer - 1
正确答案-1
Inherited
遗传
Explanation:
说明:
There is no error (neither compile-time or runtime) in the code, classes polygon and output1 are inheriting on classes rectangle and triangle.
代码中没有错误(编译时或运行时),多边形和output1类是在矩形和三角形类上继承的。
The output would be...
输出将是...
20
10
翻译自: https://www.includehelp.com/cpp-programming/inheritance-aptitude-questions-and-answers.aspx
c ++ 继承