[英]C++: How can I pass data from an object to an array of another object via a third object?
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 学生.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 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 Course.cpp
#include "Course.h"
#include "Student.h"
using namespace std;
Course::Course(){
}
Course::~Course(){
}
string Course::getCourseName() {
return this->course_name;
}
Main.cpp 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;
}
Now , how do I pass all the three student objects to the course object? 现在 ,如何将所有三个学生对象传递给课程对象?
Original Question Link : https://courses.edx.org/courses/course-v1:Microsoft+DEV210.2x+3T2016/info 原始问题链接 : https : //courses.edx.org/courses/course-v1 : Microsoft+ DEV210.2x+3T2016 / info
Refer to "Module Two" -> "Lab" 参见“模块二”->“实验室”
You should create methods named getters
and setters
. 您应该创建名为
getters
和setters
方法。 They are the best way to pass information between objections. 它们是在异议之间传递信息的最佳方法。 For example, if you want to get to know what the name of the student is, you simply make a public method for your Student:
例如,如果您想了解学生的名字,您只需为学生创建一个公共方法:
string Student::getFname() {return fname; }
The solve of your problem is just making an appropriate method for setting the students in your course. 解决您的问题的方法只是为课程设置合适的学生。 It might look like that:
可能看起来像这样:
void Course::setStudents(Student s1, Student s2, Student s3)
{
student[0] = s1;
student[1] = s2;
student[2] = s3;
}
Or just make another method to add them on a specific position. 或者只是采用另一种方法将它们添加到特定位置。
Since the student array is private you need a class method to access it, so you add a new method to course, for example: 由于学生数组是私有的,因此您需要一个类方法来访问它,因此您需要向课程中添加一个新方法,例如:
For example, 2 would be like: 例如,2就像:
Course.h: Course.h:
...
public:
bool addStudent(Student s, short pos);
Course.c: Course.c:
bool addStudent(Student s, short pos){
if(pos >=0 && pos <= 2){
student[pos] = s;
return true;
}
return false;
}
And main.c: 和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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.