問:派生出的子類為什么會顯示父類沒有建構式呢?
例子如下:
uj5u.com熱心網友回復:
默認建構式是編譯器隱含提供的一個無參建構式,僅當類中沒有定義任何建構式時才有效。但是你自定義了一個有參建構式,在沒有設定默認引數的情況下,子類會尋找父類無參建構式(因為函式多載多載產生的影響)。那怎么呼叫呢?
這里提供兩種方案
第一種:就是上面講的設定默認引數
例如下面的代碼就可以
#include<iostream>
using namespace std;
class father
{
private:
int years;
int height;
public:
father(int years=0,int height=0):years(years),height(height)
{
cout<<"father constructor"<<endl;
}
father(const father & f)
{
this->years=f.years;
this->height=f.height;
cout<<"copy constructor"<<endl;
}
~father()
{
cout<<"destructor"<<endl;
}
void init(int & a,int &b)
{
years=a;
height=b;
cout<<"father init succeed"<<endl;
}
void show()
{
cout<<"father years is "<<years<<" height is "<<height<<endl;
}
void get()
{
cout<<"give son money"<<endl;
}
};
class son:public father
{
public:
void get()
{
cout<<"give money"<<endl;
}
void sum()
{
cout<<"sum"<<endl;
}
};
int main()
{
son s1;
//father * f1=&s1;
//s1.get();
return 0;
}
這是結果圖第二種對子類設定有參構造,并且在其指定父類建構式
與什么的區別是多了這點改變
son(int c,int d):father(100,200)
{
cout<<"son Constructed\n";
}
int main()
{
son s1(1,2);
return 0;
}
結果圖

最后啰嗦一下,我的編譯環境為gcc
uj5u.com熱心網友回復:
額,是上面,不是什么,原諒我的手抖,有什么錯誤或者疑問歡迎一起討論
uj5u.com熱心網友回復:
.感謝感謝你的熱心相助
uj5u.com熱心網友回復:
我在object后面加了一個int weight=0 程式就可以了。
另外,我還是不明白我錯在哪

可以幫我請教一下您嗎
uj5u.com熱心網友回復:
額,你確定你就是這么回復的嗎?

你的代碼有多少個object。。。。。。。。。
請給出代碼區別,謝謝
uj5u.com熱心網友回復:
額,是上面,不是什么,原諒我的手抖,有什么錯誤或者疑問歡迎一起討論
我在object后面加了一個int weight=0 程式就可以了。
另外,我還是不明白我錯在哪
可以幫我請教一下您嗎
額,你確定你就是這么回復的嗎?
你的代碼有多少個object。。。。。。。。。
請給出代碼區別,謝謝
我的錯。不好意思。在第五行。weight=0。他就正常了
uj5u.com熱心網友回復:
額,是上面,不是什么,原諒我的手抖,有什么錯誤或者疑問歡迎一起討論
我在object后面加了一個int weight=0 程式就可以了。
另外,我還是不明白我錯在哪
可以幫我請教一下您嗎
額,你確定你就是這么回復的嗎?
你的代碼有多少個object。。。。。。。。。
請給出代碼區別,謝謝
我的錯。不好意思。在第五行。weight=0。他就正常了
你確定是weight不是we=0嗎?(如果可以,把你代碼發一下,不要圖片)
為什么我的不行?
這是按照你的改動
father(int year,int heigh)
{
years=0;
height=0;
cout<<"father constructor"<<endl;
}
結果一樣error(畢竟個人感覺這是函式體的改變,而計算機是通過函式名進行判斷的)

