C++ 有人能帮助我如何从类中调用此函数参数以显示在输出中吗
我使用函数重载的代码是这样一种代码,用户必须输入4个主题的指针和它的学分,以便打印出他们的GPA。问题是我在Student中有3个参数(stringtest123、stringnama、stringvinto)。但我只想显示其中一个字符串。假设我想把葡萄酒打印出来。如何在Display函数中调用vinto,以便它打印出vinto。这是我的代码 CPP.CPPC++ 有人能帮助我如何从类中调用此函数参数以显示在输出中吗,c++,overloading,function-calls,C++,Overloading,Function Calls,我使用函数重载的代码是这样一种代码,用户必须输入4个主题的指针和它的学分,以便打印出他们的GPA。问题是我在Student中有3个参数(stringtest123、stringnama、stringvinto)。但我只想显示其中一个字符串。假设我想把葡萄酒打印出来。如何在Display函数中调用vinto,以便它打印出vinto。这是我的代码 CPP.CPP #include <iostream> #include "student.h" using namespace std;
#include <iostream>
#include "student.h"
using namespace std;
void main(void)
{
string nama;
string test123;
int i;
Student StudentA(test123, nama, "vinto");
cout << "key in points for 4 subject\n";
StudentA.Getpointers();
StudentA.Display(test123);
}
#包括
#包括“student.h”
使用名称空间std;
真空总管(真空)
{
字符串nama;
字符串test123;
int i;
学生(测试123,nama,“vinto”);
cout好吧,构造函数的vinto
参数没有保存在任何地方,因此在本例中,您无法将其取回。但是,您可以存储它:
首先,在类中添加一个vinto
字段:
class Student
{
public:
Student(string test123, string nama, string vinto );
void Getpointers();
void Display(string name);
private:
double points[4];
int creditHours[4];
string name;
string vinto;
double CalculateGPA();
};
然后,将vinto
参数值存储在此字段中:
Student::Student(string test123, string nama, string vinto)
{
name = nama;
this->vinto = vinto;
}
在此之后,您可以使用vinto:
void Student::Display(string name)
{
cout << "Hello " << name << endl;
cout << "Your current GPA is " << setprecision(3) << CalculateGPA() << endl;
cout << "Your vinto is " << vinto << endl;
}
void Student::Display(字符串名称)
{
有没有办法,我可以直接调用3个参数中的任何一个((string test123,string nama,string vinto))到Display()中,所以在最后它会打印出我在Display()中调用的参数。比如说,如果我想在Display()中显示vinto我想将其分配给值名。我不确定是否理解您的意思。方法中的代码(如Student::Display
)只能访问某些数据(例如,全局变量、方法参数和类成员),因此您应该将数据放在该内存中的某个位置。上面的代码示例将vinto
放在类成员中。这里可能更倾向于将其作为方法参数传递。
Student::Student(string test123, string nama, string vinto)
{
name = nama;
this->vinto = vinto;
}
void Student::Display(string name)
{
cout << "Hello " << name << endl;
cout << "Your current GPA is " << setprecision(3) << CalculateGPA() << endl;
cout << "Your vinto is " << vinto << endl;
}