我要创建C++中的小型控制台应用程序,这将做到以下几点:在第二个对象的数组存储对象
创建类话题为接下来的属性:对象的名字,学生和阵列的数量的参加该科目的学生。之后,创建一个名为学生姓名的学生作为属性。在主文件中统计每个主题有多少重复名称。
我在这里几乎没有问题。首先,我不知道如何初始化Subject.h
文件中的数组。其次是如何实际将Student
对象放入Subject
对象中,并最终比较名称。
想输出什么我看起来像:
在subjectA
重复的名称是:迈克尔。
subjectB
重复的名字是:Nicholas,John。
其中subjectA
和subjectB
应该被称为C++
和C
。
这是我的代码到目前为止(我搜索了一两个关于我的这个问题,但我只是找不到正确的答案/例子)。
注:我包括所有这些文件的澄清。
Subject.h
#include <string>
#include <iostream>
using namespace std;
/*
* I should also have an array named `arrayOfStudents`
* which should store all students who are attending
* that Subject.
*/
class Subject
{
public:
Subject();
Subject(Subject &subject);
Subject(string nameOfSubject, int numberOfStudents);
~Subject();
const string getNameOfSubject();
const int getNumberOfStudents();
void setNameOfSubject(string nameOfSubject);
void setNumberOfStudents(int numberOfStudents);
void print();
private:
string nameOfSubject;
int numberOfStudents;
};
Subject.cpp
#include <iostream>
#include "Subject.h"
using namespace std;
Subject::Subject()
{
}
Subject::Subject(string nameOfSubject, int numberOfStudents)
{
this->nameOfSubject = nameOfSubject;
this->numberOfStudents = numberOfStudents;
}
Subject::Subject(Subject &Subject) : nameOfSubject(Subject.getNameOfSubject()), numberOfStudents(Subject.getNumberOfStudents())
{
}
Subject::~Subject()
{
cout << "Object is destroyed" << endl;
}
const string Subject::getNameOfSubject()
{
return nameOfSubject;
}
const int Subject::getNumberOfStudents()
{
return numberOfStudents;
}
void Subject::setNameOfSubject(string nameOfSubject)
{
nameOfSubject = this->nameOfSubject;
}
void Subject::setNumberOfStudents(int numberOfStudents)
{
numberOfStudents = this->numberOfStudents;
}
void Subject::print()
{
cout << "Subject: " << nameOfSubject << " :: Number of students: " << numberOfStudents << endl;
}
Student.h
#include <string>
#include <iostream>
using namespace std;
class Student
{
public:
Student();
Student(Student &student);
Student(string name, string surname);
~Student();
const string getName();
const string getSurname();
void setName(string name);
void setSurname(string surname);
void print();
private:
string name;
string surname;
};
Student.cpp
#include <iostream>
#include "Student.h"
using namespace std;
Student::Student()
{
}
Student::Student(string name, string surname)
{
this->name = name;
this->surname = surname;
}
Student::Student(Student &student) : name(student.getName()), surname(student.getSurname())
{
}
Student::~Student()
{
cout << "Object is destroyed" << endl;
}
const string Student::getName()
{
return name;
}
const string Student::getSurname()
{
return surname;
}
void Student::setName(string name)
{
name = this->name;
}
void Student::setSurname(string surname)
{
surname = this->surname;
}
void Student::print()
{
cout << "Student: " << name << " " << surname << endl;
}
Main.cpp的
#include <iostream>
#include "Subject.h"
#include "Student.h"
using namespace std;
int main()
{
/*
* First three students should attend first Subject
* while other four the second Subject.
* Also note that only names matter and not surnames.
*/
Student stA("Michael", "Doe");
Student stB("Michael", "Doe");
Student stC("Thomas", "Doe");
Student stD("Nicholas", "Doe");
Student stE("Nicholas", "Doe");
Student stF("John", "Doe");
Student stG("John", "Doe");
Subject subjectA("C++", 3);
Subject subjectB("C", 4);
return 0;
}
'nameOfSubject = this-> nameOfSubject;'在'setNameOfSubject'和其他的后面。但是我没有看到有关打印“重复名称......”的任何代码,所以,谁知道? –
如果你想得到很好的回复请发表[MCVE] –