C++ 03从简单程序引入2


//#define CLASS_STUDENT
//#define CLASS_TIME1
//#define CLASS_TIME2
#define CLASS_TIME3

#ifdef CLASS_TIME3

#include <iostream>

using namespace std;

class Time
{
public:
	void set_time();
	void show_time();

private:
	int hour;
	int minute;
	int sec;
};

void Time::set_time()
{
	cin>>hour;
	cin>>minute;
	cin>>sec;
}

void Time::show_time()
{
	cout<<hour<<":"<<minute<<":"<<sec<<endl;
}

int main(int argc, char *argv[])
{
	Time t1;
	t1.set_time();
	t1.show_time();

	Time t2;
	t2.set_time();
	t2.show_time();

	system("pause");
	return 0;
}

#elif defined CLASS_TIME2

#include <iostream>

using namespace std;

class Time
{
public:
	int hour;
	int minute;
	int sec;
};

int main(int argc, char *grgv[])
{
	void set_time(Time &, int hour=0, int minute=0, int sec=0);  //声明函数,指定默认形参,靠右边;
	void show_time(Time &);										//引用做形参

	Time t1;
	set_time(t1, 12, 23, 24);
	show_time(t1);

	Time t2;
	set_time(t2);
	show_time(t2);			//默认参数的调用

	system("pause");
	return 0;
}

void set_time(Time &t, int hour, int minute, int sec)
{
	t.hour = hour;
	t.minute = minute;
	t.sec = sec;
}

void show_time(Time &t)
{
	cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;
}



/
#elif defined CLASS_TIME1

#include <iostream>

using namespace std;

class Time
{
public:            //数据成员公用,暴露不安全。
	int hour;
	int minute;
	int sec;
};

int main(int argc, char *argv[])
{
	Time t1;
	cin>>t1.hour;
	cin>>t1.minute;
	cin>>t1.sec;

	cout<<t1.hour<<":"<<t1.minute<<":"<<t1.sec<<endl;

	system("pause");
	return 0;
}


//
#elif defined CLASS_STUDENT

#include <iostream>

#include "student.h"

using namespace std;

int main(int argc, char *argv[])
{
	student stud;
	stud.display();

	system("pause");
	return 0;
}


#endif

 

上一篇:Last minute enhancements


下一篇:JavaScript 获取视频时长的时分秒