uj5u.com熱心網友回復:
額,是上面,不是什么,原諒我的手抖,有什么錯誤或者疑問歡迎一起討論
我在object后面加了一個int weight=0 程式就可以了。
另外,我還是不明白我錯在哪
可以幫我請教一下您嗎
額,你確定你就是這么回復的嗎?
你的代碼有多少個object。。。。。。。。。
請給出代碼區別,謝謝
我的錯。不好意思。在第五行。weight=0。他就正常了
你確定是weight不是we=0嗎?(如果可以,把你代碼發一下,不要圖片)
為什么我的不行?
這是按照你的改動
father(int year,int heigh)
{
years=0;
height=0;
cout<<"father constructor"<<endl;
}
結果一樣error(畢竟個人感覺這是函式體的改變,而計算機是通過函式名進行判斷的)
我的代碼:
#include<iostream>
using namespace std;
class object{
private:
int weight;
public:
object(int weight=0):weight(weight) { cout << "object 建構式已經呼叫!" << endl; };
void objectshow(int we) { cout << "object's weight is: " << we << endl; };
~object() { cout << "object 解構式已經呼叫!" << endl; };
};
class box:public object{
public :
box(double h, double wi){
height = h, width = wi;
cout << "box建構式已呼叫!" << endl;};
void boxshow(double h,double wi) { cout << "object's height is: " << h << endl;
cout << "object's width is: " << wi << endl;
};
~box() { cout << "box解構式已經呼叫!" << endl; };
private:
double height;
double width;
};
int main() {
object o(1);
box b(1,1);
return 0;
}
原諒新手的無知和愚蠢,謝謝
uj5u.com熱心網友回復:
額,是上面,不是什么,原諒我的手抖,有什么錯誤或者疑問歡迎一起討論
我在object后面加了一個int weight=0 程式就可以了。
另外,我還是不明白我錯在哪
可以幫我請教一下您嗎
額,你確定你就是這么回復的嗎?
你的代碼有多少個object。。。。。。。。。
請給出代碼區別,謝謝
我的錯。不好意思。在第五行。weight=0。他就正常了
你確定是weight不是we=0嗎?(如果可以,把你代碼發一下,不要圖片)
為什么我的不行?
這是按照你的改動
father(int year,int heigh)
{
years=0;
height=0;
cout<<"father constructor"<<endl;
}
結果一樣error(畢竟個人感覺這是函式體的改變,而計算機是通過函式名進行判斷的)
我的代碼:
#include<iostream>
using namespace std;
class object{
private:
int weight;
public:
object(int weight=0):weight(weight) { cout << "object 建構式已經呼叫!" << endl; };
void objectshow(int we) { cout << "object's weight is: " << we << endl; };
~object() { cout << "object 解構式已經呼叫!" << endl; };
};
class box:public object{
public :
box(double h, double wi){
height = h, width = wi;
cout << "box建構式已呼叫!" << endl;};
void boxshow(double h,double wi) { cout << "object's height is: " << h << endl;
cout << "object's width is: " << wi << endl;
};
~box() { cout << "box解構式已經呼叫!" << endl; };
private:
double height;
double width;
};
int main() {
object o(1);
box b(1,1);
return 0;
}
原諒新手的無知和愚蠢,謝謝
額,這應該就是我說的第一種方法吧
object(int weight=0):weight(weight) { cout << "object 建構式已經呼叫!" << endl; };
通過設定默認引數
(額,這不是愚蠢,這是執著。。。。)(同時需要注意,像這種全部設定了默認引數的constructor函式和無參建構式是不能共存的,會無法識別,不知道選哪一個)
(感覺以后不會回論壇帖,還是問答區劃算
)
uj5u.com熱心網友回復:
額,是上面,不是什么,原諒我的手抖,有什么錯誤或者疑問歡迎一起討論
我在object后面加了一個int weight=0 程式就可以了。
另外,我還是不明白我錯在哪
可以幫我請教一下您嗎
額,你確定你就是這么回復的嗎?
你的代碼有多少個object。。。。。。。。。
請給出代碼區別,謝謝
我的錯。不好意思。在第五行。weight=0。他就正常了
你確定是weight不是we=0嗎?(如果可以,把你代碼發一下,不要圖片)
為什么我的不行?
這是按照你的改動
father(int year,int heigh)
{
years=0;
height=0;
cout<<"father constructor"<<endl;
}
結果一樣error(畢竟個人感覺這是函式體的改變,而計算機是通過函式名進行判斷的)
我的代碼:
#include<iostream>
using namespace std;
class object{
private:
int weight;
public:
object(int weight=0):weight(weight) { cout << "object 建構式已經呼叫!" << endl; };
void objectshow(int we) { cout << "object's weight is: " << we << endl; };
~object() { cout << "object 解構式已經呼叫!" << endl; };
};
class box:public object{
public :
box(double h, double wi){
height = h, width = wi;
cout << "box建構式已呼叫!" << endl;};
void boxshow(double h,double wi) { cout << "object's height is: " << h << endl;
cout << "object's width is: " << wi << endl;
};
~box() { cout << "box解構式已經呼叫!" << endl; };
private:
double height;
double width;
};
int main() {
object o(1);
box b(1,1);
return 0;
}
原諒新手的無知和愚蠢,謝謝
額,這應該就是我說的第一種方法吧
object(int weight=0):weight(weight) { cout << "object 建構式已經呼叫!" << endl; };
通過設定默認引數
(額,這不是愚蠢,這是執著。。。。)(同時需要注意,像這種全部設定了默認引數的constructor函式和無參建構式是不能共存的,會無法識別,不知道選哪一個)
(感覺以后不會回論壇帖,還是問答區劃算)
謝謝大神
感謝感謝感謝感謝
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/22450.html
標籤:基礎類
下一篇:求助
