C ++:如何通过第三个对象将数据从对象传递到另一个对象的数组?

时间:2017-01-02 21:14:49

标签: c++ c++11 c++14

**原始问题的链接在底部**

请参阅“Course.h”,“Course.cpp”和“main.cpp”代码

Student.h

#pragma once

#include <string>

using namespace std;

class Student {
    private:
        string fname;
        string lname;

        int age;
        string address;
        string city;
        string phone;
    public:
        Student();
        ~Student();

        Student(string, string, int, string, string, string);


};

Student.cpp

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

using namespace std;

Student::Student(){
}
Student::~Student(){
}

Student::Student(string _fname, string _lname, int _age, string _address, string _city, string _phone) {
    fname = _fname;
    lname = _lname;

    age = _age;
    address = _address;
    city = _city;
    phone = _phone;
}

Course.h

#pragma once

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

class Course {
    private:
        string course_name = "Intermediate C++";
        Student student[3];    // Need data of three students
    public:
        Course();
        ~Course();

        // *Create some constructor to pass student data*

        string getCourseName();
};

Course.cpp

#include "Course.h"
#include "Student.h"

using namespace std;

Course::Course(){
}
Course::~Course(){
}

string Course::getCourseName() {
    return this->course_name;
}

Main.cpp的

#include <iostream>

#include "Student.h"
#include "Course.h"
using namespace std;

int main() {
    Student student1("fName_1","lName_1",18,"address_1","city_1","phone_1");
    Student student2("fName_2","lName_2",19,"address_2","city_2","phone_2");
    Student student3("fName_3","lName_3",20,"address_3","city_3","phone_3");

    Course *course = new Course;



    system("pause");
    return 0;
}

现在,如何将所有三个学生对象传递给课程对象?

原始问题链接https://courses.edx.org/courses/course-v1:Microsoft+DEV210.2x+3T2016/info

请参阅“第二单元” - &gt; “实验室”

2 个答案:

答案 0 :(得分:1)

您应该创建名为getterssetters的方法。它们是在异议之间传递信息的最佳方式。例如,如果您想了解学生的姓名,您只需为您的学生制作一个公共方法:

string Student::getFname() {return fname; }

解决问题只是在课程中设置适当的方法来设置学生。它可能看起来像这样:

void Course::setStudents(Student s1, Student s2, Student s3)
{
   student[0] = s1;
   student[1] = s2;
   student[2] = s3;
}

或者只是制作另一种方法将它们添加到特定位置。

答案 1 :(得分:1)

由于学生数组是私有的,因此您需要一个类方法来访问它,因此您需要在课程中添加一个新方法,例如:

  1. 访问阵列本身
  2. 添加元素
  3. ...
  4. 例如,2就像:

    <强> Course.h:

    ...
    public:
        bool addStudent(Student s, short pos);
    

    <强> Course.c:

    bool addStudent(Student s, short pos){
        if(pos >=0 && pos <= 2){
            student[pos] = s;
            return true;
        }
        return false;
    }
    

    和 的 main.c中:

    int main() {
        Student student1("fName_1","lName_1",18,"address_1","city_1","phone_1");
        Student student2("fName_2","lName_2",19,"address_2","city_2","phone_2");
        Student student3("fName_3","lName_3",20,"address_3","city_3","phone_3");
    
        Course *course = new Course;
    
        course->addStudent(student1,0);
        course->addStudent(student2,1);
        course->addStudent(student3,2);
    
        system("pause");
        return 0;
    }