c ++ 继承_C ++继承能力问题和解答

c ++ 继承

1) Which concept of OOP creates a hierarchy of classes?

1)哪种OOP概念可以创建类的层次结构?

  1. Enumeration

    枚举

  2. Abstraction

    抽象化

  3. Inheritance

    遗产

  4. None of the above

    以上都不是

Answer 回答

Correct Answer - 3

正确答案-3

Inheritance

遗产

2) What is the syntax of creating Inheritance in C++?

2)在C ++中创建继承的语法是什么?

  1. class <child class name> extends <parent class name>

    类<子类名称>扩展<父类名称>

  2. class <child class name>: <parent class name>

    类<子类名称>:<父类名称>

  3. class <child class name>: access specifier <parent class name>

    类<子类名称>:访问说明符<父类名称>

  4. class <child class name>: <parent class name> access specifier

    类<子类名称>:<父类名称>访问说明符

Answer 回答

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)用于派生一个类的说明符是多少?

  1. 1

    1个

  2. 2

    2

  3. 3

    3

  4. 4

    4

Answer 回答

Correct Answer - 3

正确答案-3

4 (They are private, protected and public.)

4 (它们是私有的,受保护的和公开的。)

4) What is multiple inheritance?

4)什么是多重继承?

  1. Creating multiple classes

    创建多个类

  2. Deriving a derived class from more than one base class

    从多个基类派生派生类

  3. Deriving a derived class from base class

    从基类派生派生类

  4. Deriving a base class from derived class

    从派生类派生基类

Answer 回答

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;
}

  1. 1

    1个

  2. 12

    12

  3. ERROR

    错误

  4. Segmentation fault

    分段故障

Answer 回答

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;
}

  1. Inherited

    遗传

  2. Compile-time Error

    编译时错误

  3. Runtime Error

    运行时错误

  4. None of the mentioned

    没有提到

Answer 回答

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 ++ 继承

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值