l6_10.cpp

来自「《C++程序设计教程》电子教案及例题源码」· C++ 代码 · 共 48 行

CPP
48
字号
#include <iostream.h>
#include <string.h>
class CStudent
{
private:
	int number;
	char name[10];
	int age;
public:
	CStudent(int xh, char *xm, int a);
	int GetAge();

};

CStudent::CStudent(int xh, char *xm, int a)
{
	number = xh;
	strcpy(name, xm);
	age = a;
}

int CStudent::GetAge()
{
	return age;
}

void main()
{
	int sum=0;
	CStudent *p[5];
	p[0] = new CStudent(10001, "AAAAAA", 20);
	p[1] = new CStudent(10002, "BBBBBB", 22);
	p[2] = new CStudent(10003, "CCCCCC", 24);
	p[3] = new CStudent(10004, "DDDDDD", 26);
	p[4] = new CStudent(10005, "EEEEEE", 28);

	for(int i=0; i<5; i++)
	{
		sum += p[i]->GetAge();
	}
	cout << sum/5 << endl;
	for(i=0; i<5; i++)
	{
		delete p[i];
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?