Vanishadow
Vanishadow

Reputation: 43

How to assign array inside the class object

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 []){



}

Upvotes: 0

Views: 218

Answers (1)

Slava
Slava

Reputation: 44238

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 ) 
{
}

Upvotes: 1

Related Questions