Below are the requirements for all the classes and the relationship between them
ID: 3597205 • Letter: B
Question
Below are the requirements for all the classes and the relationship between them.
Important: You may use vectors for this assignment if you choose so. Please write in C++.
Note: For each class, you are required to create necessary functions such as constructors, setters, getters, destructors to make the program work.
University Class
The University class contains the following member variables:
name: name of the university.
The name of the university MUST be “University of Alabama”
buildings: contains n number of Building objects
people: contains m number of Person objects
It contains the following member functions:
A function that prints the information on all the buildings in its information system (name, address, and building’s size)
A function that prints the information of all the people (name, age, GPA or Rating)
Note: The information on Building class and Person class are described below.
Note: When printing information of all people, you need to distinguish instructors and students by print either a “GPA” or “Rating”.
Building Class
The Building class contains the following member variables:
name: name of the building
size: the size of the building (in sqft)
address: address of the building (stored as string)
Note: No additional member function required for this class.
Person Class
Person class is polymorphic, a Person pointer can hold an Instructor/Student object.
The Person class contains the following member variables:
name: name of the person
age: age of the person
The age of a person can be randomized or from input, but make it realistic
The Person class contains the following member function:
A function called “do_work()” that generates a random number that represents how many hours they will do work for, and then output message depending on if the Person is a Student or an Instructor.
Note: You can find more information on what message to output below.
Note: You can set a range for the random number to make the working hours look reasonable.
Student Class
The Student class contains the following member variable:
GPA, the student’s GPA (double or float data type)
The range for GPA must be between 0.0 and 4.0.
For “do_work()” in Person class, if the Person is a Student, then the function should output the following message:
“PERSON_NAME did X hours of homework.” Where PERSON_NAME is the person’s name, and X is the randomly generated number.
Instructor Class
The Instructor class contains the following member variable:
rating, the instructor’s rating (double or float data type)
The range for rating must be between 0.0 and 5.0.
For “do_work()” in Person class, if the Person is an Instructor, then the function should output the following message:
“PERSON_NAME graded papers for X hours.” Where PERSON_NAME is the person’s name, and X is the randomly generated number.
Menu
The menu must have at least the following options:
Prints information about all the buildings
Prints information of everybody at the university
Choose a person to do work
Exit the program
If option 3 is selected, the program should print another menu that prints all the people’s name and let the user input the choice of the person the user would like, to do work.
When the program starts, you need to manually instantiate at least 1 student, 1 instructor, and 2 buildings inside the University object. It can be done by either hard coding that information, or have the user input all the information at the start of the program.
You can add more options in the menu such as adding more Person/Buildings, or create other functions/variables to modularize code and complete the lab. It is not a hard requirement.
Explanation / Answer
Program for above question:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
using namespace std;
class Buildings
{
char *name;
float size;
char *address;
public:
Buildings()
{
size=0;
}
Buildings(char *n,float s,char * a)
{
this->name=n;
this->size=s;
this->address=a;
}
void setname(char *c)
{
this->name=c;
}
void setsize(float s)
{
this->size=s;
}
void setaddress(char *a)
{
this->address=a;
}
void printinfo()
{
cout<<"name of building== "<<this->name<<" ";
cout<<"size of building(sqft)== "<<this->size<<" ";
cout<<"address of building =="<<this->address<<" ";
}
};
class Person
{
char *name;
int age;
public:
int type;
void setname(char* n)
{
this->name=n;
}
char* getname()
{
return name;
}
void setage(int n)
{
this->age=n%100;
}
int getage()
{
return this->age;
}
void do_work()
{
cout<<"person do work function ";
}
void getgpa(float f)
{
//parent class
}
void getrating(float f)
{
//parent class
}
float pgpa()
{
//parent class
}
float prating()
{
//parent class
}
};
class student: public Person
{
float gpa;
int whours;
public:
virtual void getgpa(float f)
{
this->gpa=f;
this->type=0;
}
virtual float pgpa()
{
return this->gpa;
}
virtual void do_work()
{
this->whours=rand()%20;
cout<<this->getname()<<"did"<<this->whours<<"hours of homework ";
}
};
class instructor: public Person
{
float rating;
int whours;
public:
virtual void getrating(float f)
{
this->rating=f;
this->type=1;
}
virtual float prating()
{
return this->rating;
}
virtual void do_work()
{
this->whours=rand()%20;
//cout<<this->getname()<<"graded papers for"<<this->whours<<"hours of homework ";
cout<<this->getname()<<"graded papers for"<<this->whours<<"hours of homework ";
}
};
class university
{
char *name="University of Alabama";
int n,m;
public:
Buildings *b;
Person *p;
university(int n1,int m1)
{
this->n=n1;
this->m=m1;
b=new Buildings[this->n];
p=new Person[this->m];
}
void bprint()
{
for(int i=0;i<this->n;i++)
this->b[i].printinfo();
}
void pprint()
{
for(int i=0;i<this->m;i++)
{
cout<<"name=="<<this->p[i].getname()<<" ";
cout<<"age=="<<this->p[i].getage()<<" ";
if(this->p[i].type==0)
{
cout<<"gpa=="<<this->p[i].pgpa()<<" ";
}
else if(this->p[i].type==1)
cout<<"rating=="<<this->p[i].prating()<<" ";
}
}
};
int main()
{
university u(2,2);student s;instructor i;Person *po;
u.b[0].setname("csdeptt");
u.b[0].setsize(132);
u.b[0].setaddress("newyork");
u.b[1].setname("civildeptt");
u.b[1].setsize(122);
u.b[1].setaddress("newyork");
u.p[0].setname("mohit");
u.p[1].setname("kuldeep");
u.p[0].setage(17);
u.p[1].setage(25);
u.b[0].printinfo();
u.b[1].printinfo();
u.p[0].getgpa(3.5);
u.p[1].getrating(4.0);
u.pprint();
// you are required to call do_work() for student and instructor in your way
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.