版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、3-44 一个名为CPerson 的类有如下属性:姓名、身份证号、性别和年龄,请用 C+语言定义这个类,并为上述属性定义相应的方法。#include<iostream.h>#include<string.h>class CPersonprivate: char Name10; char ID20; char Sex4; int Age;public: CPerson(char *na,char *id,char *se,int ag); void Show();CPerson:CPerson(char *na,char *id,char *se,int ag)strcp
2、y(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() person.Show()
3、;3-45 设计一个日期类 Date,该类用于表示日期值(年、月、日)。要求除了能够通过相应得成员函数设置和获取日期外,还能够实现将日期加一天的操作。#include<iostream.h>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()
4、;date:date(int yr,int mo,int da) flag=false; if(mo>=1&&mo<=12&&da>=1&&da<=31) year=yr; month=mo; day=da; else flag=true; void date:setdate() cout<<"请输入年分"<<endl; cin>>year; cout<<"请输入月份(1-12)"<<endl; cin>>mon
5、th; while(month<1|month>12) cout<<"输入有误,请重新输入月份(1-12)"<<endl; cin>>month; cout<<"请输入日期(1-31)"<<endl; cin>>day; while(day<1|day>31) cout<<"输入有误,请重新输入日期(1-31)"<<endl; cin>>day; flag=false;void date:show() i
6、f(!flag) cout<<year<<'/'<<month<<'/'<<day<<endl; else cout<<"日期设置有误,不能输出"<<endl;int date:getyear() return year;int date:getmonth() return month;int date:getday() return day;void date:addday() day+; if(month=2)/判断是否是二月 bool lea
7、pyear; leapyear=(year%4=0&&year%100!=0)|(year%400=0);/定义闰年 if(leapyear) if(day>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)/若
8、不是二月月大时,Day=1,Mon 增加一个月 day=1; month+; elseif(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<iostre
9、am>using namespace std;class CRectangleprivate: 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
10、, double y)cout<<"矩形按向量("<<x<<","<<y<<")移动"<<endl; cout<<"现在矩形左上角所在的位置:"<<endl;cout<<"("<<X+x<<","<<Y+y<<")"<<endl;void CRectangle:Size(double cl,
11、 double cw)cout<<"要更改的长和宽:"<<cl<<cw;length=cl;width=cw;cout<<length*width<<endl;void CRectangle:Where()cout<<"现在矩形左上角所在的位置:"<<endl;cout<<"("<<X<<","<<Y<<")"<<endl;void CRec
12、tangle: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<iostream>using namespace std;class CRectangleprivate: double X; double Y;double length;double width;public: CRectangle(double s,
13、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<&
14、lt;"现在矩形左上角所在的位置:"<<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 C
15、Rectangle: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
16、,2);cr.Size(2,3);return 0;3-48#include<iostream.h>#include<string.h>class Bankprivate:char number20;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 mai
17、n()char num20;int money;int addmoney;cout<<"输入用户名和存入的钱:"<<endl; cin>>num>>money;Bank bank;bank.setnumber(num,money);bank.show();cout<<"输入转账的钱:"<<endl;cin>>addmoney; bank.changer(addmoney);bank.show();3-49#include<iostream>using name
18、space std;class Productprivate:char name20;double price;int m_count;public:Product()Product()void setproduct();void sellproduct();void show();void Product:setproduct()int n; cout<<"输入生产产品数量:"<<endl;cin>>n;m_count=n;void Product:sellproduct()int n;cout<<"输入销售产品数
19、量:"<<endl;cin>>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 的类,该类有以下几个私有成员变量:学生姓名、学号、性别和年龄。还有以下两个成员函数:一个用于初始化学生姓名、学号、性别和年龄的构造函数,一
20、个用于输出学生信息的函数。编写一个主函数,声明一个学生对象,然后调用成员函数在屏幕上输出学生信息。#include<iostream.h>#include <string.h>class Studentpublic: 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)
21、Name=new charstrlen(name)+1;/注意字符串的赋值strcpy(Name,name);Num=new charstrlen(num)+1;strcpy(Num,num);Sex=new charstrlen(sex)+1;strcpy(Sex,sex);Age=age;Student:Student() delete Name; delete Num; delete Sex;void Student:show() cout<<"姓名:"<<Name<<endl; cout<<"学号:"
22、;<<Num<<endl; cout<<"性别:"<<Sex<<endl; cout<<"年龄:"<<Age<<endl;void main() Student student("张三","0401011201","男",18); student.show();3-51设计一个CPetrol类,包含以下几个私有成员:90号 93号 98 号汽油加油量和单价,当天的总收入。类还包含以下几个成员函数:设置
23、有关数据成员的构造函数,输入加油量并计算总收入的成员函数。利用类编写主函数:假设加油站某天90 93 98 号汽油单价分别为3.96 4.05 4.38 计算并输出加油站一天的收入。#include<iostream>using namespace std;class CPetrolpublic: CPetrol(); void setamount(); double total;private: double am_90; double am_93; double am_98; double price_90; double price_93; double price_98;C
24、Petrol:CPetrol() price_90=3.96; price_93=4.05; price_98=4.38;void CPetrol:setamount() cout<<"input three amounts!"<<endl; cin>>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 t
25、otal is"<<c.total<<endl;3-52修改习题 3-50 中的类 Student,添加一个静态成员变量,用于表示已创建对象的数量;添加两个静态成员函数,一个用于输出已创建对象的数量,一个用于输出一个学生的姓名和学号。#include<iostream.h>#include <string.h>class Studentpublic: Student(char *name,char *num,char *sex,int age); Student(); staticvoid show(Student &a); s
26、taticvoid 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 charstrlen(name)+1;strcpy(Name,name);Num=new charstrlen(num)+1;strcpy(Num,num);Sex=new charstrlen(
27、sex)+1;strcpy(Sex,sex);Age=age;Student:Student() delete Name; delete Sex;void Student:showstudentnum() cout<<"学生的数量是:"<<studentnum<<endl;void Student:show(Student &a) cout<<"姓名:"<<a.Name<<endl; cout<<"学号:"<<a.Num<&l
28、t;endl; cout<<"性别:"<<a.Sex<<endl; cout<<"年龄:"<<a.Age<<endl;void main() Student student1("张三","0401011201","男",18); Student:show(student1);/注意调用方式,静态成员可以通过类名调用 Student:showstudentnum(); /注意调用方式 Student student2(&quo
29、t;李四","0401011202","男",18); Student:show(student2); Student:showstudentnum();3-53编写程序用静态成员的方法实现对班费的管理,要求定义一个类Student,除了声明一个存放班费的静态成员,还要分别定义一个上交班费的成员函数Contribute(),花费班费的成员函数Spend()和显示班费的静态成员函数Display()/student.cpp #include<iostream> using namespace std; class Student pr
30、ivate: 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
31、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; 放在了一个文件里了,上面是类声明,下面是测试小程序,运行过了
32、,没问题3-54定义一个类A,该类除了有两个数据成员x,y外,还有一个对象备份函数copy。copy函数的功能说明如下:对于类A的对象a1和a2,函数调用a1.copy(a2)表示将对象a2赋值给对象a1.(提示利用this指针防止一个对象对自己赋值)#include<iostream>using namespace std;class Testprivate: char *a,*b;public: Test() a = new char100; b = new char100; Test() delete a; delete b; Test ©(Test &B)
33、 if(this = &B) return *this; int len = strlen(B.a); delete a; a = new charlen+1; strcpy(a,B.a); len = strlen(B.b); delete b; b = new charlen+1; strcpy(b,B.b); return *this; void mytest(char *str1,char *str2) strcpy(a,str1); strcpy(b,str2); void myprint() cout << a << " " &l
34、t;< 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<iostream.h>class date int year; int month; int day; bool flag;public: date() year=0;month=0;day=0;date(int yr,int
35、 mo,int da);void setdate();int getyear();int getmonth();int getday();friend void addday(date &d);void show();date:date(int yr,int mo,int da) flag=false; if(mo>=1&&mo<=12&&da>=1&&da<=31) year=yr; month=mo; day=da; else flag=true; void date:setdate() cout<<
36、;"请输入年分"<<endl; cin>>year; cout<<"请输入月份(1-12)"<<endl; cin>>month; while(month<1|month>12) cout<<"输入有误,请重新输入月份(1-12)"<<endl; cin>>month; cout<<"请输入日期(1-31)"<<endl; cin>>day; while(day<1|
37、day>31) cout<<"输入有误,请重新输入日期(1-31)"<<endl; cin>>day; flag=false;void date:show()if(!flag) cout<<year<<'/'<<month<<'/'<<day<<endl;else cout<<"日期设置有误,不能输出"<<endl;int date:getyear() return year;int d
38、ate:getmonth() return month;int date:getday() return day;void addday(date &d) d.day+; if(d.month=2)/判断是否是二月 bool leapyear; leapyear=(d.year%4=0&&d.year%100!=0)|(d.year%400=0);/定义闰年 if(leapyear) if(d.day>29)/若是闰年的二月当 Day 大于 29 时,Day=1,Mon 增加一个月 d.day=1; d.month+; else if(d.day>28)/若
39、不是闰年的二月当 Day 大于 28 时,Day=1,Mon 增加一个月 d.day=1; d.month+; elseif(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+;elseif(d.day>30)/若不是二月月小时,Day=1,Mon 增加一个月 d.day=1; d.month+;if(d.month>12)/若月大于 12 则 Mon=1,Year 增加一
40、年 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<iostream.h>#include <string.h>class Studentpublic: Student(char *name,char *num,char *sex,int
41、 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 charstrlen(name)+1;strcpy(Name,name);Num=new charstrlen(num)+1;strcpy(Num,num);Sex=new charstrlen(sex)+1;strcpy(Sex,sex);Age=age;/注意字符串
42、的赋值Student:Student() delete Name; delete Sex;void show(Student &stu) cout<<"姓名:"<<stu.Name<<endl; cout<<"学号:"<<stu.Num<<endl; cout<<"性别:"<<stu.Sex<<endl; cout<<"年龄:"<<stu.Age<<endl;voi
43、d main() Student student("张三","0401011201","男",18); show(student);3-57设计一个直线类Line(设直线方程为ax+bx+c=0),其中,包含三个数据成员a、b、c,一个显示数据成员的disp成员函数和一个求两直线交点的友元函数setpoint,并在main()中给出对象或数据进行测试。请填空完成程序并上机运行验证。#include<iostream.h> class Line friend void setpoint(Line &A,Line &a
44、mp;B);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 &A,Line &B) double x;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<<" "&l
45、t;<(-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<iostream.h>#include<string.h>class Studentprivate:char name10;char id20;char sex20;int age;public:Student(char pname10,char pid20,char psex5,int page);
46、Student()void show()const;Student:Student(char pname10,char pid20,char psex5,int page)strcpy(name,pname);strcpy(id,pid);strcpy(sex,psex);age=page;void Student:show() constcout<<"姓名:"<<name<<endl<<"学号:"<<id<<endl<<"性别:"<<se
47、x<<endl<<"年龄:"<<age<<endl;int main()const Student student1("jane","200931106013","famle",20);student1.show(); /通过常对象只能调用常函数 Student student2("jane2","200931106013","famle",20);student2.show(); /通过普通对象也能调用常成
48、员函数 return 0;3-59#include<iostream.h>class CPointprivate: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()float CPoint:Getx()return x;float CPoint:Gety()return y;class CLine:public CPointprivate:f
49、loat x1;float y1;public:CLine(float a,float b,float c,float d);CLine();float Getxx();float Getyy();CLine:CLine(float a,float b,float c,float d):CPoint(a,b)x1=c;y1=d;CLine:CLine()float CLine:Getxx()return x1;float CLine:Getyy()return y1;int main()CLine p(1,2,3,4);cout<<"输出CPoint的坐标:"c
50、out<<"("<<p.Getx()<<","<<p.Gety()<<")"<<endl;cout<<"输出CLine的坐标:"cout<<"("<<p.Getxx()<<","<<p.Getyy()<<")"<<endl; return 0;3-60定义一个哺乳动物Mammal类,再由此派生出狗Dog
51、类,定义一个Dog类的对象,编写主函数测试它们并观察基类与派生类的构造函数与析构函数的调用顺序。 提示:Mammal类可以定义属性 Age, Weight等。 Dog类可以再增加自己的属性如color。 注意数据成员的访问属性#include <iostream.h>class mammal public: mammal() cout<<"it is a mammal"<<endl; void age() cout<<"mammal age"<<endl; void weight() cout&
52、lt;<"mammal weight"<<endl; mammal() cout<<"destory mammal"<<endl; ; class dog:public mammal public:dog() cout<<"it is a dog"<<endl; void weight() cout<<"dogs weight"<<endl; dog() cout<<"destory dog class&
53、quot;<<endl; ; void main() dog dg; 3-61#include<iostream.h>#include<string.h>class Vehiclepublic:char xh20,prod20,color20,chspeed20;int cweight,speed,oil,peonum,pweight;public: Vehicle(char *,char *,char *,char *,int ,int ,int ,int ,int ); Vehicle();Vehicle:Vehicle(char *x,char *p,
54、char *co,char *ch,int cw,int s,int o,int pe,int pw) strcpy(xh,x); strcpy(prod,p); strcpy(color,co); strcpy(chspeed,ch); cweight=cw; speed=s; oil=o; peonum=pe; pweight=pw;class Car : public Vehiclepublic:Car(char *x,char *p,char *co,char *ch,int cw,int s,int o,int pe,int pw):Vehicle(x,p,co,ch,cw,s,o,
55、pe,pw)void showcar();void Car : showcar()cout<<"小汽车型号是:"<<xh<<endl<<"生产商是:"<<prod<<endl<<"车重是:"<<cweight<<endl<<"颜色是:"<<color<<endl<<"最高时速是:"<<speed<<endl<&l
56、t;"耗油量为:"<<oil<<endl<<"载客数为:"<<peonum<<endl<<"在重量为:"<<pweight<<endl<<"变速箱类行为:"<<chspeed<<endl;class Truck : public Vehiclepublic :Truck(char *x,char *p,char *co,char *ch,int cw,int s,int o,int pe,int pw):Ve
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 那曲地区农村信用社联合社秋季校园招聘笔试备考题库(浓缩500题)及答案详解(基础+提升)
- 葫芦岛市农村信用社联合社秋季校园招聘笔试备考题库(浓缩500题)含答案详解(突破训练)
- 荣昌县农村信用社联合社秋季校园招聘笔试备考题库(浓缩500题)含答案详解(达标题)
- 上海市农村信用社联合社秋季校园招聘笔试备考题库(浓缩500题)含答案详解(预热题)
- 黔东南苗族侗族自治州农村信用社联合社秋季校园招聘笔试备考题库(浓缩500题)含答案详解(培优b卷)
- 梅州市农村信用社联合社秋季校园招聘笔试备考题库(浓缩500题)及答案详解(名师系列)
- 2026年临夏州农村信用社联合社秋季校园招聘笔试备考题库(浓缩500题)附答案详解(考试直接用)
- 家庭装修合同范本3篇
- 2026年黔南布依族苗族自治州农村信用社联合社秋季校园招聘笔试备考题库(浓缩500题)含答案详解(满分必刷)
- 广西壮族自治区农村信用社联合社秋季校园招聘笔试备考题库(浓缩500题)含答案详解(基础题)
- 物理期中达标测试练习卷-2025-2026学年物理八年级上学期(沪科版2024)
- 2025年低压电工操作证复审必考试题及答案
- 刑法故意杀人罪课件
- 【MOOC】人工智能原理-北京大学 中国大学慕课MOOC答案
- DGJ 08-20-2019 住宅设计标准
- 2023年08月广东海珠区工商联雇员1人笔试历年高频考点试题含答案带详解
- 第4课京剧唱腔联奏(课件)人音版音乐六年级上册
- 人行道栏杆计算
- 临床各类引流管的固定及规范管理
- 【面部除皱术】SMAS技术和FAME除皱术
- 制造业信息化现状与未来
评论
0/150
提交评论