C++新手在這裏,這是我的第一篇文章。我在學校的一個項目上工作,我有點卡住了。我的任務是創建一個課程名冊。每個名冊將包含課程名稱,課程代碼,課程學分和教授姓名。沒問題,我有一個花名冊班。問題是我不確定如何製作一個對象數組動態,因爲它必須能夠在用戶請求時增長和縮小。我對動態數組一般比較熟悉,但不確定動態數組對象的語法。而且,按照教授的指示,載體不是的選項。我搜索了這個論壇以及在互聯網上的其他領域,並沒有找到答案,或者我不理解我找到的答案,並認爲我會在這裏發佈。以下是我的非動態數組對象的代碼。幫助轉換爲動態數組將非常感激。謝謝!C++動態數組對象
StudentEnrollment.h:
#ifndef STUDENTENROLLMENT_H
#define STUDENTENROLLMENT_H
# include <iostream>
# include <string>
using namespace std;
class Roster {
private:
string courseName;
string courseCode;
string courseCredits;
string professorName;
public:
void setCourseName (string);
void setCourseCode (string);
void setCourseCredits (string);
void setProfessorName (string);
string getCourseName();
string getCourseCode();
string getCourseCredits();
string getProfessorName();
Roster();
};
#endif;
StudentEnrollment.cpp:
#include <iostream>
#include <string>
#include "StudentEnrollment.h"
using namespace std;
// Roster class implementation
Roster::Roster() {
courseName = "";
courseCode = "";
courseCredits = "";
professorName = "";
}
void Roster::setCourseName (string cn) {
courseName = cn;
}
void Roster::setCourseCode (string c) {
courseCode = c;
}
void Roster::setCourseCredits (string cc) {
courseCredits = cc;
}
void Roster::setProfessorName (string pn) {
professorName = pn;
}
string Roster::getCourseName() {
return courseName;
}
string Roster::getCourseCode() {
return courseCode;
}
string Roster::getCourseCredits() {
return courseCredits;
}
string Roster::getProfessorName() {
return professorName;
}
main.cpp中:
#include <iostream>
#include <string>
#include "StudentEnrollment.h"
using namespace std;
int main (int argc, char * const argv[]) {
int number_of_rosters = 0;
string course, code, credits, name;
cout << "Enter the number of rosters you would like to create: ";
cin >> number_of_rosters;
cin.ignore(100, '\n');
Roster roster[number_of_rosters];
for (int i = 0; i < number_of_rosters; i++){
cout << "Enter course name: ";
getline(cin,course);
roster[i].setCourseName(course);
cout << "Enter course code; ";
getline(cin, code);
roster[i].setCourseCode(code);
cout << "Enter course credits: ";
getline(cin, credits);
roster[i].setCourseCredits(credits);
cout << "Enter professor name: ";
getline(cin, name);
roster[i].setProfessorName(name);
cout << "Next course..." << endl;
}
cout << endl;
for (int i = 0; i < number_of_rosters; i++){
cout << roster[i].getCourseName() << endl;
cout << roster[i].getCourseCode() << endl;
cout << roster[i].getCourseCredits() << endl;
cout << roster[i].getProfessorName() << endl;
cout << endl;
}
return 0;
}
原諒我,如果這沒有正確格式化。這是我的第一篇文章。
亞瑟
你給'linklists'有什麼想法嗎? –