`
ding43930053
  • 浏览: 41632 次
社区版块
存档分类
最新评论

类练习

 
阅读更多

MyStudentClass.h

#include "stdafx.h"

#include <iostream>

using namespace std;

class Student

{

private:

	int m_number; //学号

	char *m_name; //字符指针变量,姓名

	double m_score; //成绩

public:

	Student()

	{
		
		m_name = new char[20];
		
	}

	Student(Student &stu)

	{

		m_name = new char[20];
		
		strcpy(m_name,stu.m_name);

		m_number = stu.m_number;

		m_score = stu.m_score;

	}

	~Student()

	{

		delete m_name;

	}

	Student &operator=(const Student &stu)

	{

		m_name = new char[20];

		strcpy(m_name,stu.m_name);

		m_number = stu.m_number;

		m_score = stu.m_score;

		return *this; 

	}

	void SetDate(int number, char *name, double score);

	void Display();

	double GetSore();

};


MyStudentClass.cpp

#include "stdafx.h"

#include "MyStudentClass.h"

using namespace std;

void Student::SetDate(int number, char *name, double score)

{

 this->m_number = number;

 strcpy(m_name,name);

 this->m_score = score;

}

void Student::Display()

{

 cout <<setw(4)<<"学号"<<setw(12)<<"姓名"<<setw(8)<<"成绩"<<endl;

 cout <<setw(4)<<this->m_number<<setw(12)<<this->m_name<<setw(8)<<this->m_score<<endl;

}

double Student::GetSore()

{

 return m_score;

}


MyManageClass.h

#include "MyStudentClass.h"

#define NUM 2

class Manage

{

private:

	Student stu[NUM];

public:

	void Input();

	void DescSort();

	void Output();

};


MyManageClass.cpp

#include "stdafx.h"

#include "MyManageClass.h"

using namespace std;

void Manage::Input()

{

	int number;

	char name[20];

	double scord;
	for (int i=0;i<NUM;i++)

	{

		cout <<"请输入学号:";

		cin >>number;

		cout <<"请输入姓名:";

		cin >>name;

		cout <<"请输入成绩:";

		cin >>scord;

		cout <<endl;

		stu[i].SetDate(number, name, scord);

	}



}

void Manage::DescSort()

{
	
	
	Student sTempMin;

	for (int i=0; i<NUM; i++)
	{
		for (int j=i+1; j<NUM; j++)

		{

			if (stu[i].GetSore() < stu[j].GetSore())

			{

				sTempMin = stu[j];

				stu[j] = stu[i];

				stu[i] = sTempMin;
				
			}
			
		}
	
	}


}

void Manage::Output()

{

	for (int i=0;i<NUM;i++)

	{

		stu[i].Display();

	}

}


MyClassTestContro.cpp

// MyClassTest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include "MyManageClass.h"

#include "TestCoty.h"

//#include "MyDateClass.h"

using namespace std;

void HanShu()

{

	Manage m;

	m.Input();

	m.Output();

	cout <<"降序排列"<<endl;
	m.DescSort();

	m.Output();

}


void main()

{

	HanShu();
	
}



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics