C++ 的 继承
等级 4216 次按键
Dog 继承自 Animal,因此 Dog 对象除了自己的 bark 方法外,还能使用继承来的 name 成员和 speak 方法。
C++ 代码
// 带有 name 和 speak 方法的基类1class Animal {2public:3 string name; // 输出名字,后接 " speaks"4 void speak() { cout << name << " speaks" << endl; }5};6 // Dog 以 public 方式派生自 Animal,并继承其公有成员7class Dog : public Animal {8public: // 只有 Dog 才有的方法9 void bark() { cout << "Woof!" << endl; }10};11 12Dog d;// name 继承自 Animal13d.name = "Rex";// 继承的方法:输出 Rex speaks14d.speak();// Dog 自己的方法:输出 Woof!15d.bark();运行结果
Rex speaks Woof!
可以用这段代码练习打字。成绩不会计入排行榜。
这段代码中的符号
这段 C++ 代码中需要输入的符号及其出现次数。
<× 10;× 9"× 6(× 4)× 4{× 4}× 4:× 3.× 3!× 1