#include<iostream> using namespace std; class Family { protected: string name1; string name2; int age1; int age2; public: Family(string a,string b) { name1 = a; name2 = b; } virtual void Getage() = 0; virtual void working() = 0; }; class Grand:public Family { public: Grand(string a, string b) : name1(a), name2(b) { cout << "Grandpa's name is " << name1 << "," << "Grandma's name is " << name2 << endl; } void Getage(int x, int y) { age1 = x; age2 = y; cout << "Grandpa's age is " << age1 << "," << "Grandma's age is " << age2 << endl; } void working() { cout << "Grandparents' activity is watering the flowers" << endl; } }; int main() { Grand *g = new Grand("tom", "sara"); g->Getage(65, 34); g->working(); return 0; }
时间: 2024-02-10 15:14:02 浏览: 62
vc++2008编译不了#include<iostream.h>头文件
4星 · 用户满意度95%
这是一段 C++ 代码,定义了一个家庭类 Family 和一个继承自 Family 的祖父母类 Grand。在 Family 类中,有父母的姓名和年龄属性,并且定义了两个纯虚函数 Getage 和 working,表示获取年龄和工作。在 Grand 类中,继承了 Family 类,并且实现了 Getage 和 working 函数。在 main 函数中,创建了一个 Grand 类对象 g,并调用了 Getage 和 working 函数来获取祖父母的年龄和工作。
阅读全文