假設我有:任何人都可以幫助我使用C++類嗎?
我的頭文件:
class student{
public:
void setStudent(char *, char *, int, char, char *);
char getStudent();
void printStudent();
private:
char *name;
char *major;
int id;
float gpa;
char *city;
}
我implementation.cpp:
void Student::setStudent(char *sName, char *sMajor, int sID, float sGPA , char *sCITY){
//set def student.
name = sName;
major = sMajor;
ID = sID;
gpa = sGPA;
city = sCITY;
}
char student::getStudent(){
}
void student::printStudent(){
cout << "Name: " << sName << endl;
cout << "Major: " << sMajor << endl;
cout << "ID: " << sID << endl;
cout << "GPA: " << sGPA << endl;
// city will be the "labels", no need to print.
}
- 我會讀student.txt
- 將所有信息存儲到卡中。
我的問題是,對於我的getStudent()
,我應該製作五個不同的功能,如getStudentName() { return name; } ... getStudentMajor() { return major; }
?或者無論如何我都可以在我的getStudent();
中做一次,因爲我不認爲我可以同時返回所有的班級成員。
我應該使用指針函數嗎?我需要使用strcpy
和strcmp
。
PS:所有數組必須仍然是動態聲明的並且指針用於數據操作。
另外,在我的readFile() { ifstream fin; }
中,如何使用fin
調用類函數? fin >> getStudentName;
或fin = getStudentName;
?
閱讀文本文件和類ADT時有什麼需要注意的嗎?
讓你的代碼編譯第一 –
我建議你使用'的std :: string否則你就需要擔心這些字符串的存在位置。 –
@NeilKirk我不能使用字符串。 – 7llllll