Creating a class instance with parameters

13,179

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);
Share:
13,179
user3341518
Author by

user3341518

Updated on June 04, 2022

Comments

  • user3341518
    user3341518 almost 2 years

    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?