Viktor Anastasov
Viktor Anastasov

Reputation: 1113

Adding objects of one class to the data of another class

I have a class Course with information about a course in the university (like when in starts, how long it is, which day of the week, etc). Now I have to do a class Semester with an undefined length of courses :

class Semester {
    public :
       void addCourse(Course c);
    private :
       Course* courses;
}

So I'd like to know how can I do the addCourse function so that I can add infinite number of courses (class Course) to my class Semester ?

Upvotes: 1

Views: 65

Answers (1)

Dimitrios Bouzas
Dimitrios Bouzas

Reputation: 42939

Take a look at the code below:

class Semester {
    public :
       void addCourse(Course *c) { courses.push_back(c); }
    private :
       std::vector<Course*> courses;
};

Upvotes: 1

Related Questions