brajevicm
brajevicm

Reputation: 47

Storing Objects in an Array of a second Object

I have to create a small console application in C++ which will do the following:

Create class Subject which has next attributes: name of the subject, number of students and array of students who are attending that subject. After that createa class Student which has name and surname of the student as attributes. In Main file count how many duplicate names there are in each Subject.

I have few problems here. First one is that I don't know how to initialize an array in my Subject.h file. Second is how to actually put Student objects into Subject objects and compare the names in the end.

What I'd like output to look like:

Duplicate names in subjectA are: Michael.

Duplicate names in subjectB are: Nicholas, John.

Where subjectA and subjectB should be called C++ and C.

Here is my code so far (I googled for past hour or two about this problem of mine but I just couldn't find a proper answer/example).

NOTE: I'm including all these files for clarification.

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;
}

Upvotes: 0

Views: 86

Answers (2)

Mohammed Li
Mohammed Li

Reputation: 901

1) Get an array of Students into your Subject object: you probably want to use vectors instead of arrays here: in subject.h add

#include "Student.h"

public:
    void addStudent(Student student);
private:
    std::vector<Student> students_;

in subject.cpp add

void Subject::addStudent(Student student)
    {
        this->students_.push_back(student);
    }

If you want to extract the student list somehow later, you need to write a function to access it (or make it public).

2) For finding duplicates, look here Checking for duplicates in a vector

You have to pay attention: the Student objects are in your subject object, not the student names. You have to extract them first and e.g. put them in a vector.

Upvotes: 1

David
David

Reputation: 13

Your task definition sais you should have an array of Students attribute of the Subject class, but i don't see this in your Subject class definition. And maybe an add Student method and then iterate over the array.

Upvotes: 0

Related Questions