C++函数调用参数的问题

昨天晚上做C++作业的时候出现了关于函数调用参数的问题,和同学讨论了下,发现是我把简单的东西复杂化了。

作业题目如下:
3、定义一个表示学生信息的类Student,要求如下:
(1)类Student的成员变量:
sNo 表示学号;sName表示姓名;sSex表示性别;sAge表示年龄;sCplus:表示C++课程成绩。
(2)类Student带参数的构造方法:
在构造方法中通过形参完成对成员变量的赋值操作。
(3)类Student的方法成员(方法返回值需自行设置):
getNo():获得学号; getName():获得姓名; getSex():获得性别; getAge()获得年龄; get Cplus():获得C++课程成绩
(4)根据类Student的定义,编写代码对Student类进行测试。可创建创建五个该类的对象或对象数组(会的,可以尝试一下这种形式),输出每个学生的信息,计算并输出这五个学生C++语言成绩的平均值,以及计算并输出他们C++语言成绩的最大值和最小值。

最初实现如下:

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

using namespace std;

class Student{
public:
	Student(string sNo,string sName,string sSex,string sAge,int sCplus);
	string getNo(){return sNo;};
	string getName(){return sName;};
	string getSex(){return sSex;};
	string getAge(){return sAge;};
	int getCplus(){return sCplus;};
private:
	string sNo;
	string sName;
	string sSex;
	string sAge;
	int sCplus;
};

//学生信息
Student::Student(string sNo,string sName,string sSex,string sAge,int sCplus){
	this->sNo = sNo;
	this->sName = sName;
	this->sSex = sSex;
	this->sAge = sAge;
	this->sCplus = sCplus;
	cout << sName << ": " << sSex << " " << sAge << " " << sCplus << endl;
}

int main(){
	Student Sam("01","Sam","boy","18",95);
	Student Bob("01","Bob","boy","19",96);
	Student Jay("03","Jay","girl","17",92);
	Student Dan("04","Dan","boy","19",97);
	Student Rio("05","Rio","girl","18",91);

	//求出最高分和最低分
	double max,min;
	if(Sam.getCplus()> Bob.getCplus()){
		max = Sam.getCplus();
		min = Bob.getCplus();
	} 
	else{
		max = Bob.getCplus();
		min = Sam.getCplus();
	}
	if(max < Jay.getCplus()){
		max = Jay.getCplus();
	}
	else if(min > Jay.getCplus()){
		min = Jay.getCplus();
	}
	if(max < Dan.getCplus()){
		max = Dan.getCplus();
	}
	else if(min > Dan.getCplus()){
		min = Dan.getCplus();
	}		
	if(max < Rio.getCplus()){
		max = Rio.getCplus();
	}
	else if(min > Rio.getCplus()){
		min = Rio.getCplus();
	}
	cout << "最大值是" << max << endl;
	cout << "最小值是" << min << endl;
}

我遇到的问题是,如何将求最高分和求最低分这个模块写在函数体部分中,而且这个函数可以调用由student 类创建的对象的Cplus值。

一开始我想在这个函数内创建类对象,并对类对象赋值Cplus,但是我发现这样并不行。

我当时的思想一直局限在,写的这个getMaxandMin()的形式参数一定要使用对象的值。但其实这个比大小的函数用的形式参数只要是普通的字母就可以了,在主体部分函数的实参使用对象的值。

具体如下:

void getMaxandMin(int &a,int &b,int &c,int &d,int &e){
	double max,min;
	if(a> b){
		max = a;
		min = b;
	} 
	else{
		max = b;
		min = a;
	}
	if(max < c){
		max = c;
	}
	else if(min > c){
		min = c;
	}
	if(max < d){
		max = d;
	}
	else if(min > d){
		min = d;
	}		
	if(max < e){
		max = e;
	}
	else if(min > e){
		min = e;
	}
	cout << "最高分是" << max << endl;
	cout << "最低分是" << min << endl;
}//求出最大和最小的分数
mark1=Sam.getCplus();
	mark2=Bob.getCplus();
	mark3=Jay.getCplus();
	mark4=Dan.getCplus();
	mark5=Rio.getCplus();
	getMaxandMin(mark1,mark2,mark3,mark4,mark5);
	getAverage(mark1,mark2,mark3,mark4,mark5);

