#include"iostream"
#include"string"
using namespace std;
class father
{
public:
father(string s,int a)
{name=s;tell=a;print();cout<<"離開基類建構式"<<endl;};
~father(){cout<<"\n釋放基類物件\n";}
void print(){cout<<name<<"身高為: "<<tell<<endl;}
protected:
string name;int tell;
};
class son:public father
{
public:
son(string a,int b,int c);
~son(){cout<<"\n釋放子類物件\n"<<endl;}
void print1();
private:
int weight;
};
son::son(string a,int b,int c)
{
name=a;
tell=b;
cout<<"在子類建構式中"<<endl;
weight=c;
}/**/
void son::print1()//這里為什么要加 void
{
father::print();
cout<<"體重: "<<weight<<endl;
}
int main()
{
son a("Mike",180,70);
//a.print1();
cout<<"結束啦"<<endl;
return 0;
}
uj5u.com熱心網友回復:
'father' : no appropriate default constructor availableuj5u.com熱心網友回復:
1.子類在呼叫建構式時需要先呼叫父類的建構式從你的父類也就是father類來看father類中沒有默認也就是father();的實作
所以在子類建構式中需要顯式呼叫father的建構式
如下:
son::son(string a, int b, int c):father(a,b)
{
name = a;
tell = b;
cout << "在子類建構式中" << endl;
weight = c;
}/**/
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/83433.html
標籤:基礎類
