天天看點

C++繼承的例子 (1)

#include <iostream>
#include <cstdio>

using std::cin;
using std::cout;
using std::endl;

class A {
private :
    int a;
public :
    A() : a(100) {
        cout << "無參構造A" << endl;
    }
    A(int k) : a(k) {}
};
class B : public A {
private :
    int b;
public :
    B() : b(900) {}
    void print() {
        //cout << "a : " << a << endl; 因為是 父類A 的私有成員
        cout << "b : " << b << endl;
    }
};
int main() {
    B* pb = new B();
    pb->print();
    return 0;
}
    
      

繼續閱讀