[C++] 纯文本查看 复制代码
#include <iostream>
#include <cstring>
using namespace std;
class B_class {
char author[80];
public:
void put_author(char *s) { strcpy(author, s); }
void show_author() { cout << author << endl; }
};
class D_class : public B_class {
char title[80];
public:
void put_title(char *num) { strcpy(title, num); }
void show_title() { cout << "Title: " << title << endl; }
};
int main()
{
B_class *p;
B_class B_ob;
D_class *dp;
D_class D_ob;
p = &B_ob; //基类型对象地址
//通过基类型指针访问 B_class 类型的对象
p->put_author("beyes");
//通过基类型指针访问 D_class 类型对象
p = &D_ob;
p->put_author("Groad");
//输出保存在对象中的每个作者姓名
B_ob.show_author();
D_ob.show_author();
cout << "\n";
dp = &D_ob;
dp->put_title("The killer is not cold");
dp->show_title();
p->show_author(); //可以使用 p 或者 dp 访问 show_author() 函数
return 0;
}