Course类(11.16)

实现一个course类

类的定义与类的实现分离有以下两点好处
1.可以隐藏实现。可以在不改变类定义的前提下,自由更改实现,而使用该类的客户端程序并不需要更改。
2.有利于保护知识产权。

course.h

#ifndef COURSE_H
#define COURSE_H
#include <string>
using namespace std;

class Course
{
public:
	Course();                                     //缺省构造函数
	Course(const string& courseName, int capacity);    //构造函数
	~Course();                                         //析构函数(销毁对象后调用)
	Course(const Course&);                         //自定义拷贝构造函数,实现深拷贝
	string getCourseName() const;
	void addStudent(const string& name);
	void dropStudent(const string& name);          //删除学生
	string* getStudents() const;
	int getNumberOfStudents() const;

private:
	string courseName;
	string* students;
	int numberOfStudents;
	int capacity;
};

#endif

类的实现
course.cpp

#include <iostream>
#include "Course.h"
using namespace std;

Course::Course()
{
	return;
}

Course::Course(const string& courseName, int capacity)
{
	numberOfStudents = 0;
	this->courseName = courseName;
	this->capacity = capacity;
	students = new string[capacity];
}

Course::~Course()
{
	delete[] students;
}

Course::Course(const Course& course)
{
	courseName = course.courseName;
	numberOfStudents = course.numberOfStudents;
	capacity = course.capacity;
	students = new string[capacity];

}

string Course::getCourseName() const
{
	return courseName;
}

void Course::addStudent(const string& name)
{
	if (numberOfStudents >= capacity)             
	{
		capacity += 5;
		string*students1 = new string[capacity];
		memcpy(students1, students, (capacity - 5) * sizeof(string));

	}
	students[numberOfStudents] = name;
	numberOfStudents++;
}

void Course::dropStudent(const string& name)
{
	int i;
	for (i = 0;i < numberOfStudents++;i++)
	{
		if(students[i] ==name)                    //也可以用数组函数find(index,index,key)
		{
			for (;i <= numberOfStudents;i++)
			{
				students[i] = students[i + 1];
			}
		}
	}
	numberOfStudents--;
}

string* Course::getStudents() const
{
	return students;
}

int Course::getNumberOfStudents() const
{
	return numberOfStudents;
}

主函数

#include <iostream>
#include "Course.h"
using namespace std;

int main()
{
	Course course1("数据结构", 10);
	Course course2("数值分析", 15);
	

	course1.addStudent("张雪鹏");
	course1.addStudent("刘朝阳");
	course1.addStudent("王子光");

	course2.addStudent("李三毛");
	course2.addStudent("胡一文");

	cout << "选该课程的学生有: " <<course1.getNumberOfStudents() << endl;
	string* students = course1.getStudents();
	for (int i = 0; i < course1.getNumberOfStudents(); i++)
		cout << students[i] << endl;
	                    
	cout << "课程2中的学生有: "<< course2.getNumberOfStudents() << endl;
	students = course2.getStudents();
	for (int i = 0; i < course2.getNumberOfStudents(); i++)
		cout << students[i] <<endl;

	Course course3(course1);
	cout << "课程3中的学生有: " << course3.getNumberOfStudents() << endl;
	students = course3.getStudents();
	for (int i = 0; i < course3.getNumberOfStudents(); i++)
		cout << students[i] << endl;
	return 0;
}
发布了21 篇原创文章 · 获赞 10 · 访问量 2655

猜你喜欢

转载自blog.csdn.net/weixin_42199022/article/details/97927912
今日推荐