A person has a name, age and budget. A student is a person studying a number of
ID: 3721894 • Letter: A
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 << (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 input / output:
Explanation / Answer
Here is the complete code. You can test the functions by inserting your own test cases in main()
/**************************/
/*
* Utilities.h
*
* Created on: 02-Mar-2018
* Author: tan
*/
#ifndef UTILITIES_H_
#define UTILITIES_H_
struct Utilities{
double food;
double housing;
double books;
};
#endif /* UTILITIES_H_ */
/*************************/
/*
* Course.h
*
* Created on: 02-Mar-2018
* Author: tan
*/
#include <string>
#include <iostream>
using namespace std;
#ifndef COURSE_H_
#define COURSE_H_
class Course {
string code;
string title;
int credit;
double fee;
string grade;
public:
Course();
Course( string code,string title,int credit,double fee, string grade);
void set(string code, string title, int credit);
double getFee();
int getCredit();
string getGrade();
void print();
};
#endif /* COURSE_H_ */
/**********************/
/*
* Course.cpp
*
* Created on: 02-Mar-2018
* Author: tan
*/
#include "Course.h"
Course::Course() {
// TODO Auto-generated constructor stub
}
Course::Course( string cd,string titl,int credt,double fees, string grd){
code =cd;
title=titl;
credit=credt;
fee=fees;
grade=grd;
}
void Course::set(string cd, string titl, int crdt){
code=cd;
title=titl;
credit=crdt;
}
double Course::getFee(){
return fee;
}
int Course::getCredit(){
return credit;
}
string Course::getGrade(){
return grade;
}
void Course::print(){
cout<<"Printing Course information"<<endl;
cout<<"Code: "<<code<<", Title: "<<title<<", Credit: "<<credit<<endl;
cout<<"Fees: "<<fee<<" Grade: "<<grade<<endl;
}
/*************************/
/*
* Person.h
*
* Created on: 02-Mar-2018
* Author: tan
*/
#include <string>
using namespace std;
#ifndef PERSON_H_
#define PERSON_H_
class Person {
string name;
int age;
double budget;
public:
Person();
Person(string name,int age);
void set(string name,int age);
void get(string&,int&,int&);
void print();
};
#endif /* PERSON_H_ */
/*********************/
/*
* Person.cpp
*
* Created on: 02-Mar-2018
* Author: tan
*/
#include "Person.h"
Person::Person() {
// TODO Auto-generated constructor stub
}
Person::Person(string nm,int ag){
name=nm;
age=ag;
}
void Person::set(string nm,int ag){
name=nm;
age=ag;
}
void Person::get(string& nm,int& ag,int& budg){
nm=name;
ag=age;
budg=budget;
}
/*********************/
/*
* Student.h
*
* Created on: 02-Mar-2018
* Author: tan
*/
#include <string>
#include <iostream>
#include "Utilities.h"
#include "Person.h"
using namespace std;
#ifndef STUDENT_H_
#define STUDENT_H_
class Student:public Person {
string department;
int numberOfCourses;
double tution;
Utilities utl;
public:
Student();
Student (string name, int age, string dept, int noOfCourse);
void set(string name, int age, string department, int numberOfCourses);
void print();
};
#endif /* STUDENT_H_ */
/*********************/
/*
* Student.cpp
*
* Created on: 02-Mar-2018
* Author: tan
*/
#include "Student.h"
Student::Student() {
// TODO Auto-generated constructor stub
Person();
}
Student::Student(string name, int age, string dept, int noOfCourse):Person(name,age){
department=dept;
numberOfCourses=noOfCourse;
}
void Student::set(string name, int age, string dept, int noOfCourse){
Person::set(name,age);
department=dept;
numberOfCourses=noOfCourse;
}
void Student::print(){
string name;
int age;
int budget;
cout<<"Printing Student Information"<<endl;
get(name,age,budget);
cout<<"Name: "<<name<<", Age: "<<age<<", budget: "<<budget<<endl;
cout<<"Department: "<<department<<", Number of Course: "<<numberOfCourses<<endl;
}
/*********************/
//============================================================================
// Name : Academic.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include "Course.h"
#include "Person.h"
#include "Student.h"
using namespace std;
int main() {
Student someOne("Ali Omar", 19, "Computer Science", 5);
someOne.print();
Course engg("CSE01","Engineering",200,5000,"A");
engg.print();
return 0;
}
/**********************/sample output
Printing Student Information
Name: Ali Omar, Age: 19, budget: 0
Department: Computer Science, Number of Course: 5
Printing Course information
Code: CSE01, Title: Engineering, Credit: 200
Fees: 5000Grade: A
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.