随笔 - 398  文章 - 0  评论 - 6  阅读 - 3205

第四章部分例题(1)

例4-1

题目描述:时钟类的完整程序

代码实现:

复制代码
#include<iostream>
using namespace std;
class Clock
{
private:
    int hour, minute, second;
public:
    void setTime(int newH = 0, int newM = 0, int newS = 0)
    {
        hour = newH;
        minute = newM;
        second = newS;
    }
    void showTime()
    {
        cout << hour << ":" << minute << ":" << second << endl;
    }
};
int mian()
{
    Clock myClock;
    cout << "First time set and output:" << endl;
    myClock.setTime();
    myClock.showTime();
    cout << "Second time set and output:" << endl;
    myClock.setTime(8, 30, 30);
    myClock.showTime();
    return 0;
}
复制代码

例4-2

题目描述:Point类的完整程序。

代码实现:

复制代码
#include<iostream>
using namespace std;
class Point {
private:
    int x,y;
public:
    Point(int xx = 0, int yy = 0)
    {
        x = xx;
        y = yy;
    }
    Point(Point& p)
    {
        x = p.x;
        y = p.y;

    }
    int getX()
    {
        return x;
    }
    int getY()
    {
        return y;
    }
};
void fun1(Point p)
{
    cout<< p.getX() << endl;

}
Point fun2()
{
    Point a(1, 2);
    return a;
}
int main()
{
    Point a(4, 5);
    Point b = a;
    cout << b.getX() << endl;
    fun1(b);
    b = fun2();
    cout << b.getX ()<< endl;
    return 0;
}
复制代码

 

posted on   石铁生  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示