【发布时间】:2020-07-22 08:50:19
【问题描述】:
#include<bits/stdc++.h>
using namespace std;
class Vehicle
{
private:
int speed;
public:
Vehicle(int x)
{
speed=x;
cout << "Vehicle's parametrized constructor called" << endl;
}
};
class Car : public Vehicle
{
public:
Car() : Vehicle(5)
{
cout << "Car's constructor called" << endl;
}
};
int main()
{
Car a;
}
输出-
车辆的参数化构造函数调用
汽车的构造函数称为
由于访问说明符是公开的,因此速度不会被继承。由于 Car 中没有 speed 成员,5 被分配到什么位置?
【问题讨论】:
-
“由于访问说明符是公开的,所以速度不会被继承”...什么?我建议您阅读更多关于继承的内容,派生类包含其基类的所有成员。
-
私有属性永远不会被继承。据我所知
-
他们肯定是遗传的。但是,您不能从派生类访问它们。
-
privatevsprotected指定访问成员,但不禁止他们存在 -
@jamesgem 私有成员是继承的,除非你是声明它们的类的朋友,否则你不能引用它们。
标签: c++ inheritance constructor public