




已阅读5页,还剩15页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
作业1:输入两个变量,输出两个变量的最大值。要求:使用C+语言,使用max函数完成求最大值的操作。#includeint max(int x, int y) return (xy?x:y); int main(int argc, char* argv) int a,b; cout输入两个数,求出最大值: ab; cout最大值为:max(a,b)a; return 0;作业2:(1) 输入下列简单C+程序,完成编译、连接、运行,熟悉C+程序的开发过程。#include using namespace std;const float PI = 3.1416;void main()int iType;float radius, a, b, area;cout iType;switch(iType)case 1:cout radius;area = PI * radius * radius;cout面积为:areaendl;break;case 2:cout a;cout b;area = a * b; cout面积为:areaendl;break;case 3:cout a;area = a * a; cout面积为:areaendl;break;default:cout 不是合法的输入值!endl;(2) 改写以上程序,要求:编写三个内联函数分别完成 1-圆形 2-长方形 3-正方形的求面积操作。(3) 改写以上程序,要求:编写三个函数分别完成 1-圆形 2-长方形 3-正方形的求面积操作,以上三个函数分别写在三个文件中。(4) 思考:可否使用重载函数完成三种形状的求面积操作?答案:(2) 改写以上程序,要求:编写三个内联函数分别完成 1-圆形 2-长方形 3-正方形的求面积操作。#include using namespace std;const double PI = 3.1416;inline float cirlearea(float);inline float rectarea(float);inline float zrectarea(float);void main()int iType;float radius, a, b, area;cout iType;switch(iType)case 1:cout radius;area = cirlearea(radius);cout面积为:areaendl;break;case 2:cout a;cout b;area = a * b; cout面积为:areaendl;break;case 3:cout a;area = a * a;cout面积为:areaendl;break;default:cout 不是合法的输入值!endl;float cirlearea(float r)float area;return area = PI * r * r; float rectarea(float a,float b)float area;return area = a * b;float zrectarea(float a)float area;area = a * a;(3) 改写以上程序,要求:编写三个函数分别完成 1-圆形 2-长方形 3-正方形的求面积操作,以上三个函数分别写在三个文件中。#include using namespace std;const double PI = 3.1416;inline float cirlearea(float,float);inline float rectarea(float,float);inline float zrectarea(float,float);void main()int iType;float radius, a, b, area;float (*carea)(float,float);cout iType;switch(iType)case 1:carea=cirlearea;cout radius;area = (*carea)(radius,a=0);cout面积为:areaendl;break;case 2:carea=rectarea;cout a;cout b;area = (*carea)(a,b); cout面积为:areaendl;break;case 3:carea=zrectarea;cout a;area = (*carea)(a,a);cout面积为:areaendl;break;default:cout 不是合法的输入值!endl;float cirlearea(float r,float def)float area;return area = PI * r * r; float rectarea(float a,float b)float area;return area = a * b;float zrectarea(float a,float def)float area;return area = a * a;作业3:(1)设计一个类CRectangle,要求如下所述:a. 该类中的私有成员变量存放Rectangle的左上角x,y和它的长、宽,并且它们的默认值都是10。b. 通过函数设置其长和宽,并确保长和宽在(0,50)范围之内。c. 定义求它的周长的函数Perimeter。#includeusing namespace std;class Rectprivate:int length;int width;int perim;public:Rect()length =10;width =10;Rect(int length,int width)this-length = length;this-width = width;void setLength(int length)if(length 0 & length length = length;elsecoutINPUT ERROR!0 & width width = width;elsecoutINPUT ERROR!endl;float permi()return length*width;int main()Rect a;a.setLength(20);a.setWidth(20);couta.permi()endl;cin.get();return 0;(2)在实验任务一的基础上,要求有如下成员函数。a. Move:从一个位置移动到另一个位置。b. Size:改变矩形的大小。c. Where:返回矩形左上角的坐标值。d. Area:计算面积。clase CRectanglepublic:void Move(int dx,int dy)left+=dx;top+=dy;void Size(int newW,int newH)width=newW;height=newH;void Where(int &x,int &y)x=left;y=top;int Area()return(width*height);private:int left,top;/矩形的左上角横坐标和纵坐标int width,height;/矩形的宽度和高度;作业4:声明一个CPU类,要求:(1)包含主频(frequency),字长(wordlength),CPU倍频系数(coefficient)属性,其中字长为枚举型enum cpu_wordlen=W16,W32,W64,W128,W256, frequency是单位为GHz的实数,coefficient为浮点型数据。两个公有成员函数run和stop分别表示CPU的运行与停止。(2)请在构造函数(带参数和不带参数)、拷贝构造函数、析构函数、run和stop函数体给出相应的提示(输出提示字符串,例如“the CPU is running!”)。(3)说明并实现这个类,观察构造函数、拷贝构造函数和析构函数的调用顺序。主函数内容如下所示:int main()double x=3.2,y=4.5;CPU mycpu;mycpu.run();mycpu.stop();CPU hiscpu(x,W64,y);hiscpu.run();hiscpu.stop();CPU hercpu(hiscpu);hercpu.run();hercpu.stop(); return 0;答案:#include using namespace std;enum cpu_wordlenW16,W32,W64,W128,W256;class CPUpublic:CPU()frequency=0;wordlength=W16;coefficient=0;coutfrequency: frequency wordlength:wordlength coefficient:coefficientendl;coutcalling the constructor without parameters!endl;CPU(float f,cpu_wordlen l,float c)frequency=f;wordlength=l;coefficient=c;coutfrequency: frequency wordlength:wordlength coefficient:coefficientendl;coutcalling the constructor with parameters!endl;CPU(CPU &C)frequency=C.frequency;wordlength=C.wordlength;coefficient=C.coefficient;coutcalling the copy constructor!endl;CPU()coutcalling the deconstructor!endl;void run()coutCPU is running!endl; void stop()coutCPU stops!endl;private:double frequency;enum cpu_wordlen wordlength;double coefficient;int main()double x=3.2,y=4.5;CPU mycpu;mycpu.run();mycpu.stop();CPU hiscpu(x,W64,y);hiscpu.run();hiscpu.stop();CPU hercpu(hiscpu);hercpu.run();hercpu.stop(); return 0;作业5:声明一个学生信息类:要求有学生的基本信息(自定),以及显示和设置这些信息的接口(公有成员函数),要求定义构造函数(带参数和不带参数)和析构函数以及复制构造函数。并进行测试。(2)执行下列程序,观察结果,并给出所有对象的构造、复制构造和析构函数的调用顺序。#include class point int x,y;public:point(int a,int b)x=a;y=b;coutcalling the constructor function.endl;point(point &p);friend point move(point q);point()coutcalling the destructor function.n;int getx() return x;int gety() return y;point:point(point &p)x=p.x; y=p.y;coutcalling the copy_initialization constructor function.n;point move(point q)coutOK!n;int i,j;i=q.x+10;j=q.y+20;point r(i,j);return r;void main() point m(15,40),p(0,0);point n(m);p=move(n);coutp=p.getx(),p.gety()endl;答案:#include #include using namespace std;class Studentpublic:Student(char*, int, bool);Student(const Student&);Student();Student& operator =(const Student&);void print();private:char* name;int age;bool sex;int len; ;Student:Student(char* name, int age, bool sex)len = strlen(name);this-name = new charlen+1;strcpy(this-name, name);this-age = age;this-sex = sex;Student:Student(const Student& stu)len = strlen();this-name = new charlen+1;strcpy(this-name, );this-age = stu.age;this-sex = stu.sex;Student:Student()len = 0;delete name;Student& Student:operator =(const Student& stu)len = strlen();this-name = new charlen+1;strcpy(this-name, );this-age = stu.age;this-sex = stu.sex;return *this;void Student:print()cout name t;if (sex) cout 男;else cout 女;cout t age endl;int main()Student s1(张三, 20, true);Student s2(s1);Student s3 = s1;s1.print();s2.print();s3.print();return 0;(2)M: point(int a,int b)构造,point(point &p)复制P:point(int a,int b)构造,move(point q) ,gety(),getx()N:point(point &p)复制,R:point(int a,int b)构造,point(point &p)复制point()析构,point()析构point()析构作业6:1设计一个解决王婆卖瓜问题的程序。王婆卖瓜,每卖一个瓜,需记录该瓜的重量,还要记录所卖出的总重量和总个数。同时还允许退瓜。设计一个具有静态数据、函数成员的watermelon类。实现提示:西瓜类中,设计3个数据成员(重量weight、总重量total_weiht、总个数total_number)。因为不论西瓜是否存在,总重量total_weiht和总个数total_number这两个数据总是要保留的,因此这两个数据要申明为静态数据成员。成员函数:卖瓜用构造函数模拟,退瓜用析构函数模拟,瓜重用显示disp()成员函数模拟。为了用不与特定对象相联系的静态成员函数来访问静态数据,还需要定义一个显示总重量和总数的静态成员函数total_disp()。2设计一个程序,其中有3个类,即CBank,BBank和GBank,分别为中国银行类、工商银行类和农业银行类。每个类都包含一个私有数据balance,用于存放储户在该行的存款数,另有一个友元函数total用于计算储户在这3家银行中的总存款数。答案:1.#include #include class CMelonSalepublic:CMelonSale(float vWeight)m_Weight = vWeight;m_TotalWeight += m_Weight;m_WeightStore.push_back(m_Weight);m_TotalNum+;CMelonSale()m_TotalNum-;static void totalDisP()std:coutThe Total Num ism_TotalNumstd:endl;std:coutThe Total Weight arem_TotalWeightstd:endl;static void showWeight()std:vector:iterator itr;for (itr = m_WeightStore.begin();itr!=m_WeightStore.end();itr+)std:cout*itrstd:endl;protected:private:static int m_TotalNum ;static float m_TotalWeight ;float m_Weight;static std:vector m_WeightStore;int CMelonSale:m_TotalNum = 0;float CMelonSale:m_TotalWeight = 0.0;std:vector CMelonSale:m_WeightStore ;int main()CMelonSale a(0.6);CMelonSale b(0.8);CMelonSale c(0.9);CMelonSale d(1.1);CMelonSale:totalDisP();CMelonSale:showWeight();return 0;2.#include using namespace std;class CBankprivate:float balance;public:CBank(float balance = 0)this-balance = balance;friend class user;class BBankprivate:float balance;public:BBank(float balance = 0)this-balance = balance;friend class user;class GBankprivate:float balance;public:GBank(float balance = 0)this-balance = balance;friend class user;class userprivate:CBank C;BBank B;GBank G;public:user(CBank c,BBank b,GBank g)C = c;B = b;G = g;void total()coutC.balance + B.balance + G.balanceendl;int main()CBank c(100.1);BBank b(100.2);GBank g(100.3);user u(c,b,g);u.total();作业7:1要求用多文件结构实现该问题(可以使用类向导)。声明一个学生类和一个教师类:要求有学生和教师的基本信息(自定),以及显示和设置这些信息的接口(公有成员函数),要求定义构造函数(带参数和不带参数)和析构函数以及复制构造函数。并进行测试。#include using namespace std;class Studentpublic:Student(int,char,char,float);int get_num()return num;char * get_name()return name;char get_sex()return sex;void display()coutnum:numnname:namensex:sexnscore:scorenn;private:int num;char name20;char sex;float score;Student:Student(int n,char nam,char s,float so)num=n;strcpy(name,nam);sex=s;score=so;class Teacherpublic:Teacher()Teacher(Student&);Teacher(int n,char nam,char sex,float pay);void display();private:int num;char name20;char sex;float pay;Teacher:Teacher(int n,char nam,char s,float p)num=n;strcpy(name,nam);sex=s;pay=p;Teacher:Teacher(Student& stud)num=stud.get_num();strcpy(name,stud.get_name();sex=stud.get_sex();pay=1500;void Teacher:display()coutnum:numnname:namensex:sexnpay:paynn;int main()Teacher teacher1(10001,Li,f,1234.5),teacher2;Student student1(20010,kuang,m,89.5);coutstudent1:endl;student1.display();teacher2=Teacher(student1);coutteacher2:endl;teacher2.display();return 0;作业8:1) 学生信息管理系统 创建一个班级学生的信息,班级人数手动输入 创建一个学生信息类(2) 完善以上程序:加入一个班级类,并测试。 班级类中包含一个动态对象数组用于存储学生信息,构造函数中根据学生人数确定数组长度 析构函数中删除动态数组 采用多文件结构答案:#includeclass Personchar name10;char sex;int age;public:void input()coutname;coutsex;coutage;void display()cout姓名:name,性别:sex,年龄:ageendl;class Student:public Personchar sno10;int score;public:void input()Person:input();coutsno;coutscore;void display()Person:display();cout学号;sno,成绩:scoreendl;void main()Student s1;s1.input();s1.display();作业9:由在校人员类(Person)作为基类派生出学生类(Student):在校人员类有成员数据:编号(ID)、姓名(name)、性别(sex)、年龄(age),要求有如下成员函数:构造函数、获取编号的函数和输出所有成员的函数。把在校人员类作为基类,通过公有继承,派生学生类,派生类新增成员数据有数学(math)、物理(physical)、英语(english)和C+程序设计(cpp)四门课程以及总成绩(total);新增成员函数有构造函数和输出所有成员的函数。main()完成派生类对象的定义和有关成员函数的测试。参考程序:#include #include usingnamespacestd;class Personpublic:Person(int i,char *n, char s, int a)ID=i;name=n;sex=s;age=a; ;class Student:public Personpublic:Student(int i,char *n,char s,int a,float m,float p,float e,float c):Person(i,n,s,a)math=m;physical=p;english=e;cpp=c;total=math+physical+english+cpp;void main()Person p1(1,张帅,M,22);p1.show();coutendl;Student s1(9901,林维,S,21,65,70,75,88);s1.show();答案:#include #include using namespace std;class Personpublic:Person(int i,char *n, char s, int a)ID=i;name=n;sex=s;age=a; int getID()return ID;v coutID:IDendl;coutname:nameendl;coutoid show()sex:sexendl;coutage:ageendl;private:int ID;string name;char sex;int age;class Student:public Personpublic:Student(int i,char *n,char s,int a,float m,float p,float e,float c):Person(i,n,s,a)math=m;physical=p;english=e;cpp=c;total=math+physical+english+cpp;void show()Person:show();coutmath:mathendl;coutphysical:physicalendl;coutenglish:englishendl;coutcpp:cppendl;couttotal:totalendl;private:float math,physical,english,cpp,total;void main()Person p1(1,张帅,M,22);p1.show();coutendl;Student s1(9901,林维,S,21,65,70,75,88);s1.show();作业10:由学生类、课程类作为基类,共同派生选课类。1 声明一个学生类,有成员数据:学号、姓名、性别、年龄,要求有如下成员函数:构造函数、输出所有成员的函数。2 声明一个课程类,有成员数据:课程编号(cnum)、课程名称(cname)、学时数(chour),要求有如下成员函数:构造函数、输出所有成员的函数。3 将学生类和课程类作为基类,通过公有继承,共同派生选课类,派生类新增成员数据有:成绩(score);新增成员函数有:构造函数、输出所有成员的函数。4 main()完成派生类对象的定义和有关成员函数的测试。答案:#include #include using namespace std;class Studentpublic:Student(int i
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 福州车位租赁合同(标准版)
- 草拟入股合同(标准版)
- 乳品干燥工国庆节后复工安全考核试卷含答案
- 钢琴调律师中秋节后复工安全考核试卷含答案
- 淀粉糖制造工国庆节后复工安全考核试卷含答案
- 盐斤分装设备操作工中秋节后复工安全考核试卷含答案
- 企业劳动合同参考
- 粪便清运工国庆节后复工安全考核试卷含答案
- 稀土发光材料工节假日前安全考核试卷含答案
- 正式员工劳动协议5篇
- 2023年国家教育行政学院招聘笔试真题
- 快递设备安全培训
- 典当行财产抵押借款合同2024
- 行为矫正技术学习通超星期末考试答案章节答案2024年
- 家具安装调试及施工进度保障措施
- 2024制冷系统管路结构设计指导书
- GB/T 18029.6-2024轮椅车第6 部分:电动轮椅车最大速度的测定
- 健康照护师测试题及答案【300题】附有答案
- 城市配送行业未来发展展望
- 大学生校园网络安全文明主题
- (正式版)SHT 3115-2024 石油化工管式炉轻质浇注料衬里工程技术规范
评论
0/150
提交评论