在这里又遇到个问题,为什么函数getMaxandMin()不可以直接调用对象的值?就像这样

getMaxandMin(Sam.getCplus(),Bob.getCplus(),Jay.getCplus(),Dan.getCplus(),Rio.getCplus());

这个问题的解答目前我还不知道为啥,但我个人的理解是函数在创建的时候用的形参是普通变量,并没有调用类的对象值吗?

在提交作业的时候,老师说我的求最高分和最低分的部分可以用更通用的“冒泡排序”方法,我修改了之后
实现如下:

void getMaxandMin(int &a,int &b,int &c,int &d,int &e){
	int choose[]= {a,b,c,d,e};
	int max = choose[0];
	int min = choose[0];	
	for(int i=0;i<5;i++){
		if(choose[i] > max)
			max = choose[i];
		if(choose[i] < min)
			min = choose[i];
	}
	cout << "最高分是" << max << endl;
	cout << "最低分是" << min << endl;
}//求出最大和最小的分数

通过循环,将max和min与每一个数组内的值进行比较。

最终提交的版本如下:

#include <iostream>
#include <string.h>
using namespace std;

class Student{
public:
	Student(string sNo,string sName,string sSex,string sAge,int sCplus);
	string getNo(){return sNo;};
	string getName(){return sName;};
	string getSex(){return sSex;};
	string getAge(){return sAge;};
	int getCplus(){return sCplus;};
protected:
	string sNo;
	string sName;
	string sSex;
	string sAge;
	int sCplus;
};

Student::Student(string sNo,string sName,string sSex,string sAge,int sCplus){
	this->sNo = sNo;
	this->sName = sName;
	this->sSex = sSex;
	this->sAge = sAge;
	this->sCplus = sCplus;
	cout << "姓名:" << sName << " 性别:" << sSex 
	<< " 年龄:" << sAge << " 成绩:" << sCplus << endl;
} 

void getMaxandMin(int &a,int &b,int &c,int &d,int &e){
	int choose[]= {a,b,c,d,e};
	int max = choose[0];
	int min = choose[0];	
	for(int i=0;i<5;i++){
		if(choose[i] > max)
			max = choose[i];
		if(choose[i] < min)
			min = choose[i];
	}
	cout << "最高分是" << max << endl;
	cout << "最低分是" << min << endl;
}//求出最大和最小的分数

void getAverage(int &a,int &b,int &c,int &d,int &e){
	double average;
	average = (a + b + c + d + e)/5.0;
	cout << "学生的平均分数是" << average << endl;
}

int main(){
	int mark1,mark2,mark3,mark4,mark5;
	Student Sam("01","Sam","boy","18",95);
	Student Bob("01","Bob","boy","19",96);
	Student Jay("03","Jay","girl","17",92);
	Student Dan("04","Dan","boy","19",97);
	Student Rio("05","Rio","girl","18",91);
	mark1=Sam.getCplus();
	mark2=Bob.getCplus();
	mark3=Jay.getCplus();
	mark4=Dan.getCplus();
	mark5=Rio.getCplus();
	getMaxandMin(mark1,mark2,mark3,mark4,mark5);
	getAverage(mark1,mark2,mark3,mark4,mark5);
}

这问题搞了我好久,昨天和同学讨论了一会儿问题就解决了,果然还是要多问问题啊!

发布了16 篇原创文章 · 获赞 0 · 访问量 167

猜你喜欢

转载自blog.csdn.net/qq251031557/article/details/104819115
今日推荐