user3341518
user3341518

Reputation: 57

Creating a class instance with parameters

This is the constructor in the class:

Course(int courseId, Instructor instructor, string courseName, string dept) 
    : courseId(courseId)
    , instructor(instructor)
    , courseName(courseName)
    , dept(dept)
{ };

My problem is with the second argument Instructor instructor. What exactly does this mean because I have never seen mixing of two classes like this?

Upvotes: 3

Views: 20840

Answers (1)

Joseph Mansfield
Joseph Mansfield

Reputation: 110768

It means you need to pass an Instructor object to it, just as the first parameter means it takes an int object, and the third and fourth take string objects. For example:

int courseId = 0;
Instructor instructor; // Here we default construct an Instructor
std::string courseName = "Foo";
std::string dept = "Bar";

Course my_course(courseId, instructor, courseName, dept);
//                         ^^^^^^^^^^
//              Here the Instructor is being passed

That declaration of instructor will only work if Instructor has a default constructor, which I'm guessing it doesn't. If the constructor for Instructor has some parameters, then you need to pass them like so:

Instructor instructor(some, params, here);

Upvotes: 7

Related Questions