0

I have a student array of objects inside of Course. How do i initialize the array size of assign to the Student name[]? should i use pointer or just array?

#include <iostream>
#include "student.h"
#include "course.h"

int main(int argc, char** argv) {

    Student student[4]; 
    Course computerClass(student);
    return 0;
}

#ifndef COURSE_H
#define COURSE_H
#include "student.h"
class Course
{
    private:
    Student name[];
    public:
        Course();
        Course(Student []);

};

 #endif

 #include "course.h"
 #include <iostream>
using namespace std;
Course::Course()
{
}

Course::Course(Student []){



}

1 Answer 1

1

You can use array only when you know array size at compile time, use std::vector when you do not:

#include <iostream>
#include "student.h"
#include "course.h"


int main(int argc, char** argv) {

    Students students(4); 
    Course computerClass(students);
    return 0;
}

#ifndef COURSE_H
#define COURSE_H
#include "student.h"

typedef std::vector<Student> Students;

class Course
{
    private:
        Students names;
    public:
        Course();
        Course(const Students &students);

};

 #endif

 #include "course.h"
 #include <iostream>
using namespace std;
Course::Course()
{
}

Course::Course(const Students &students) : names( students ) 
{
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but just wondering why do you use const in the parameter of constructor?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.