一些C++小程序(二)


//构造一个学生的结构体
#include 
#include 
#include 
using namespace std;
struct Student {
	int num;
	string name;
	char sex;
	int age;
};

int main()
{
	Student stu = { 97001,"LinLin",'F',19 };
	cout << "NUM:" << stu. num << endl;
	cout << "NAME:" << stu.name << endl;
	cout << "SEX:" << stu.sex << endl;
	cout << "AGE:" << stu.age << endl;
    return 0;
}


//声明一个CPU类包括等级(rank)频率(frequency)电压(voltage)等属性
//有两个公有成员函数run stop
//其中rank为枚举类型CPU_Rank声明为enum CPU_Rank{p1=1,p2,p3,p4,p5,p6,p7},frequency为单位是MHz的整形数,voltage为浮点型的电压值
//注意不同访问成员的访问方式,以及构造函数和析构函数调用顺序

#include 
using namespace std;

enum CPU_Rank { p1 = 1, p2, p3, p4, p5, p6, p7 };

class CPU
{
private:
	CPU_Rank rank;//声明CPU类,包含私有数据成员rank,frequency,voltage
	int frequency;
	float voltage;

public:
	CPU(CPU_Rank r, int f, float v)//构造函数
	{
		rank = r;
		frequency = f;
		voltage = v;
		cout << "构造了一个CPU!" << endl;
	}

	~CPU() { cout << "析构了一个CPU!" << endl; }

	CPU_Rank GetRank() const { return rank; }//外部接口函数
	int GetFrequency() const { return frequency; }
	float GetVoltag() const { return voltage; }

	void SetRank(CPU_Rank r) { rank = r; }
	void SetFrequency(int f) { frequency = f; }
	void SetVoltage(float v) { voltage = v; }

	void Run() { cout << "CPU开始运行!" << endl; }
	void Stop() { cout << "CPU停止运行!" << endl; }
};

int main()
{
	CPU a(p5, 300, 2.8);//调用成员函数
	a.Run();
	a.Stop();

return 0;
}


//在声明CPU类,RAM类,CDROM类基础之上,再声明Computer类。
//要求声明私有数据成员cpu、ram、cdrom、声明公有成员函数rum、stop、可在其中输入提示信息。
//在main()函数声明一个Computer类的对象,调用其成员函数。

#include 
using namespace std;

enum CPU_Rank { p1 = 1, p2, p3, p4, p5, p6, p7 };//声明cpu

class CPU 
{
private:
	CPU_Rank rank;//声明CPU类,包含私有数据成员rank,frequency,voltage
	int frequency;
	float voltage;

public:
	CPU(CPU_Rank r, int f, float v)//构造函数
	{
		rank = r;
		frequency = f;
		voltage = v;
		cout << "构造了一个CPU!" << endl;
	}

	~CPU() { cout << "析构了一个CPU!" << endl; }

	CPU_Rank GetRank() const { return rank; }//外部接口函数
	int GetFrequency() const { return frequency; }
	float GetVoltag() const { return voltage; }

	void SetRank(CPU_Rank r) { rank = r; }
	void SetFrequency(int f) { frequency = f; }
	void SetVoltage(float v) { voltage = v; }

	void Run() { cout << "CPU开始运行!" << endl; }
	void Stop() { cout << "CPU停止运行!" << endl; }
};

enum RAM_Type{DDR2=2,DDR3,DDR4};//声明RAM

class RAM
{
private:
	enum RAM_Type type;
	unsigned int frequency;//MHz
	unsigned int size;//GB

public:
	RAM(RAM_Type t, unsigned int f, unsigned int s)
	{
		type = t;
		frequency = f;
		size = s;
		cout << "构造了一个RAM!" << endl;
	}
	~RAM() { cout << "析构了一个RAM!" << endl; }

	RAM_Type GetType() const { return type; }
	unsigned int GetFrequency() const { return frequency; }
	unsigned int GetSize() const { return size; }

	void SetType(RAM_Type t) { type = t; }
	void SetFrequency(unsigned int f) { frequency = f; }
	void SetSize(unsigned int s) { size = s; }

	void Run() { cout << "RAM开始运行!" << endl; }
	void Stop() { cout << "RAM停止运行!" << endl; }
};

