第四章 类与对象

版权声明:qq836678589 https://blog.csdn.net/weixin_43924623/article/details/85113621

例4-1

#include<iostream>
using namespace std;
class Clock {
public:
	void setTime(int newH=0,int newM=0,int newS=0);
	void showTime();
private:
	int hour,minute,second;
};
void Clock::setTime(int newH,int newM,int newS)
{
	hour=newH;
	minute=newM;
	second=newS;
}
inline void Clock::showTime() {
	cout<<hour<<":"<<minute<<":"<<second<<endl;
}
int main() {
	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;
}

猜你喜欢

转载自blog.csdn.net/weixin_43924623/article/details/85113621