c++面向对象程序设计 谭浩强 第五章答案
1:
#include <iostream>
using namespace std;
class Student
{public:
void get_value()
{cin>>num>>name>>sex;}
void display( )
{cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;}
private :
int num;
char name[10];
char sex;
};
class Student1: public Student
{public:
void get_value_1()
{get_value();
cin>>age>>addr;}
void display_1()
{ cout<<"age: "<<age<<endl; //引用派生类的私有成员,正确。
cout<<"address: "<<addr<<endl;} //引用派生类的私有成员,正确。
private:
int age;
char addr[30];
};
int main()
{Student1 stud1;
stud1.get_value_1();
stud1.display();
stud1.display_1();
return 0;
}
2:
#include <iostream>
using namespace std;
class Student
{public:
void get_value()
{cin>>num>>name>>sex;}
void display( )
{cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;}
private :
int num;
char name[10];
char sex;
};
class Student1: private Student
{public:
void get_value_1()
{get_value();
cin>>age>>addr;}
void display_1()
{display();
cout<<"age: "<<age<<endl; //引用派生类的私有成员,正确。
cout<<"address: "<<addr<<endl;} //引用派生类的私有成员,正确。
private:
int age;
char addr[30];
};
int main()
{Student1 stud1;
stud1.get_value_1();
stud1.display_1();
return 0;
}
3:
#include <iostream>
using namespace std;
class Student //声明基类
{public: //基类公用成员
void get_value();
void display( );
protected : //基类保护成员
int num;
char name[10];
char sex;
};
void Student::get_value()
{cin>>num>>name>>sex;}
void Student::display( )
{cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;
}
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<<"num: "<<num<<endl; //引用基类的保护成员
cout<<"name: "<<name<<endl; //引用基类的保护成员
cout<<"sex: "<<sex<<endl; //引用基类的保护成员
cout<<"age: "<<age<<endl; //引用派生类的私有成员
cout<<"address: "<<addr<<endl; //引用派生类的私有成员
}
int main( )
{Student1 stud1; //stud1是派生类student1类的对象
stud1.get_value_1(); //调用派生类对象stud1的公用成员函数
stud1.display1( ); //调用派生类对象stud1的公用成员函数
return 0;
}
4: 解法一
#include <iostream>
using namespace std;
class Student//声明基类
{public: //基类公用成员
void get_value();
void display( );
protected : //基类保护成员
int num;
char name[10];
char sex;
};
void Student::get_value()
{cin>>num>>name>>sex;}
void Student::display( )
{cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;
}
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<<"num: "<<num<<endl; //引用基类的保护成员,合法
cout<<"name: "<<name<<endl; //引用基类的保护成员,合法
cout<<"sex: "<<sex<<endl; //引用基类的保护成员,合法
cout<<"age: "<<age<<endl; //引用派生类的私有成员,合法
cout<<"address: "<<addr<<endl; //引用派生类的私有成员,合法
}
int main( )
{Student1 stud1; //stud1是派生类student1类的对象
stud1.get_value_1(); //调用派生类对象stud1的公用成员函数get_value_1
stud1.display1( ); //调用派生类对象stud1的公用成员函数display1
return 0;
}
解法二
#include <iostream>
using namespace std;
class Student //声明基类
{public: //基类公用成员
void get_value();
void display( );
protected : //基类保护成员
int num;
char name[10];
char sex;
};
void Student::get_value()
{cin>>num>>name>>sex;}
void Student::display( )
{cout<<"num: "<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
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<<"age:"<<age<<endl;
cout<<"address:"<<addr<<endl;
}
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 //C为B的公用派生类
{public:
void f4();
private:
int p;
};
int main()
{A a1; //a1是基类A的对象
B b1; //b1是派生类B的对象
C//c1是派生类C的对象
return 0;
}
6:
#include <iostream>
using namespace std;
class A
{public:
void f1( );
protected:
void f2();
private:
int i;
};
class B: public A
{public:
void f3( );
int k;
private:
int m;
};
class C: protected B
{public:
void f4();
protected:
int n;
private:
int p;
};
class D: private C
{public:
void f5();
protected:
int q;
private:
int r;
};
int main()
{A a1;
B b1;
C c1;
D d1;
return 0;
}
7:
#include <iostream>
using namespace std;
class A
{
public:
A(){a=0;b=0;}
A(int i){a=i;b=0;}
A(int i,int j){a=i;b=j;}
void display(){cout<<"a="<<a<<" b="<<b;}
private:
int a;
int b;
};
class B : public A
{
public:
B(){c=0;}
B(int i):A(i){c=0;}
B(int i,int j):A(i,j){c=0;}
B(int i,int j,int k):A(i,j){c=k;}
void display1()
{display();
cout<<" c="<<c<<endl;
}
private:
int c;
};
int main()
{ B b1;
B b2(1);
B b3(1,3);
B b4(1,3,5);
b1.display1();
b2.display1();
b3.display1();
b4.display1();
return 0;
}
8:
#include <iostream>
using namespace std;
class A
{
public:
A(){cout<<"constructing A "<<endl;}
~A(){cout<<"destructing A "<<endl;}
};
class B : public A
{
public:
B(){cout<<"constructing B "<<endl;}
~B(){cout<<"destructing B "<<endl;}
};
class C : public B
{
public:
C(){cout<<"constructing C "<<endl;}
~C(){cout<<"destructing C "<<endl;}
};
int main()
{ C c1;
return 0;
}
9:
#include<string>
#include <iostream>
using namespace std;
class Teacher
{public:
Teacher(string nam,int a,char s,string tit,string ad,string t);
void display();
protected:
string name;
int age;
char sex;
string title;
string addr;
string tel;
};
Teacher::Teacher(string nam,int a,char s,string tit,string ad,string t):
name(nam),age(a),sex(s),title(tit),addr(ad),tel(t){ }
void Teacher::display()
{cout<<"name:"<<name<<endl;
cout<<"age"<<age<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"title:"<<title<<endl;
cout<<"address:"<<addr<<endl;
cout<<"tel:"<<tel<<endl;
}
class Cadre
{public:
Cadre(string nam,int a,char s,string p,string ad,string t);
void display();
protected:
string name;
int age;
char sex;
string post;
string addr;
string tel;
};
Cadre::Cadre(string nam,int a,char s,string p,string ad,string t):
name(nam),age(a),sex(s),post(p),addr(ad),tel(t){}
void Cadre::display()
{cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"post:"<<post<<endl;
cout<<"address:"<<addr<<endl;
cout<<"tel:"<<tel<<endl;
}
class Teacher_Cadre:public Teacher,public Cadre
{public:
Teacher_Cadre(string nam,int a,char s,string tit,string p,string ad,string t,float w);
void show( );
private:
float wage;
};
Teacher_Cadre::Teacher_Cadre(string nam,int a,char s,string t,string p,string ad,string tel,float w):
Teacher(nam,a,s,t,ad,tel),Cadre(nam,a,s,p,ad,tel),wage(w) {}
void Teacher_Cadre::show( )
{Teacher::display();
cout<<"post:"<<Cadre::post<<endl;
cout<<"wages:"<<wage<<endl;
}
int main( )
{Teacher_Cadre te_ca("Wang-li",50,'f',"prof.","president","135 Beijing Road,Shanghai","(021)61234567",1534.5);
te_ca.show( );
return 0;
}
10:
#include <iostream>
#include <cstring>
using namespace std;
class Teacher //教师类
{public:
Teacher(int,char [],char); //声明构造函数
void display(); //声明输出函数
private:
int num;
char name[20];
char sex;
};
Teacher::Teacher(int n,char nam[],char s) //定义构造函数
{num=n;
strcpy(name,nam);
sex=s;
}
void Teacher::display() //定义输出函数
{cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
class BirthDate //生日类
{public:
BirthDate(int,int,int); //声明构造函数
void display(); //声明输出函数
void change(int,int,int); //声明修改函数
private:
int year;
int month;
int day;
};
BirthDate::BirthDate(int y,int m,int d) //定义构造函数
{year=y;
month=m;
day=d;
}
void BirthDate::display() //定义输出函数
{cout<<"birthday:"<<month<<"/"<<day<<"/"<<year<<endl;}
void BirthDate::change(int y,int m,int d) //定义修改函数
{year=y;
month=m;
day=d;
}
class Professor:public Teacher //教授类
{public:
Professor(int,char [],char,int,int,int,float); //声明构造函数
void display(); //声明输出函数
void change(int,int,int); //声明修改函数
private:
float area;
BirthDate birthday; //定义BirthDate类的对象作为数据成员
};
Professor::Professor(int n,char nam[20],char s,int y,int m,int d,float a):
Teacher(n,nam,s),birthday(y,m,d),area(a){ } //定义构造函数
void Professor::display() //定义输出函数
{Teacher::display();
birthday.display();
cout<<"area:"<<area<<endl;
}
void Professor::change(int y,int m,int d) //定义修改函数
{birthday.change(y,m,d);
}
int main()
{Professor prof1(3012,"Zhang",'f',1949,10,1,125.4); //定义Professor对象prof1
cout<<endl<<"original data:"<<endl;
prof1.display(); //调用prof1对象的display函数
cout<<endl<<"new data:"<<endl;
prof1.change(1950,6,1); //调用prof1对象的change函数
prof1.display(); //调用prof1对象的display函数
return 0;
}
c++面向对象程序设计 谭浩强 第五章答案的更多相关文章
- c++面向对象程序设计 谭浩强 第三章答案
2: #include <iostream> using namespace std; class Date {public: Date(int,int,int); Date(int,in ...
- c++面向对象程序设计 谭浩强 第一章答案
c++面向对象程序设计 谭浩强 答案 第一章 目录: c++面向对象程序设计 谭浩强 答案 第一章 c++面向对象程序设计 谭浩强 答案 第二章 c++面向对象程序设计 谭浩强 答案 第三章 c++面 ...
- c++面向对象程序设计 谭浩强 第二章答案
类体内定义成员函数 #include <iostream> using namespace std; class Time { public: void set_time(); void ...
- c++面向对象程序设计 课后题 答案 谭浩强 第四章
c++面向对象程序设计课后题答案 谭浩强 第四章 1: #include <iostream> using namespace std; class Complex {public: Co ...
- C语言程序设计·谭浩强(第四版)第二章课后习题的答案,算法——程序的灵魂
C语言程序小练习 1.用C语言设计程序算出1-1/2+1/3-14+1/5...+1/99-1/100的值 #include<stdio.h> int main() { ; double ...
- 关于指针的笔记【1】【C语言程序设计-谭浩强】
指针是什么? 一个 变量的地址称为该变量的"指针"[将地址形象化的称为“指针”].(指针是什么百度百科) 注意区分储存单元的地址和内容这两个概念的区别. 直接访问:直接按变量名进行 ...
- C程序设计(谭浩强)第五版课后题答案 第一章
大家好,这篇文章分享了C程序设计(谭浩强)第五版课后题答案,所有程序已经测试能够正常运行,如果小伙伴发现有错误的的地方,欢迎留言告诉我,我会及时改正!感谢大家的观看!!! 1.什么是程序?什么是程序设 ...
- 挂羊头卖狗肉蓄意欺骗读者——谭浩强《C程序设计(第四版)》中所谓的“按照C99”(二)
挂羊头卖狗肉蓄意欺骗读者——谭浩强<C程序设计(第四版)>中所谓的“按照C99”(二) 在<谭C>p4:“本书的叙述以C99标准为依据”,下面从C89到C99的主要变化方面来看 ...
- 再论谭浩强《C语言程序设计》
一些同学学不好C语言,把罪责归于“因为教材是谭浩强写的”实在是很滑稽. 谭浩强老先生 1934 年生,现在已经 80 岁了.他 1958 年从清华大学自动控制系毕业,那年 24 岁.要知道 C 语言那 ...
随机推荐
- MVP演化论
本文是翻译MVP: Model-View-Presenter The Taligent Programming Model for C++ and Java(Mike Potel)文章的摘要.该文介绍 ...
- MacType 缺字问题【转】
- 三维点集拟合:平面拟合、RANSAC、ICP算法
ACM算法分类:http://www.kuqin.com/algorithm/20080229/4071.html 一: 拟合一个平面:使用SVD分解,代码里面去找吧 空间平面方程的一般表达式为: A ...
- 杭电1019 Least Common Multiple【求最小公倍数】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1019 解题思路:lcm(a,b)=a*b/gcd(a,b) 反思:最开始提交的时候WA,以为是溢出了, ...
- 关于《Python核心编程》第2版和第3版
关于<Python核心编程>第2版和第3版 以前开始学Python的时候,根据某大神的建议买了本<Python核心编程>第2版,慢慢学习.而最近回家没带书回来,刚好JD有活动, ...
- RabbitMQ学习之集群模式
由于RabbitMQ是用erlang开发的,RabbitMQ完全依赖Erlang的Cluster,因为erlang天生就是一门分布式语言,集群非常方便,但其本身并不支持负载均衡.Erlang的集群中各 ...
- Multitier architecture-n-tier architecture
In software engineering, multitier architecture (often referred to as n-tier architecture) or multil ...
- C# dataGridView1 添加数据 和清空数据
#region MyRegion DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn(); DataGridViewTextBo ...
- POI操作Excel的三种Workbook的发展和区别
POI的一些使用方法: 创建流程:(上级为下级的载体) 1.创建Workbook(工作薄): 2.创建Sheet(表单,可以创建多个): 3.创建Row(行): 4.创建Cell(单元格) 接下来分别 ...
- Python爬虫3-----浏览器伪装
1.浏览器伪装技术原理 当爬取CSDN博客时,会发现返回403,因为对方服务器会对爬虫进行屏蔽,故需伪装成浏览器才能爬取.浏览器伪装一般通过报头进行. 2.获取网页的报头 3.代码: import u ...