Classes help

I need help creating object in main for class that accepts other classes as part of that class. Feel free to suggest any other fixes. Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::string;
using std::endl;

class Instructor{
    
private:
    string lastName;
    string firstName;
    string officeNumber;
    
public:
    
    Instructor(const string *lname, const string *fname, const string *office);
    Instructor thomas(string &Instructor);
    
    void setInfo(const string *lname, const string *fname, const string *office)
    {
        lastName = *lname;
        firstName = *fname;
        officeNumber = *office;
    }
    
    void printInfo()const
    {
        cout << "Instructor Information: " << endl;
        cout << "Last name: " << lastName << endl;
        cout << "First name: " << firstName << endl;
        cout << "Office: " << officeNumber << "\n" << endl;
    }
};

class Textbook{
    
private:
    string title;
    string author;
    string publisher;

public:
    
    Textbook(const string *textTitle, const string *auth, const string *pub);
    Textbook econ(string &Textbook);
    
    void setInfo(const string *textTitle, const string *auth, const string *pub)
    {
        title = *textTitle;
        author = *auth;
        publisher = *pub;
    }
    
    void printInfo()const
    {
        cout << "Textbook Information: " << endl;
        cout << "Title: " << title << endl;
        cout << "Author: " << author << endl;
        cout << "Publisher: " << publisher << "\n" << endl;
    }
};

class Course{
    
private:
    string courseName;
    Instructor instructor;
    Textbook textbook;
    
public:
    Course(const string *name, const string &instructor, const string &textbook);
    
    void setInfo(const string *name)
    {
        courseName = *name;
    }
    
    void printInfo()const
    {
        cout << "Course Information: " << endl;
        cout << "Course Name: " << courseName << "\n";
        instructor.printInfo();
        textbook.printInfo();
    }
};

int main()
{
    Course (economics, instructor, textbook);
    string lname = "Gray";
    string fname = "Thomas";
    string office = "DLS 313";
    
    const string *i = &lname;
    const string *u = &fname;
    const string *y = &office;
    instructor.setInfo(i, u, y);
    
    string title = "Microeconomics";
    string author = "Wiley and Sons";
    string publisher = "McDaniel Publishing";
    
    const string *t = &title;
    const string *r = &author;
    const string *e = &publisher;
    textbook.setInfo(t, r, e);
    
    string course = "Economics 214";
    const string *ptr = &course;
    economics.setInfo(ptr);
    economics.printInfo();
    
    return 0;
}
Last edited on
Have you even tried to compile this and correct the errors reported by the compiler?

Line 18: What is this line supposed to be?

Line 46: Ditto

Line 90: Course is a class name. You need a variable name to instantiate a Course.

Line 90: economics, instructor, textbook are all undefined variables.

Line 98: Instructor is undefined.

Line 95-97: These pointers are not needed. Simply dereference the variables when you call setInfo().
 
  instructor.setInfo (&lname, &fname, &office);

Better yet, pass by const reference rather than using pointers.

Line 107: textbook is undefined.

Lines 104-106: ditto regarding no need for pointers.

Line 111-112: economics is undefined.

Line 110: ditto regarding no need for pointer.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::string;
using std::endl;

class Instructor
{   string lastName;
    string firstName;
    string officeNumber;

public:
    //  Default constructor required
    Instructor ()
    {}
    
    //  Pass arguments by const reference
    Instructor (const string & lname, const string & fname, const string &office)
    //  Add body to constructor
    {   lastName = lname;
        firstName = fname;
        officeNumber = office;
    }
    
    void printInfo() const
    {   cout << "Instructor Information: " << endl;
        cout << "Last name: " << lastName << endl;
        cout << "First name: " << firstName << endl;
        cout << "Office: " << officeNumber << "\n" << endl;
    }
};

class Textbook
{   string title;
    string author;
    string publisher;

public:
    //  Default constructor required
    Textbook ()
    {}
    
    //  Pass arguments by const reference
    Textbook (const string & textTitle, const string & auth, const string & pub)
    //  Add body to constructor
    {   title = textTitle;
        author = auth;
        publisher = pub;
    }

    void printInfo()const
    {   cout << "Textbook Information: " << endl;
        cout << "Title: " << title << endl;
        cout << "Author: " << author << endl;
        cout << "Publisher: " << publisher << "\n" << endl;
    }
};

class Course
{   string courseName;
    Instructor instructor;
    Textbook textbook;

public:
    //  Pass arguments by const reference
    //  Correct argument types
    Course (const string & name, const Instructor & inst, const Textbook & book)
    //  Add body to constructor
    {   courseName = name;
        instructor = inst;
        textbook = book;
    }        

    void printInfo()const
    {   cout << "Course Information: " << endl;
        cout << "Course Name: " << courseName << "\n";
        instructor.printInfo();
        textbook.printInfo();
    }
};

int main()
{   //  Create objects 
    Instructor  gray ("Gray", "Thomas", "DLS 313");
    Textbook    book ("Microeconomics", "Wiley and Sons", "McDaniel Publishing"); 
    Course course ("Economics 214", gray, book);
    
    course.printInfo ();
    return 0;
}

Last edited on
Thanks!
Topic archived. No new replies allowed.