云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 答案-c++面向对象程序设计课后习题答案(谭浩强版)

答案-c++面向对象程序设计课后习题答案(谭浩强版)

  • 62 次阅读
  • 3 次下载
  • 2025/5/1 9:30:15

cout<<\ cout<<\ }

class Student1: protected Student //声明一个保护派生类 {public:

void get_value_1(); void display1( ); private:

int age; char addr[30]; };

void Student1::get_value_1() {get_value();

cin>>age>>addr; }

void Student1::display1( )

{cout<<\ cout<<\ cout<<\ cout<<\ cout<<\ }

int main( )

{Student1 stud1; stud1.get_value_1(); stud1.display1( ); return 0; }

4: 解法一

#include using namespace std; class Student//声明基类

{public: void get_value(); void display( );

protected : int num;

char name[10]; char sex; };

//引用基类的保护成员 //引用基类的保护成员 //引用基类的保护成员 //引用派生类的私有成员 //引用派生类的私有成员 //stud1是派生类student1类的对象 //调用派生类对象stud1的公用成员函数 //调用派生类对象stud1的公用成员函数 //基类公用成员 //基类保护成员 void Student::get_value() {cin>>num>>name>>sex;}

void Student::display( )

{cout<<\ cout<<\ cout<<\ }

class Student1: public Student {public:

void get_value_1(); void display1( ); private:

int age; char addr[30]; };

void Student1::get_value_1() {get_value();

cin>>age>>addr; }

void Student1::display1( )

{cout<<\ cout<<\ cout<<\ cout<<\ cout<<\ }

int main( )

{Student1 stud1; stud1.get_value_1(); get_value_1

stud1.display1( ); return 0; }

解法二

#include using namespace std;

class Student {public: void get_value(); void display( );

//声明一个公用派生类 //引用基类的保护成员,合法

//引用基类的保护成员,合法 //引用基类的保护成员,合法 //引用派生类的私有成员,合法 //引用派生类的私有成员,合法 //stud1是派生类student1类的对象

//调用派生类对象stud1的公用成员函数 //调用派生类对象stud1的公用成员函数display1 //声明基类

//基类公用成员 protected : //基类保护成员 int num;

char name[10]; char sex; };

void Student::get_value() {cin>>num>>name>>sex;}

void Student::display( )

{cout<<\ cout<<\ cout<<\ }

class Student1: protected Student //声明一个公用派生类 {public:

void get_value_1(); void display1( ); private:

int age; char addr[30]; };

void Student1::get_value_1() {cin>>age>>addr;}

void Student1::display1( ) {cout<<\

cout<<\ }

int main( )

{Student1 stud1; //stud1是派生类student1类的对象 stud1.get_value(); stud1.get_value_1(); stud1.display( );

stud1.display1(); //合法。display1是派生类中的公用成员函数 return 0; } 5:

class A //A为基类 {public: void f1( );

int i; protected: void f2(); int j; private: int k; };

class B: public A //B为A的公用派生类 {public: void f3( ); protected: int m; private: int n; };

class C: public B {public: void f4(); private: int p; };

int main()

{A a1; B b1; C//c1是派生类C的对象 return 0; }

6:

#include using namespace std; class A {public: void f1( ); protected: void f2(); private: int i; };

class B: public A {public:

//C为B的公用派生类 //a1是基类A的对象 //b1是派生类B的对象

  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

cout<<\ cout<<\ } class Student1: protected Student //声明一个保护派生类 {public: void get_value_1(); void display1( ); private: int age; char addr[30]; }; void Student1::get_value_1() {get_value(); cin>>age>>addr; } void Student1::display1( ) {cout<<\ cout<<\ cout<<\ cout<<\

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com