学校Test3

来源:互联网 发布:电脑美工基础视频 编辑:程序博客网 时间:2024/04/27 19:50

(一)类和对象的简单应用

1、声明一个时间类,该类中有3个私有数据成员(HourMinuteSecond)和两个公有成员函数(setTimeprintTime)。setTime根据传递的三个参数为对象设置时间;printTime负责将对象表示的时间显示输出。

在主函数中建立时间类的对象,设置时间为92030秒,并显示该时间。

2、使用构造函数代替上面的setTime成员函数,并在主函数中使用构造函数设置时间为104050秒,并显示该时间。

3、重载时间类的构造函数(不带参数),使小时、分、秒均为0

4、在时间类的析构函数中输出“Goodbye!”

5、定义拷贝构造函数并调用


(二)类和对象的应用(1

1、定义一个Stock类,记录一支股票交易的基本信息,信息包括交易日序号(表示本月的第几个交易日,用整数表示)、当日最高价、当日最低价、当日开盘价和当日收盘价。尽量发挥想象力,为该类设计成员函数。

2、在主函数中建立两个股票对象,分别存储该股票昨天和今天两天的信息,以当日收盘价计算该股票今天的涨幅。


(三)类和队形的应用(2

定义一个矩形类(Rectangle),私有数据成员为矩形的长度(length)和宽度(width),无参构造函数置lengthwidth0,有参构造函数置lengthwidth为对应形参的值,另外还包括求矩形周长、求矩形面积、求矩形长度和宽度、修改矩形长度和宽度为对应形参的值、输出矩形尺寸等共有成员函数。

test3-1

#include<iostream>using namespace std;class time{    private:    int hour;    int minute;    int second;    public:    time()    {        hour=0;        minute=0;        second=0;    }    time(int h,int m,int s)    {        hour=h;        minute=m;        second=s;    }    time(const time &a)    {        hour=a.hour+1;        minute=a.minute+1;        second=a.second+1;    }    ~time()    {cout<<"goodbye!"<<endl;}    void settime(int,int,int);    void printtime();};void time::settime(int h,int m,int s){    hour=h;    minute=m;    second=s;}void time::printtime(){    cout<<hour<<":"<<minute<<":"<<second<<endl;}int main(){    class time t;    class time t1(10,40,50);    class time t2(t1);    t.settime(9,20,30);    t.printtime();    t1.printtime();    t2.printtime();    return 0;}

test3-2

#include<iostream>using namespace std;class stock{    private:    int num;    float maxprice;    float minprice;    float startprice;    float endprice;    public:    stock(int n)    {        num=n;        cout<<"pls input the date of "<<num<<"'stock"<<endl;        cout<<"maxprice:";cin>>maxprice;        cout<<"minprice:";cin>>minprice;        cout<<"startprice:";cin>>startprice;        cout<<"endprice:";cin>>endprice;    }    void rate(stock &,stock &);    void show();};void stock::rate(stock &a,stock &b){    cout<<"rate:"<<(b.endprice-a.endprice)/a.endprice<<endl;}void stock::show(){    cout<<"the date of"<<num<<"'stock:"<<endl<<"startprice:"<<startprice<<endl<<"endprice:"<<endprice<<endl<<"maxprice:"<<maxprice<<endl<<"minprice"<<minprice<<endl;}int main(){    stock s1(1),s2(2);    s1.show();    s2.show();    s1.rate(s1,s2);    return 0;}

test3-3

#include<iostream>using namespace std;class rectangle{    private:    float length;    float width;    public:    rectangle()    {length=0;width=0;}    rectangle(float l,float w)    {length=l;width=w;}    void compute();    void changedata();    void show();};void rectangle::compute(){    cout<<"the rectangle'周长 is:"<<(width+length)*2<<endl;    cout<<"the rectangle'面积 is:"<<width*length<<endl;}void rectangle::changedata(){    cout<<"update the data of r1:"<<endl;    cout<<"length:";cin>>length;    cout<<"width:";cin>>width;}void rectangle::show(){    cout<<"the length of the rectangle is:"<<length<<endl;    cout<<"the width of the rectangle is:"<<width<<endl;}int main(){    rectangle r1,r2(12,23);    r1.show();r2.show();    r1.compute();r2.compute();    r1.changedata();    r1.show();    r1.compute();    return 0;}