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'有什么想法吗? –