舉個例子吧:
class base1{
int x;
public:
base1(int a){x = a;}
};
//單繼承
class derived1: public base1{
int a;
derived1(int m, int n): base1(m){ //依次呼叫基類的建構函式,然後再是派生類的建構函式
a = n;
}
class base2{
int t;
base2(int p){t = p;}
//多繼承
class derived2: public base1,public base2{
int k;
derived2(int x, int y, int z): base1(x),base2(y){ //依次呼叫基類建構函式,用逗號隔開,末尾沒有分號
k = z;
舉個例子吧:
class base1{
int x;
public:
base1(int a){x = a;}
};
//單繼承
class derived1: public base1{
int a;
public:
derived1(int m, int n): base1(m){ //依次呼叫基類的建構函式,然後再是派生類的建構函式
a = n;
}
};
class base2{
int t;
public:
base2(int p){t = p;}
};
//多繼承
class derived2: public base1,public base2{
int k;
public:
derived2(int x, int y, int z): base1(x),base2(y){ //依次呼叫基類建構函式,用逗號隔開,末尾沒有分號
k = z;
}
};