私はコードを持っています:
#include <iostream>
#include <cstdlib>
using namespace std;
class Animal{
private:
int age;
public:
Animal() : age(1) {}
void toString(){
cout << "Age: " << age << endl;
}
};
class Cat : public Animal
{
public:
Cat() : age(5) {}
/*
void toString(){
cout << "Age: " << age << endl;
}*/
private:
int age;
};
int main(){
Cat tom;
tom.toString();
system("pause");
return 0;
}
しかし、プログラムを実行すると、tom変数の age は 5 ではなく 1 です。toStringはage変数を読み取ることができませんか? /* */ Cat クラスの toString メソッドを開くと、年齢は 5 になります。
(私の英語はあまり上手ではありません。ありがとう)