私は関数のオーバーロードを使用してこのコーディングは、ユーザーが4科目とその信用時間のポインタを入力する必要があるコードですGPA。私は学生の3つのパラメータを持っている(文字列test123、文字列nama、文字列vinto)。しかし、私は文字列のいずれかを表示したいだけです。私はVintoをプリントアウトしたいと言います。 Vintoを表示するには、Display関数でvintoを呼び出すにはどうしたらいいですか? Heresの私のコーディング。誰も私の出力に表示するクラスからこの関数のパラメータを呼び出す方法を助けることができます
CPP.cpp
#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
#include<iostream>
#include<string>
using namespace std;
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;
double CalculateGPA();
};
Student.cpp
#include <iostream>
#include <iomanip>
#include<string>
#include "student.h"
using namespace std;
Student::Student(string test123, string nama, string vinto)
{
name = nama;
}
void Student::Getpointers()
{
for (int i = 0; i<4; i++)
{
cout << "points for subject :" << i + 1 << endl;
cin >> points[i];
cout << "credit hour for subject " << i + 1 << endl;
cin >> creditHours[i];
}
}
void Student::Display(string name)
{
cout << "Hello " << name << endl;
cout << "Your current GPA is " << setprecision(3) << CalculateGPA() << endl;
}
double Student::CalculateGPA()
{
double totalpoints = 0;
int totalcreditHours = 0;
for (int i = 0; i<4; i++)
{
totalpoints += (points[i] * creditHours[i]);
totalcreditHours += creditHours[i];
}
return totalpoints/totalcreditHours;
}
Display()に3つのパラメータ((文字列test123、string nama、string vinto)のいずれかを呼び出すことができますので、最後にDisplay()で呼び出すパラメータを出力します。 。 vintoを最後に表示させたい場合は、ディスプレイ(vinto)を置いて、値の名前に割り当てる必要があります。 –
私はあなたを理解しているか分かりません。メソッド内のコード( 'Student :: Display'の中など)は、いくつかのデータ(グローバル変数、メソッド引数、クラスメンバーなど)のみにアクセスできるため、データをこのメモリのどこかに置く必要があります。上記のコード例は、クラスメンバに 'vinto'を置きます。おそらくメソッドの引数として渡すことはここで好まれます。 – alexeykuzmin0