VisualC++面向对象编程教程第2版(王育坚)清华大学出版社课后答案

上传人:gbs****77 文档编号:9292735 上传时间:2020-04-04 格式:DOC 页数:104 大小:264.50KB
返回 下载 相关 举报
VisualC++面向对象编程教程第2版(王育坚)清华大学出版社课后答案_第1页
第1页 / 共104页
VisualC++面向对象编程教程第2版(王育坚)清华大学出版社课后答案_第2页
第2页 / 共104页
VisualC++面向对象编程教程第2版(王育坚)清华大学出版社课后答案_第3页
第3页 / 共104页
点击查看更多>>
资源描述
2 45 编写一个程序 输入三角形的三条边的边长 求三角形的面积 2 45 include include int main int a b c int s S cin a b c s a b c 2 S powl s s a s b s c 0 5 if S 0 cout 此三边不能构成三角形 endl else cout 面积 endl cout S endl return 0 2 46 从键盘输入一个大写字母 然后改用小写字母在屏幕输出 2 46 include int main char ch int a cout 输入一个大写字母 ch if ch 65 a int ch cout ch endl else cout 该字母不是字母或不是字母大写 endl return 0 2 47 用户输入两个整数 编程输出稍大于第一个整数而又是第 2 个整数的倍数的数 计算 公式是 valuel value2 value1 value2 2 47 include int main int value1 value2 int result cin value1 value2 result value1 value2 value1 value2 cout 该整数为 result endl return 0 2 48 华氏温度转换为摄氏温度的公式是 C F 32 5 9 编写一个程序 输入一个华氏 温度 程序输出相应的摄氏温度 请将 32 和 5 9 用 const 型变量表示 2 48 include int main const float i 32 0 const float j 5 0 9 0 float degFahr float degCel cin degFahr for degFahr 0 degFahr 300 degFahr 10 degCel j degFahr i cout 华氏温度 degFahr endl cout 摄氏温度 degCel endl return 0 2 49 从键盘输入 20 个整数 检查 100 是否存在于这些整数中 若是的话 求出他是第几 个被输入的 2 49 include int main int Array 20 int i count 0 c 0 for i 0 i Array i for i 0 i 20 i if Array i 100 count if Array i 100 c if c 19 cout 100 不存在该数组中 endl for i 0 i 20 i if Array i 100 cout 它是第 i 1 个被输入的 endl return 0 2 50 从键盘输入一个 N X N 的整型数组 并将每一行的最大值显示输出 2 50 include int main int Array 100 100 0 int n temp cin n for int i 0 i n i for int j 0 j Array i j for int x 0 x n x for int y x 1 y n y if Array i x Array i y temp Array i x Array i x Array i y Array i y temp cout 每一行的最大值 endl for i 0 i n i cout Array i 0 cout endl 2 51 输入三个整数 采用指针方法将三个数按从大到小的顺序输出 2 51 include include int main int Array 3 int pA Array int a Array int i int temp for i 0 i Array i for i 0 i 3 i a if pA a temp pA pA a a temp a pA a for i 0 i 3 i cout Array i return 0 2 52 采用指针方法将一个数组中的所有元素颠倒顺序 结果仍然存放在原来的数组中 要 求使用最少的辅助存储单元 2 52 include int main int n cout 输入数组的长度 小于 100 n cout 输入 n 个数 endl int Array 100 int pA Array int i for i 0 i Array i pA for i 0 i n i pA cout pA return 0 2 53 输入两个字符串 如果两个字符串的字符和长度都相同 认为它们相等 在屏幕上 输出 Equal 否则在屏幕上输出 Unequal 要求使用字符指针 2 53 include int main char pstr Enter a string char str1 100 str2 100 cout pstr endl cin get str1 100 cin get cin get str2 100 int len1 0 len2 0 while str1 len1 0 len1 while str2 len2 0 len2 if len1 len2 cout Unequal endl else if len1 len2 int i 0 for i 0 i len1 i if str1 i str2 i if i len1 1 cout Equal endl else cout Unequal endl return 0 2 54 编程将一个整数转换成对应的数字串 例如将值 1234 转换为数字串 1234 2 54 include int main int number int Array 100 int i j cin number for i 0 number 0 i Array i number 10 number number 10 j i for i j i 0 i cout Array i 1 cout endl return 0 2 55 编程求两个复数的和 2 55 include int main float a 2 b 2 c 2 int i for i 0 i a i for i 0 i b i for i 0 i 2 i c i a i b i cout c 0 c 1 endl return 0 2 56 使用结构变量表示每个学生的信息 姓名 学号和三门课的成绩 从键盘输入 10 个 学生的数据 然后输出每个学生的姓名和三门课的平均成绩 2 56 include struct student char num 10 char name 20 float grade 3 float average int main student stu 10 int i j float sum 0 for i 0 i 9 i printf Enter num n scanf s printf Enter name n scanf s printf Enter three grades n for j 0 j 3 j scanf f sum stu i grade j stu i average sum 3 printf f s n stu i average stu i name return 0 2 57 用结构数组建立并初始化一个工资表 然后输入一个人的姓名 查询其工资情况 并 在屏幕上输出 2 57 2 58 用枚举值 MON TUE WED THU FRI SAT 和 SUN 表示一个星期中的 7 天 从 键盘输入一个 0 6 之间的整数 根据输入的整数输出对应的英文缩写 2 58 2 59 编写一个求解一元二次方程的根的程序 方程的系数由用户输入 2 59 include include int main int a b c int R float p float x1 x2 cin a b c R b b 4 a c if R 0 p sqrt R x1 b p 2 a x2 b p 2 a if x1 x2 cout 仅且只有一个根 x1 endl else cout 有两个不同的根 x1 x2 endl else if R 0 cout 无根 endl return 0 2 60 从键盘输入一个字符 判断输入的字符是 m a n 或其他字符 如果是 m 则输出 Good morning 如果是 a 则输出 Good afternoon 如果是 n 则输出 Good night 如果是其他字符则输出 I can t undersrand 2 60 2 61 编程实现两个整数的加 减 乘 除四则运算 运算式形如 32 120 2 61 2 62 编写一个程序 利用 swith 语句将百分制的学生成绩转换为优 良 中 及格和不及 格 5 分制成绩 2 62 2 63 从键盘输入一个字符 判断输入的字符是数字 空格还是其他字符 并给出相应的提 示信息 2 63 include int main char ch cout 请输入一个字符 cin get ch if ch cout 这是一个空格 0 else cout 这是一个其他字符 endl return 0 2 64 从键盘输入一个字符序列 编程统计其中的数字个数和英文字母个数 输入的字符序 列以 作为结束符 2 64 include int main char symbol 100 int i end j 0 k 0 cin symbol for i 0 i 100 i if symbol i end i break for i 0 i 0 else k cout digit j endl cout letter k endl return 0 2 65 输入一个由若干单词组成的文本串 每个单词之间用一些空格分隔 统计此文本串单 词的个数 2 65 include int main char str 1000 int count 1 cin get str 1000 while str 0 for int i 0 str i 0 i if str i cout the count is count endl return 0 2 67 编程求 值 使用如下公式 4 1 1 3 1 5 1 7 直到最后一项的绝对值小于 10 6 为止 2 67 1 include include int main int i n 1 j 1 double s 0 for i 0 2 i 1 pow 10 6 i cout i endl cout cout 2 i 1 endl cout cout pow 2 i 1 1 endl cout s pow 2 i 1 1 n j 2 i 1 n n 1 cout i endl cout 4 s endl return 0 2 67 2 include include int main float i 0 double s 0 for i 0 2 i 1 pow 10 6 i s pow 1 i 1 2 i 1 cout s 4 endl return 0 2 68 把 100 150 之间不能被 3 整除的数输出 要求一行输出 10 个数 2 68 2 69 编程输出一个九九乘法表 2 69 include int main char table 9 9 int i j X Y for i 0 i 9 i for j 0 j Y cout X Y cout endl return 0 2 70 编程计算整型数各位数字之和 例如数 2007 各位数字之和为 2 0 0 7 9 2 70 include int main int num int sum 0 i cin num for i 0 num 0 i sum num 10 num num 10 cout sum sum endl return 0 2 71 输入 n 个整数 利用冒泡排序法将它们从小到大排列 并在屏幕上输出 2 71 include int main int n int i j temp int num 100 cin n for i 0 i num i for i 0 i n i for j i 1 j n j if num i num j temp num i num i num j num j temp for i 0 i n i cout num i cout endl return 0 2 72 编程求出从键盘输入的 10 个数之和 遇到负数时终止输入求和 2 72 include int main int Array 10 int sum 0 for int i 0 i Array i if Array i 0 sum Array i else break cout sum endl return 0 2 73 编程求出从键盘输入的 10 个数中所有正数之和 负数不参加求和 2 73 include int main int num 10 int i sum 0 for i 0 i num i if num i 0 sum num i cout sum is sum endl return 0 2 74 设计函数 prime 它只带一个整型参数 当这个参数的值是素数时 该函数返回非 0 否则返回 0 利用这个函数编写一个程序来验证哥德巴赫猜想 任何一个大于 2 的偶数都 可以表示成两个素数之和 2 74 include int prime int x int i for i 2 i a if a 2 j a j b prime j if b j c a b d prime c if d c cout 该定理成立 endl else cout 此数不是大于 2 的偶数 endl return 0 2 75 编制如下函数原型的函数 int index const char str char c 这个函数返回字符串 str 中第一次出现字符 c 的位置 2 75 include int index const char str char c int count 1 int Ccount 0 for int i 0 str i 0 i if str i c break count return count int main char string 100 char C int V 0 int result cin string cin C while string V 0 V result index string C if V 1 result cout 此字符不在字符串中 endl else cout result endl return 0 2 76 首先编写以下函数声明的函数 void swap float px float py 该函数用于交换两个实 型变量的值 然后编写一个主函数验证函数 swap 的功能 2 76 include void swap float px float py float temp temp px px py py temp int main float X Y cin X Y swap cout 使用 swap 函数的值 cout X Y endl float temp temp X X Y Y temp cout 验证值 X Y endl return 0 2 77 定义将一个字符串反转的函数 例如将字符串 abcd 反转为 dcba 2 77 include include int Array char s int j n char string 100 strcpy string s for j 0 j str z Array str cout 0 i cout str i cout endl return 0 2 78 首先编写一个冒泡排序函数 然后在主函数中调用排序函数对 10 个整数从小到达进 行排序 提示 采用数组名作为函数参数 2 78 2 79 将习题 2 76 中的 swap 函数改为内联函数 并实现相同的程序功能 2 79 include inline void swap float px float py float temp temp px px py py temp int main float X Y cin X Y swap cout 使用 swap 函数的值 cout X Y endl float temp temp X X Y Y temp cout 验证值 X Y endl return 0 2 80 编写一个函数 maxmin 该函数有两个实型参数 执行函数后 第一个参数为两个 参数中值较大者 第二个参数为较小者 要求使用引用作为函数参数 并编写主函数验证 函数功能 2 80 include void maxmin int if A X Y maxmin X Y cout X Y endl return 0 2 81 编写一个函数 swapstruct 实现交换两个结构变量的功能 编写主函数验证函数 swapstruct 的功能 要求使用引用传递参数 2 81 include include struct student char name 20 int score void swapstruct student char s 20 strcpy s s1 name strcpy s1 name s2 name strcpy s2 name s temp s1 score s1 score s2 score s2 score temp int main student stu1 zhangsan 90 student stu2 lisi 85 cout stu1 stu1 name stu1 score endl cout stu2 stu2 name stu2 score endl swapstruct stu1 stu2 cout stu1 stu1 name stu1 score endl cout stu2 stu2 name stu2 score endl return 0 2 82 编写一个程序 在主函数 main 的外部和内部分别声明两个同名的整型变量并赋值 然后在主函数 main 中分别访问两个变量 2 82 include int i 3 int main cout i endl int i 5 cout i endl return 0 2 83 一个程序由两个 C 源文件组成 在一个源文件中定义主函数 main 并声明一个外部 整型变量 n 在另一个源文件中定义一个不带参数的函数 factorial void 该函数用于计算变 量 n 的阶乘 编程在主函数 main 中输入一个整数并求它的阶乘 2 83 include int factorial int x int y pro 1 for y x y 0 y pro y return pro void main int n R cout n R factorial n cout The product is R endl 2 84 采用外部函数的方式使用习题 2 75 中的函数 index 即在一个源文件中定义该函数 然后在另一个源文件中调用该函数 2 84 2 85 编写一段程序 利用 new 运算动态分配 float 型 long 型和 char 型三个内存单元 将 它们的首地址分别赋给指针 pf pl 和 pc 给这些存储但愿赋值 并在屏幕上显示它们的值 最后利用 delete 运算释放所有动态分配的内存单元 2 85 include int main float pf new float pf 3 14 long pl new long pl 2135567889 char pc new char pc A cout pf endl cout pl endl cout pc endl delete pf delete pl delete pc return 0 2 86 编写一个程序 用 new 运算为一个整型数组动态分配内存空间 对其进行赋值 并在 屏幕上输出 2 86 include int main int size int num cin size int f new int size for int i 0 i num f i num for i 0 i size i cout f i delete f return 0 2 87 采用动态内存分配方法设计一个学生成绩处理程序 要求输入任何数量学生的学号 姓名和四门课的成绩 并按平均成绩的高低输出每个学生的高低输出每个学生的姓名和成 绩 2 87 include include struct student int ID char name 20 int grade 4 float average int main int num int i j k temp float sum 0 char s 20 cin num student stu new student num for i 0 i num i cout 输入第 i 1 人的信息 endl cout stu i ID cout stu i name cout grade for j 0 j stu i grade j sum stu i grade j stu i average sum 4 sum 0 cout average stu i average endl for i 0 i num i 排序 for j i 1 j num j if stu i average stu j average strcpy s stu i name strcpy stu i name stu j name strcpy stu j name s for k 0 k 4 k temp stu i grade k stu i grade k stu j grade k stu j grade k temp cout 成绩排名 endl cout name grade endl for i 0 i num i cout stu i name for j 0 j 4 j cout stu i grade j cout endl return 0 2 88 输入一行字符 建立一个链表 链表的每一个结点含有一个输入的字符 通过访问链 表中的每个结点计算链表中结点的总个数 2 88 2 89 使用带参数的宏定义计算两个实数之和 并编写主函数验证宏的功能 2 89 include define sum a b a b int main int x y cin x y cout 主函数计算结果 x y endl cout 宏定义函数结果 sum x y endl return 0 2 90 定义一个带参数的宏 求出三个数中最大的一个数 并进行验证 2 90 include define MAX a b a b a b void main int x y z cin x y z cout 宏定义参数结果 cout MAX MAX x y z y if x z cout 验证结果为 x endl else cout 验证结果为 z z cout 验证结果为 y endl else cout 验证结果为 z endl 2 91 输入一个字符串 根据需要设置条件编译 使之能将输入的字符串以大写字母的形式 或小写字母的形式输出 2 91 include int main char str 100 char X int i cin str cout 是否输出大写形式 Y or N X if X Y for i 0 str i 0 i str i str i 32 cout str i else if X N for i 0 str i 0 i cout str i cout endl return 0 2 92 修改习题 2 61 中的求和程序 在程序中定义一个调试宏 利用条件编译指令编译不 同的代码段 使得在调试程序时能够输出一些调试信息 2 92 2 93 假设有三个文件 test13 h test2 h 和 test cpp 在 test1 h 中定义了一个宏 PI test2 h 文件包含了 test1 h 文件 而 test cpp 文件又包含了 test1 h 文件和 test2 h 文件 请问编译时 会出现什么错误 如何解决 提示 宏的重复定义 2 93 3 44 一个名为 CPerson 的类有如下属性 姓名 身份证号 性别和年龄 请用 C 语言定义 这个类 并为上述属性定义相应的方法 include include class CPerson private char Name 10 char ID 20 char Sex 4 int Age public CPerson char na char id char se int ag void Show CPerson CPerson char na char id char se int ag strcpy Name na strcpy ID id strcpy Sex se Age ag void CPerson Show cout 姓名 Name endl cout 身份证 ID endl cout 性别 Sex endl cout 年龄 Age endl void main CPerson person 王三 610102198506041952 男 21 person Show 3 45 设计一个日期类 Date 该类用于表示日期值 年 月 日 要求除了能够通过相应 得成 员函数设置和获取日期外 还能够实现将日期加一天的操作 include class date int year int month int day bool flag public date year 0 month 0 day 0 date int yr int mo int da void setdate int getyear int getmonth int getday void addday void show date date int yr int mo int da flag false if mo 1 month mo day da else flag true void date setdate cout 请输入年分 year cout 请输入月份 1 12 month while month12 cout 输入有误 请重新输入月份 1 12 month cout 请输入日期 1 31 day while day31 cout 输入有误 请重新输入日期 1 31 day flag false void date show if flag cout year month day endl else cout 日期设置有误 不能输出 29 若是闰年的二月当 Day 大于 29 时 Day 1 Mon 增加一 个月 day 1 month else if day 28 若不是闰年的二月当 Day 大于 28 时 Day 1 Mon 增加 一个月 day 1 month else if month 1 month 3 month 5 month 7 month 8 month 10 month 12 if day 31 若不是二月月大时 Day 1 Mon 增加一个月 day 1 month else if day 30 若不是二月月小时 Day 1 Mon 增加一个月 day 1 month if month 12 若月大于 12 则 Mon 1 Year 增加一年 month 1 year void main date d1 1999 5 30 d1 show d1 setdate d1 show cout 日期增加一天后为 d1 addday d1 show 3 46 include using namespace std class CRectangle private double X double Y double length double width public CRectangle double s double e double l double w X s Y e length l width w CRectangle void Move double double void Size double double void Where void Area void CRectangle Move double x double y cout 矩形按向量 x y 移动 endl cout 现在矩形左上角所在的位置 endl cout X x Y y endl void CRectangle Size double cl double cw cout 要更改的长和宽 cl cw length cl width cw cout length width endl void CRectangle Where cout 现在矩形左上角所在的位置 endl cout X Y endl void CRectangle Area double area area length width cout area int main CRectangle cr 2 3 5 4 cr Where cr Area cr Move 1 2 cr Size 2 3 return 0 3 47 include using namespace std class CRectangle private double X double Y double length double width public CRectangle double s double e double l double w X s Y e length l width w CRectangle void Move double double void Size double double void Where void Area void CRectangle Move double x double y cout 矩形按向量 x y 移动 endl cout 现在矩形左上角所在的位置 endl cout X x Y y endl void CRectangle Size double cl double cw cout 要更改的长和宽 cl cw length cl width cw cout length width endl void CRectangle Where cout 现在矩形左上角所在的位置 endl cout X Y endl void CRectangle Area double area area length width cout area int main CRectangle cr 2 3 5 4 cr Where cr Area cr Move 1 2 cr Size 2 3 return 0 3 48 include include class Bank private char number 20 int money public void setnumber char num int mon strcpy number num money mon void changer int mon money mon void show cout 当前的总钱数为 money endl void main char num 20 int money int addmoney cout 输入用户名和存入的钱 num money Bank bank bank setnumber num money bank show cout 输入转账的钱 addmoney bank changer addmoney bank show 3 49 include using namespace std class Product private char name 20 double price int m count public Product Product void setproduct void sellproduct void show void Product setproduct int n cout 输入生产产品数量 n m count n void Product sellproduct int n cout 输入销售产品数量 n m count n void Product show cout 输出剩余产品数量 endl cout m count endl int main Product p p setproduct p sellproduct p show return 0 3 50 建立一个名为 Student 的类 该类有以下几个私有成员变量 学生姓名 学号 性别 和年龄 还有以下两个成员函数 一个用于初始化学生姓名 学号 性别和年龄的构造函 数 一个用于输出学生信息的函数 编写一个主函数 声明一个学生对象 然后调用成员 函数在屏幕上输出学生信息 include include class Student public Student char name char num char sex int age Student void show private char Name char Num char Sex int Age Student Student char name char num char sex int age Name new char strlen name 1 注意字符串的赋值 strcpy Name name Num new char strlen num 1 strcpy Num num Sex new char strlen sex 1 strcpy Sex sex Age age Student Student delete Name delete Num delete Sex void Student show cout 姓名 Name endl cout 学号 Num endl cout 性别 Sex endl cout 年龄 Age endl void main Student student 张三 0401011201 男 18 student show 3 51 设计一个 CPetrol 类 包含以下几个私有成员 90 号 93 号 98 号汽油加油量和单价 当 天的总收入 类还包含以下几个成员函数 设置有关数据成员的构造函数 输入加油量并 计算总收入的成员函数 利用类编写主函数 假设加油站某天 90 93 98 号汽油单价分别为 3 96 4 05 4 38 计算并输出加油站一天的收入 include using namespace std class CPetrol public CPetrol void setamount double total private double am 90 double am 93 double am 98 double price 90 double price 93 double price 98 CPetrol CPetrol price 90 3 96 price 93 4 05 price 98 4 38 void CPetrol setamount cout input three amounts am 90 am 93 am 98 total am 90 price 90 am 93 price 93 am 98 price 98 void main CPetrol c c setamount cout The total is c total endl 3 52 修改习题 3 50 中的类 Student 添加一个静态成员变量 用于表示已创建对象的数量 添加两个静态成员函数 一个用于输出已创建对象的数量 一个用于输出一个学生的姓名 和 学号 include include class Student public Student char name char num char sex int age Student staticvoid show Student staticvoid showstudentnum private char Name char Num char Sex int Age static int studentnum int Student studentnum 0 Student Student char name char num char sex int age studentnum Name new char strlen name 1 strcpy Name name Num new char strlen num 1 strcpy Num num Sex new char strlen sex 1 strcpy Sex sex Age age Student Student delete Name delete Sex void Student showstudentnum cout 学生的数量是 studentnum endl void Student show Student cout 学号 a Num endl cout 性别 a Sex endl cout 年龄 a Age endl void main Student student1 张三 0401011201 男 18 Student show student1 注意调用方式 静态成员可以通过类名调用 Student showstudentnum 注意调用方式 Student student2 李四 0401011202 男 18 Student show student2 Student showstudentnum 3 53 编写程序用静态成员的方法实现对班费的管理 要求定义一个类 Student 除了声明一个存 放班费的静态成员 还要分别定义一个上交班费的成员函数 Contribute 花费班费的成 员函数 Spend 和显示班费的静态成员函数 Display student cpp include using namespace std class Student private static double fee fee 班费 静态成员数据 public Student 默认构造函数 析构函数 Student void Contribute double n n 上缴的班费数额 void Spend double n n 花费班费数量 static void Display 静态成员函数 double Student fee 0 类声明外面对静态数据成员初始化 类方法 void Student Contribute double n fee fee n void Student Spend double n if fee n cout 班费不够 请求失败 n else fee fee n void Student Display cout 现有班费 fee endl int main Student stu stu Display stu Contribute 103 4 交钱 stu Display stu Spend 42 3 花钱 stu Display return 0 放在了一个文件里了 上面是类声明 下面是测试小程序 运行过了 没问题 3 54 定义一个类 A 该类除了有两个数据成员 x y 外 还有一个对象备份函数 copy copy 函数的功能说明如下 对于类 A 的对象 a1 和 a2 函数调用 a1 copy a2 表示将对象 a2 赋值给对象 a1 提示利用 this 指针防止一个对象对自己赋值 include using namespace std class Test private char a b public Test a new char 100 b new char 100 Test delete a delete b Test Test int len strlen B a delete a a new char len 1 strcpy a B a len strlen B b delete b b new char len 1 strcpy b B b return this void mytest char str1 char str2 strcpy a str1 strcpy b str2 void myprint cout a b endl A B int main B mytest this is B B myprint A copy B A myprint return 0 3 55 将习题 3 45 中类 Date 的 日期加一天 成员函数改为友员函数 include class date int year int month int day bool flag public date year 0 month 0 day 0 date int yr int mo int da void setdate int getyear int getmonth int getday friend void addday date void show date date int yr int mo int da flag false if mo 1 month mo day da else flag true void date setdate cout 请输入年分 year cout 请输入月份 1 12 month while month12 cout 输入有误 请重新输入月份 1 12 month cout 请输入日期 1 31 day while day31 cout 输入有误 请重新输入日期 1 31 day flag false void date show if flag cout year month day endl else cout 日期设置有误 不能输出 29 若是闰年的二月当 Day 大于 29 时 Day 1 Mon 增加一 个月 d day 1 d month else if d day 28 若不是闰年的二月当 Day 大于 28 时 Day 1 Mon 增加 一个月 d day 1 d month else if d month 1 d month 3 d month 5 d month 7 d month 8 d month 10 d month 12 if d day 31 若不是二月月大时 Day 1 Mon 增加一个月 d day 1 d month else if d day 30 若不是二月月小时 Day 1 Mon 增加一个月 d day 1 d month if d month 12 若月大于 12 则 Mon 1 Year 增加一年 d month 1 d year void main date d1 1999 5 30 d1 show d1 setdate d1 show cout 日期增加一天后为 addday d1 d1 show 3 56 将习题 3 50 中类 Student 的学生信息输出函数改为友员函数 include include class Student public Student char name char num char sex int age Student friend void show Student private char Name char Num char Sex int Age Student Student char name char num char sex int age Name new char strlen name 1 strcpy Name name Num new char strlen num 1 strcpy Num num Sex new char strlen sex 1 strcpy Sex sex Age age 注意字符串的赋值 Student Student delete Name delete Sex void show Student cout 学号 stu Num endl cout 性别 stu Sex endl cout 年龄 stu Age endl void main Student student 张三 0401011201 男 18 show student 3 57 设计一个直线类 Line 设直线方程为 ax bx c 0 其中 包含三个数据成员 a b c 一 个显示数据成员的 disp 成员函数和一个求两直线交点的友元函数 setpoint 并在 main 中给 出对象或数据进行测试 请填空完成程序并上机运行验证 include class Line friend void setpoint Line private double a double b double c public Line double a1 double b1 double c1 a a1 b b1 c c1 Line void setpoint Line if A a B a A b B b x 100 while 1 if A c A a x A b B c B a x B b 0 00001 break x x 0 00001 cout x A c A a x A b else cout error endl void main Line A 2 1 4 B 1 10 3 setpoint A B 3 58 include include class Student private char name 10 char id 20 char sex 20 int age public Student char pname 10 char pid 20 char psex 5 int page Student void show const Student Student char pname 10 char pid 20 char psex 5 int page strcpy name pname strcpy id pid strcpy sex psex age page void Student show const cout 姓名 name endl 学号 id endl 性别 sex endl 年龄 age endl int main const Student student1 jane 200931106013 famle 20 student1 show 通过常对象只能调用常函数 Student student2 jane2 200931106013 famle 20 student2 show 通过普通对象也能调用常成员函数 return 0 3 59 include class CPoint private float x float y public CPoint float xx 0 float yy 0 CPoint float Getx float Gety CPoint CPoint float xx float yy x xx y yy CPoint CPoint floa
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!