我做了一個父類和一個繼承類。主要是當我給繼承類的對象時會出現一個錯誤,例如「對象不可訪問」。這裏是我的代碼C++幫助類繼承
#include <iostream>
#include <string>
using namespace std;
class Parents{
protected:
int age;
string name;
public:
void getInfo(int hAge, string hName)
{
name = hName;
age = hAge;
cout << "Their son name is " << name << endl;
cout << "He is " << age << " years old" << endl;
cout << "His hair is red" << endl;
cout << "He is a boy" << endl;
}
};
class Son : public Parents{
Son(){
name = "John";
age = 25;
}
};
int main(){
Son boy; //object not accessible
system("pause");
return 0;
}
你需要構造公共 –
還存在另一個問題。當我打電話給boy.getInfo()時,它表示調用的函數太少。爲什麼 – user3264250
顯示你怎麼做 –