第四章部分例题(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; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了