enum CDROM_Interface{SATA,USB};
enum CDROM_Install_type{extermal,built_in};

class CD_ROM
{
private:
	enum CDROM_Interface interface_type;
	unsigned int cache_size;//MB
	CDROM_Install_type install_type;

public:
	CD_ROM(CDROM_Interface i,unsigned int s,CDROM_Install_type it)
	{
		interface_type = i;
		cache_size = s;
		install_type = it;
		cout << "构造了一个CD_ROM!" << endl;
	}
	~CD_ROM() { cout << "析构了一个CD_ROM!" << endl; }

	CDROM_Interface GetInterfaceType() const { return interface_type; }
	unsigned int GetSize() const { return cache_size; }
	CDROM_Install_type GetInstallType() const { return install_type; }

	void SetInterfaceType(CDROM_Interface i) { interface_type = i; }
	void SetSize(unsigned int s) { cache_size = s; }
	void SetInstallType(CDROM_Install_type it) { install_type = it; }

	void Run() { cout << "CD_ROM开始运行!" << endl; }
	void Stop() { cout << "CD_ROM停止运行!" << endl; }
};

class COMPUTER
{
private:
	CPU my_cpu;
	RAM my_ram;
	CD_ROM my_cdrom;
	unsigned int storage_size;//GB
	unsigned int bandwidth;//MB

public:
	COMPUTER(CPU c, RAM r, CD_ROM cd, unsigned int s, unsigned int b);
	~COMPUTER() { cout << "析构了一个COMPUTER!" << endl; }

	void Run()
	{
		my_cpu.Run();
		my_ram.Run();
		my_cdrom.Run();
		cout << "COMPUTER开始运行!" << endl;
	}
	void Stop()
	{
		my_cpu.Stop();
		my_ram.Stop();
		my_cdrom.Stop();
		cout << "COMPUTER停止运行!" << endl;
	}
};

COMPUTER::COMPUTER(CPU c, RAM r, CD_ROM cd, unsigned int s, unsigned int b) :my_cpu(c),my_ram(r), my_cdrom(cd)
{
	storage_size = s;
	bandwidth = b;

	cout << "构造了一个COMPUTER!" << endl;

}

int main()
{
	CPU a(p6, 300, 2.8);
	a.Run();
	a.Stop();
	cout << "******************************************\n";

	RAM b(DDR3, 1600, 8);
	b.Run();
	b.Stop();
	cout << "******************************************\n";

	CD_ROM c(SATA,2,built_in);
	c.Run();
	c.Stop();
	cout << "******************************************\n";

	COMPUTER my_computer(a, b, c, 128, 10);
	cout << "******************************************\n";

	my_computer.Run();
	my_computer.Stop();
	cout << "******************************************\n";

    return 0;
}

//区分全局变量,静态局部变量,动态局部变量

#include 
using namespace std;
int i = 1;
void other() {
	static int a = 2;//a,b为静态局部变量,具有全局寿命,局部可见
	static int b;//只第一次进入函数被初始化
	int c = 10;//c为局部变量,具有动态生存期,每次进入都初始化
	a += 2; i += 32; c += 5;
	cout << "--OTHER--\n";
	cout << "i:" << i << "a:" << a << "b:" << b << "c:" << c << endl;
	b = a;
}

int main()
{
	static int a;//静态局部变量,有全局寿命,局部可见
	int b = -10;//b,c为局部变量,具有动态生存期
	int c = 0;
	cout << "--MAIN--\n";
	cout << "i:" << i << "a:" << a << "b:" << b << "c:" << c << endl;
	c += 8; other();
	cout << "--MAN--\n";
	cout << "i:" << i << "a:" << a << "b:" << b << "c:" << c << endl;
	i += 10; other();
	return 0;
}


//第二次学习对类的定义,体系重构
//计算面积周长
#include
using namespace std;

class CRecctangle {
public:
	int w, h;
	void Init(int w_, int h_) {
		w = w_;
		h = h_;
	}
	int Area() {
		return w*h;
	}
	int Preimeter() {
		return 2 * (w + h);
	}
};
int main()
{
	int w, h;
	CRecctangle r;
	cin >> w >> h;
	r.Init(w, h);
	cout << r.Area() << endl << r.Preimeter();
    return 0;
}



你可能感兴趣的:(C++)