类的作业及复习资料.docx
/编程:建立一个分数类。分数类的数据成员包括分子与分母,操作包括显示、输入、约分、通分、比较、加、减、乘、除、求相反数。#include<iostream>#include<cmath>using namespace std;class fractionint above; /分子int below; /分母void reduction(); /约分void makeCommond(fraction&); /通分public:fraction(int a=0,int b=1) /构造函数above=a;below=b;fraction add(fraction); /两分数相加fraction sub(fraction); /本分数减去实参分数fraction mul(fraction); /两分数相乘fraction div(fraction); /本分数除以实参分数fraction reciprocal(); /求倒数bool equal(fraction); /等于运算bool greaterThan(fraction); /大于运算bool lessThan(fraction); /小于运算void display(); /显示分数void input(); /输入分数;void fraction:reduction() /约分先求最大公约数int a,b,temp;if(below<0)above=-above;below=-below;a=abs(above);b=abs(below);while(a%b) /欧几里德法求最大公约数temp=a;a=b;b=temp%b;above/=b;below/=b;void fraction:makeCommond(fraction& b)int temp;reduction();b.reduction();above*=b.below;b.above*=below;temp=below*b.below;below=b.below=temp;fraction fraction:add(fraction b)fraction temp;makeCommond(b); /通分temp.above=above+b.above;temp.below=below;temp.reduction(); /约分return temp;fraction fraction:sub(fraction b)fraction temp;makeCommond(b); /通分temp.above=above-b.above;temp.below=below;temp.reduction(); /约分return temp;fraction fraction:mul(fraction b)fraction temp;temp.above=above*b.above;temp.below=below*b.below;temp.reduction(); /约分return temp;fraction fraction:div(fraction b)fraction temp;if(b.above=0)cout<<"零不能作除数!"<<endl;exit(1);temp.above=above*b.below;temp.below=below*b.above;temp.reduction(); /约分return temp;fraction fraction:reciprocal()fraction temp;temp.above=below;temp.below=above;temp.reduction(); /约分return temp;bool fraction:equal(fraction b)makeCommond(b); /通分return(above=b.above);bool fraction:greaterThan(fraction b)makeCommond(b); /通分return(above>b.above);bool fraction:lessThan(fraction b)makeCommond(b); /通分return(above<b.above);void fraction:display()reduction(); /约分cout<<"为:"<<above<<"/"<<below<<endl;void fraction:input()while(1)cout<<"请顺序输入分子与分母(整数):"<<endl;cin>>above>>below;if(below=0) cout<<"分母不可为零!"<<endl;elsereduction();return;int main()fraction f1(-3,-5),f2(-3,5),f3(3,-7),f4,f5(8);cout<<"f1" f1.display();cout<<"f2" f2.display();cout<<"f3" f3.display();cout<<"f4" f4.display();cout<<"f5" f5.display();if(f1.greaterThan(f2) cout<<"f1>f2"<<endl;if(f2.lessThan(f3) cout<<"f2<f3"<<endl;if(f1.equal(f1) cout<<"f1=f1"<<endl;f4=f1.add(f3);cout<<"f4=f1+f3" f4.display();f4=f1.sub(f2);cout<<"f4=f1-f2" f4.display();f4=f1.mul(f3);cout<<"f4=f1*f3" f4.display();f4=f2.div(f3);cout<<"f4=f1/f3" f4.display();f4=f2.reciprocal();cout<<"f4=1/f2" f4.display();f4.input(); cout<<"f4" f4.display();return 0;4.3 构造一个日期时间类(Timedate),数据成员包括年、月、日与时、分、秒,函数成员包括设置日期时间与输出时间,其中年、月请用枚举类型,并完成测试。(包括用成员函数与用普通函数)解:本题要求仅是定义类的练习,并非实用的提供日期时间的程序。实用的日期时间程序见附录二的日期时间函数。#include <iostream>#include <iomanip>using namespace std;enum YRY2000,Y2001,Y2002,Y2003,Y2004,Y2005;/enum MTJan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec;class Timedateprivate:YR year;MT month;int date;int hh;int mm;int ss;public:Timedate()year=Y2000;month=Jan;date=1;hh=0;mm=0;ss=0;Timedate(YR a,MT b,int c)year=a;month=b;date=c;hh=12;mm=30;ss=0;void getdate(YR &,MT &,int &);/使用引用一次取得3个数值void gettime(int &,int &,int &);void putdate(YR ,MT ,int );void puttime(int ,int ,int );void list();void Timedate:getdate(YR &y,MT &m,int &d)y=year;m=month;d=date;void Timedate:gettime(int &a,int &b,int &c)a=hh;b=mm;c=ss;void Timedate:putdate(YR a,MT b,int c)year=a;month=b;date=c;void Timedate:puttime(int a,int b,int c)hh=a;mm=b;ss=c;void Timedate:list()/成员函数,直接访问私有的数据成员cout<<"year/month/date :"switch(year)case Y2000:cout<<"2000"break;case Y2001:cout<<"2001"break;case Y2002:cout<<"2002"break;case Y2003:cout<<"2003"break;case Y2004:cout<<"2004"break;case Y2005:cout<<"2005"break;switch(month)case Jan:cout<<'/'<<"Jan"break;case Feb:cout<<'/'<<"Feb"break;case Mar:cout<<'/'<<"Mar"break;case Apr:cout<<'/'<<"Apr"break;case May:cout<<'/'<<"May"break;case Jun:cout<<'/'<<"Jun"break;case Jul:cout<<'/'<<"Jul"break;case Aug:cout<<'/'<<"Aug"break;case Sep:cout<<'/'<<"Sep"break;case Oct:cout<<'/'<<"Oct"break;case Nov:cout<<'/'<<"Nov"break;case Dec:cout<<'/'<<"Dec"break;cout<<'/'<<date<<endl;cout<<"hour:minite:second :"cout<<hh<<':'<<mm<<':'<<ss<<endl;int main(int argc, char* argv)Timedate A(Y2004,Mar,3),B;A.list();B.list();B.putdate(Y2005,Oct,18);B.puttime(17,30,00);B.list();return 0;4.4 设计并测试一个矩形类(Rectangle),属性为矩形的左下及右上角的坐标,矩形水平放置。操作为计算矩形周长及面积。测试包括用成员函数与普通函数。解:这里的矩形的4边分别及x轴y轴平行,为最简单的情况。注意参数有缺省值的函数的声明与定义格式。#include <iostream>#include <cmath>using namespace std;class Rectangle double left, top ;double right, bottom;public:Rectangle(double l=0, double t=0, double r=0, double b=0); Rectangle(); /析构函数,在此函数体为空void Assign(double l,double t,double r,double b);double getLeft() return left; / 以下四个函数皆为内联成员函数double getRight() return right;double getTop()return top;double getBottom()return bottom;void Show();double Area();double Perimeter();/ 构造函数,带缺省参数,缺省值为全0,在声明中指定Rectangle:Rectangle(double l , double t, double r, double b) left = l; top = t;right = r; bottom = b; void Rectangle:Assign(double l, double t, double r, double b)/赋值left = l; top = t;right = r; bottom = b;void Rectangle:Show()/成员函数直接使用私有的数据成员cout<<"left-top point is ("<<left<<","<<top<<")"<<'n'cout<<"right-bottom point is ("<<right<<","<<bottom<<")"<<'n'double Rectangle:Area()return fabs(right-left)*(bottom-top);double Rectangle:Perimeter()return 2*(fabs(right-left)+fabs(bottom-top);int main()Rectangle rect; rect.Show();rect.Assign(100,200,300,400);rect.Show();Rectangle rect1(0,0,200,200);rect1.Show();Rectangle rect2(rect1);rect2.Show();cout<<"面积"<<rect.Area()<<'t'<<"周长"<<rect.Perimeter()<<endl;return 0;4.5 定义一个圆类(Circle),属性为半径(radius)、圆周长与面积,操作为输入半径并计算周长、面积,输出半径、周长与面积。要求定义构造函数(以半径为参数,缺省值为0,周长与面积在构造函数中生成)与拷贝构造函数。解:通常所有数据成员都在构造函数中赋初值。拷贝构造函数以本类的引用为参数。#include<iostream>#include<cmath>using namespace std;class Circledouble r,Area,Circumference;public:Circle(double a=0);Circle(Circle &);void SetR(double R);double GetR()return r;double GetAreaCircle()return Area;double GetCircumference()return Circumference;Circle:Circle(double a)r=a;Area=r*r*3.14159265;Circumference=2*r*3.14159265;Circle:Circle(Circle & cl)r=cl.r;Area=cl.Area;Circumference=cl.Circumference;void Circle:SetR(double R)r=R;Area=r*r*3.14159265;Circumference=2*r*3.14159265;int main()Circle cl1(2),cl2,cl3=cl1;cout<<"圆半径:"<<cl3.GetR()<<'t'<<"圆周长:"<<cl3.GetCircumference()<<'t'<<"圆面积:"<<cl3.GetAreaCircle()<<endl;cl2.SetR(4);cout<<"圆半径:"<<cl2.GetR()<<'t'<<"圆周长:"<<cl2.GetCircumference()<<'t'<<"圆面积:"<<cl2.GetAreaCircle()<<endl;return 0;