Homework

I'm having trouble solving this assignment. I know some parts of the code are messed up because I have tried some things right before I placed it here and it didn't work. I don't necessarily need the solution but I want know if I am on the right track.

This is the question:

A person has a name, age and budget. A student is a person studying a number of courses in a department. A course has a code, title, credit, fee, and a grade. The sum of all courses fee is called tuition. The expenditures is the money the student spends on tuition, food, housing, and books.

Define a struct called Utilities with components food, housing and books.

Define a class called Course with private data members code, title, credit, fee, and grade. The class has the public member functions set(string code, string title, int credit) which reads the fee and the grade, getFee(), getCredit(), getGrade(), print( ), a default constructor, and a constructor with parameters.

Define a class called Person with private data members name, age, and budget. The class has the public member functions set(string name, int age) which reads the budget, get(string &, int &, int &), print( ), a default constructor, and a constructor with parameters.

Define a class called Student which publicly inherits the class Person and has the private data members department, numberOfCourses, tuition (the sum of all courses fee), living of type Utilities, and expenditures (sum of tuition, food, housing and books). The class has the public member functions set(string name, int age, string department, int numberOfCourses), print( ), a default constructor, and a constructor with parameters.
Implement all member functions of the three classes enforcing the least privileged principle and the principle of information hiding.
Use the following driver:

int main()
{
Student someOne("Ali Omar", 19, "Computer Science", 5);

someOne.print();

return 0;
}

Sample Output:


Enter the budget of Ali omar: 57000
Enter the information of 5 courses
Enter the course code, title, credit: 1411110 IT English, 3
Enter the course fee and grade: 4000 A
Enter the course code, title, credit: 1411113 Programming for Engineers, 3
Enter the course fee and grade: 4500 C
Enter the course code, title, credit: 1411116 Programming I, 3
Enter the course fee and grade: 5500 B
Enter the course code, title, credit: 1411211 Programming II, 3
Enter the course fee and grade: 4500 B
Enter the course code, title, credit: 1411215 Java Programming, 3
Enter the course fee and grade: 4500 C
Enter the cost of food, housing and books: 3500 5000 3500
The person name is: Ali Omar
The person age is: 19 and the budget is 57000
The student is in Computer Science department and studied 5 courses at the total cost of 35000

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
  #include <iostream>
using namespace std;

struct Utilities
{
    int food, housing, books;
};

class Course
{
    private:
        string code, title;
        int credit, fee;
        char grade;

    public:
        void set(string, string, int);
        int getFee();
        int getCredit();
        int getGrade();
        void print();

        Course();
        Course(string, string, int, int, char);
};

class Person
{
    private:
        string name;
        int age, budget;

    public:
        void set(string, int);
        void get(string &, int &, int &);
        void print();

        Person();
        Person(string, int, int);
};

class Student: public Person
{
    private:
        string department;
        int numberOfCourses, tuition, expenditures;

        Utilities living;

    public:
        void set(string, int, string, int);
        void print();

        Student();
        Student(string, int, string, int);
};

int main()
{
	Student someOne("Ali Omar", 19, "Computer Science", 5);

	someOne.print();

	return 0;
}

void Course::set(string cCode, string cTitle, int cCredit)
{
    code = cCode;
    title = cTitle;
    credit = cCredit;
}

int Course::getFee()
{
    return fee;
}

int Course::getCredit()
{
    return credit;
}

int Course::getGrade()
{
    return grade;
}

void Course::print()
{
    cout << "Enter information of 5 courses" << endl;

    for(int i = 0; i < 5; i++)
    {
        cout << "Enter the course code, title, credit: ";
        cin >> code >> title >> credit;

        cout << "Enter the course fee and grade: ";
        cin >> fee >> grade;

        tuition += fee;
    }
}

Course::Course()
{
    code = "";
    title = "";
    credit = 0;
    fee = 0;
    grade = 0;
}
Course::Course(string cCode, string cTitle, int cCredit, int cFee, char cGrade)
{
    set(cCode, cTitle, cCredit);
    fee = cFee;
    grade = cGrade;
}

void Person::set(string pName, int pAge)
{
    name = pName;
    age = pAge;
}

void Person::get(string & pName, int & pAge, int & pBudget)
{
    pName = name;
    pAge = age;
    pBudget = budget;
}

void Person::print()
{
    cout << "The person name is: Ali Omar" << endl;
    cout << "The person age is: 19 and budget is " << budget;
}

Person::Person()
{
    name = "";
    age = 0;
    budget = 0;
}

Person::Person(string pName, int pAge, int pBudget)
{
    cout << "Enter the budget of Ali Omar: " << pBudget;
    budget = pBudget;

    set(pName, pAge);
}

void Student::set(string sName, int sAge, string sDepartment, int sNumberOfCourses)
{
    Person::set(sName, sAge);
    department = sDepartment;
    numberOfCourses = sNumberOfCourses;

    tuition = Course::getFee();
}

void Student::print()
{
    int totalCost;

    cout << "Enter the budget of Ali Omar: ";
    cin >> budget;

    Course::print();

    cout << "Enter the cost of food, housing and books: ";
    cin >> living.food >> living.housing >> living.books;

    expenditures = living.food + living.housing + living.books;

    totalCost = expenditures + tuition;

    Person::print();

    cout << "The student is in" << department << " and studied 5 courses at the total cost of " << totalCost;
}

Student::Student()
{
    department = "";
    numberOfCourses = 0;
    tuition = 0;
    expenditures = 0;
}

Student::Student(string sName, int sAge, string sDepartment, int sNumberOfCourses)
{
    Person::set(sName, sAge);
    department = sDepartment;
    numberOfCourses = sNumberOfCourses;
}
I want know if I am on the right track.
I think you are, but there are a few problems:

a) void Course::print()
It should print, not ask the user questions.
Anyway: ‘tuition’ seems out of scope there (it’s defined in Student).

b) You don’t need to initialise std::strings (thanks to their constructor)

c) You’re asked to implement the “least privileged principle”: in this case it means you should declare const all the methods which can be declared const (e.g. Course::getFee(), Course::getCredit()...)

d) (Course)
set(string code, string title, int credit) which reads the fee and the grade
Does your Course::set() read the fee and the grade?

e) You could consider making your methods arguments const reference in many functions

f) Have you ever seen the member initializer list syntax for constructors?
http://en.cppreference.com/w/cpp/language/initializer_list

g) (Person)
set(string name, int age) which reads the budget
Does your Person::set() read the budget?

Topic archived. No new replies allowed.