类和对象编程实例


#include<iostream>
#include <ctime>
#include <ostream>
#include <time.h>
using namespace std;
class clock
{
    public:
    void SetTime(int NewH,int NewM,int NewS);
    void ShowTime();
    private:
    int Hour,Second,Minute;
};
void clock::SetTime(int NewH, int NewM, int NewS)
{
    Hour=NewH;
    Minute=NewM;
    Second=NewS;
}
void clock::ShowTime()
{
    cout<<Hour<<":"<<Minute<<":"<<Second<<endl;

}
int main()
{
    Clock myclock;
    myclock.SetTime(8,30,0);
    myclock.ShowTime();
    return 0;
}


#include <iostream>
using namespace std;
class box
{
    private:
    int height;
    int width;
    int length;
    public:
    void setbox(int h=10,int w=12,int len=13)
    {
        height=h;
        width=w;
        length=len;

    }
    int volume();
};
int box::volume()
{return (height*width*length);}
int main()
{
    box a[3];
    a[0].setbox();
    a[1].setbox(15,17,24);
    a[2].setbox(16,18,23);
    cout<<"box of a.[0] is"<<a[0].volume()<<endl;
    cout<<"box of a[1] is"<<a[1].volume()<<endl;
    cout<<"box of a[2] is"<<a[2].volume()<<endl;
    return 0;
}


#include <iostream>
using namespace std;
class time
{
    public:
    int Hour;
    int Minute;
    int Second;
    void SetTime(int H,int M,int S);
    void Get_Time();
};
void time::SetTime(int H, int M, int S)
{
    Hour=H;
    Minute=M;
    Second=S;
}
void time::Get_Time()
{cout<<Hour<<":"<<Minute<<":"<<Second<<endl;}
int main()
{
    time t1;
    int *p1=&t1.Hour;
    t1.SetTime(10, 13, 56);
    cout<<*p1<<endl;
    t1.Get_Time();
    time *p2=&t1;
    p2->Get_Time();
    void(time::*p3)();
    p3=&time::Get_Time();
    (t1.*p3)();
    return 0;
}

#include <iostream>
using namespace std;
class A
{
    private:
    int i;
    public:
    int get() const{return i;}
    void set(int x)
    {
        this->i=x;
        cout<<",,"<<this<<endl;
    }
};
int main()
{
    A a;
    a.set(9);
    cout<<",,"<<&a<<endl;
    cout<<".."<<a.get()<<endl;
    cout<<endl;
    A b;
    b.set(999)
    cout<<",."<<&b<<endl;
    cout<<".,"<<b.get()<<endl;
    return 0;
}

#include<iostream>
using namespace std;
class Box
{
private:
int height;
int width;
int length;
public:
Box();
Box(int h=10,int w=10,int len=10);
int volume();
};
Box::Box()
{
    height=10;
    width=10;
    length=10;
}
Box::Box(int h,int w,int len)
{
height=h;
width=w;
length=len;
}
int Box::volume()
{return (height*width*length);}
int main()
{
    Box box1;
    cout<<".."<<box1.volume()<<endl;
    Box box4(15,20,25);
    cout<<",,"<<box4.volume()<<endl;
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超翔之逸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值