这怎么给出输出?

问题描述 投票:0回答:1
#include <iostream>
using namespace std;

// first base class
class Vehicle
{
public:
    Vehicle() { cout << "This is a Vehicle\n"; }
};

// second base class
class FourWheeler
{
public:
    FourWheeler()
    {
        cout << "This is a 4 wheeler Vehicle\n";
    }
};

// sub class derived from two base classes
class Car : private Vehicle, private FourWheeler
{
};

// main function
int main()
{
    // Creating object of sub class will
    // invoke the constructor of base classes.
    Car obj;
    return 0;
}

我原以为不会有输出,但即使在子类为私有的情况下,它也会提供输出(即它运行超类的构造函数)。如何 ??请赐教。

c++ class inheritance constructor multiple-inheritance
1个回答
0
投票

Private用于描述类的访问控制。无论私有或公共继承,继承的类都将运行各自的构造函数。

Private 应该用于定义您不希望暴露给类外部的方法和函数的方法和函数。它并不是为了隐藏或锁定功能。

© www.soinside.com 2019 - 2024. All rights reserved.