从类c ++动态创建数组

时间:2017-08-05 16:26:51

标签: c++ arrays class

首先把它扔出去我还在学习如何在学校开课。我有一个问题是读入一个动态创建的数组,其中包含指向我的一个类的指针。函数readClassArray()没有从student.getCreditNumber获取变量。该程序在Visual Studio中符合要求但是当我得到readClassArray它只是跳过函数,因为s.getCreditNumber返回0.感谢高级的任何帮助。

#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
using namespace std;

class Courses{
 private:
  int courseNumber;
  double hours;
  string courseName;
  char grade;
public:
void setCourseNumber(int n){courseNumber = n; }
void setCreditHours(double c) { hours = c; }
void setCourseName(string n) { courseName = n; }
void setGrade(char g) { grade = g; }
int getCourseNumber() { return courseNumber; }
double getCreditHours() { return hours; }
string getCourseName() { return courseName; }
char getGrade() { return grade; }
};
class Student : public Courses{
 private:
  string firstName;
  string lastName;
  string studentNumber;
  int creditNumber;
  double gpa;
 public:
 Courses * courses;
  Student() {
   firstName = " ";
   lastName = " ";
   studentNumber = " ";
   creditNumber = 0;
   gpa = 0.0;
   courses = NULL;
 }
~Student() {
    delete[] courses;
};
void setFirstName(string n) { firstName = n; }
void setLastName(string l) { lastName = l; }
void setStudentNumber(string a) { studentNumber = a; }
void setCreditNumber(int num) { creditNumber = num; }
string getFirstName() { return  firstName; }
string getLastName() { return lastName; }
string getStudentNumber() { return studentNumber; }
int getCreditNumber() { return creditNumber; }
};
#endif

Student.cpp

#include "Student.h"
#include <iostream>
#include <string>

 using namespace std;

void readStudent();
void readCourseArray();
void computeGPA();
void printSummary();

void readStudent() {
Student a;
string number;
string firstName;
string lastName;
int courses;

cout << "Enter student number: ";
cin >> number;
a.setStudentNumber(number);
cout << "Enter student first name: ";
cin >> firstName;
a.setFirstName(firstName);
cout << "Enter student last name: ";
cin >> lastName;
a.setLastName(lastName);
cout << "Enter student number of courses: ";
cin >> courses;
a.setCreditNumber(courses);

cout << "\n"; }


void readCourseArray(){
Student s;
s.courses = new Courses[s.getCreditNumber()];

int num;
double cHours;
string cName;
char grade;
cout << "test" << endl;
for (int i = 0; i < s.getCreditNumber(); i++){
    cout << "Enter class " << i + 1 << " number: ";
    cin >> num;
    s.courses[i].setCourseNumber(num);
    cout << "Enter class " << i + 1 << " name: ";
    cin >> cName;
    s.courses[i].setCourseName(cName);
    cout << "Enter class " << i + 1 << " hours: ";
    cin >> cHours;
    s.courses[i].setCreditHours(cHours);
    cout << "Enter class " << i + 1 << " grade: ";
    cin >> grade;
    s.courses[i].setGrade(grade);
    cout << "\n";
}
}

1 个答案:

答案 0 :(得分:0)

readCourseArray开始时,您已创建s。当发生这种情况时,creditNumber成员的值为0,由默认构造函数设置。您需要做一些事情来将其设置为非零值。如果您期望readStudent中设置的值继续结束,则需要将两个函数连接在一起。传入Student对象作为每个函数的引用,或让readStudent返回Student个对象并将其传递给readCourseArray