C/C++教程

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

本文主要是介绍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

 

这篇关于C++ 03从简单程序引入2的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!