C++随笔——成员函数的相互调用问题:

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013555238/article/details/78156803

C++ 程序设计 Y.Daniel Liang 11章例子:Course

Course.h

#ifndef COURSE_H
#define COURSE_H
#include <string>

using namespace std;

class Course
{
public:
	Course(const string& courseName,int Capacity);
	~Course();
	string getCoureName() const;
	void addStudent(const string& name);
	void dropStudent(const string& name);
	string* getStudent() const;
	int getNumberOfStudents() const;
	void printStudent();
private:
	string courseName;
	string* student;
	int numberOfStudent;
	int Capacity;
};


#endif  COURSE_H

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

Course::Course(const string&courseName ,int Capacity)
{
	numberOfStudent = 0;
	this->courseName = courseName;
	this->Capacity = Capacity;
	student = new string[Capacity];
}

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

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

void Course::addStudent(const string& name)
{
	if(numberOfStudent < Capacity)
	{
	    student[numberOfStudent] = name;
		numberOfStudent++;
	}
	else{
		cout << "学生太多了" << endl;
		return;
	}
	
}
void Course::dropStudent(const string& name)
{
	//
	string* pFindName = find(student,student+numberOfStudent,name);
	if(pFindName != student+numberOfStudent)
	{
		cout << "this student has been found" << "at position" << " " <<pFindName - student <<  endl;
		int postion = pFindName - student;
		for(int i = postion+1; i < numberOfStudent ;i++){
			student[i-1] = student[i];
		}
		numberOfStudent--;
	}
	else{
		cout << name << "is not found" << endl;
	}
}
string* Course::getStudent() const
{
	return student;
}
 
int Course::getNumberOfStudents() const{
	return numberOfStudent;
}

int getNumberOfStudents(){
	return 1;
}

void Course::printStudent(){
	for (int i = 0 ;i < getNumberOfStudents();i++)  //屏蔽了 全局的 函数 getNumberOfStudents(),效果和 Course::getNumberOfStudents() 相同。
	{
		cout << student[i] <<",";
	}
}

TestCourse.cpp

#include <iostream>
#include <string>
#include "Course.h"

using namespace std;

int main(void){
	int a;
	Course course1("Data_Structure",10);
	Course course2("Database_Systems",15);

	course1.addStudent("PJ");
	course1.addStudent("BS");
	course1.addStudent("AK");

	course2.addStudent("PJ");
	course2.addStudent("SS");

	cout << "Number of student in course1:" << course1.getNumberOfStudents() << "\n";
	string* students = course1.getStudent();
	course1.printStudent();

	cout << " \n Number of students in course2:" << course2.getNumberOfStudents() << "\n";

	students = course2.getStudent();
	course2.printStudent();

	course1.dropStudent("BS");
	course1.printStudent();
	cin >> a;
		return 0;
}

在  Course::printStudent(){
 for (int i = 0 ;i < getNumberOfStudents();i++)  //屏蔽了 全局的 函数 getNumberOfStudents(),效果和 Course::getNumberOfStudents() 相同。
 {
  cout << student[i] <<",";
 }
}
中调用的 getNumerOfStudent() 为自己的成员函数 Course::getNumberOfStudent(),而不是 在上面定义的全局命名空间的 getNumberOfStudent();相当于写成 Couse::getNumberOfStudent(); 如果想 调用上面的 全局命名空间的普通函数,则需要 显示的写成:

for (int i = 0 ;i < ::getNumberOfStudents();i++)  //屏蔽了 全局的 函数 getNumberOfStudents(),效果和 Course::getNumberOfStudents() 相同。
	{
		cout << student[i] <<",";
	}

::getNumberOfStudent()显示的说明了调用 全局命名空间的 getNumberOfStudent();



阅读更多
换一批

没有更多推荐了,返回首页