Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Program: Goals Implement a program using inheritance and polymorphism Get mo

ID: 3713348 • Letter: C

Question

C++ Program:

Goals

Implement a program using inheritance and polymorphism

Get more practice on classes and objects

In this lab, we will write an information system for Oregon State University. The purpose of this lab is to help you understand the basic concepts of inheritance and polymorphism.

Requirements

This program is a simple representation of an Oregon State information system that contains information about the university.

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.

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 “Oregon State University”

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)

You are encouraged to get the actual address and sizes of buildings that exist on the actual campus, here is the link to look this up, but this is not a hard requirement.

http://facilities.oregonstate.edu/buildings (Links to an external site.)Links to an external site.

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.

Style Guideline

Review the “Things not to do in the code” page and the “Things you need to do in your code” page as you will be held to those requirements. Make sure you indent the code correctly, and comment on code appropriately.

Extra Credit 15%

Add options that have the following features:

Save the information to a file

Read a saved information from a file

Add people to the program during runtime.

This will allow the user to add more people to the university, and close the program without losing all the information.

Note: This is an all or nothing extra credit. (There is no partial extra credit

Explanation / Answer

1) University.h

#ifndef UNIVERSITY_H
#define UNIVERSITY_H
#include <string>
#include <iostream>
#include "Person.h"
#include "Building.h"
#include <vector>

using namespace std;

class University
{
    public:
        University(string name);
        virtual ~University();
        string getName();
        vector<Person> getPeople();
        vector<Building> getBuildings();

        void setName(string name);

        void printPeople();
        void printBuildings();

        void addBuilding(Building building);
        void addPeople(Person person);

    protected:

    private:
        string name;
        vector<Person> people;
        vector<Building> buildings;
};

#endif // UNIVERSITY_H

2) Building.h

#ifndef BUILDING_H
#define BUILDING_H
#include <string>
#include <iostream>

using namespace std;

class Building
{
    public:
        Building(string name,int size,string address);
        virtual ~Building();
        string getName();
        string getAddress();
        int getSize();

    protected:

    public:
        string name;
        int size;
        string address;
};

#endif // BUILDING_H

3) Person.h

#ifndef PERSON_H
#define PERSON_H

#include <string>
#include <random>
#include <time.h>

using namespace std;

class Person
{
    public:
        Person(string Name,int Age);
        virtual ~Person();

        string getName();
        int getAge();
        int genRandom();

        void setName(string Name);
        void setAge(int Age);

        void do_work();
        virtual string getType()=0;

    protected:

    private:
        string name;
        int age;
};

#endif // PERSON_H

4) Student.h

#ifndef STUDENT_H
#define STUDENT_H
#include "Person.h"

class Student: public Person
{
    public:
        Student(string name,int age,double Gpa);
        virtual ~Student();
        void setGpa(doub Gpa);
        double getGpa();

        string getType(){return "STUDENT"};

    protected:

    private:
        double gpa;
};

#endif // STUDENT_H

5) Instructor.h

#ifndef INSTRUCTOR_H
#define INSTRUCTOR_H


class Instructor
{
    public:
        Instructor(string name, int age, double Rating);
        virtual ~Instructor();
        void SetRating(double Rating);
        double getRating();

        string getType(){return "INSTRUCTOR"};

    protected:

    private:
        double rating;
};

#endif // INSTRUCTOR_H

6) University.cpp

#include "University.h"


University::University(string name)
{
    setName(name);
}

University::~University()
{

}

void University::printPeople()
{
cout<<"printing people in campus"<endl;
cout<<"Name Age Type workHrs Rat/CGP"<endl;
vector<Person> people=getPeople();
for(int i=0; i<getPeople().size(); ++i)
    cout<<people[i].getName()<<" "<<people[i].getAge()<<" "<<people[i].getType()<<" "<<people[i].do_word()<<" ";
    if(people[i].getType()=="STUDENT")
        cout<<people[i].getGPA()<<endl;
    else
         cout<<people[i].getRating()<<endl;
}
}

void University::printBuildings()
{
cout<<"Name size Address"<endl;
vector<Building> buildings =getBuildings();
for(int i=0; i<buildings.size(); ++i)
cout<<buildings[i].getName()<<" "<<buildings.getSize()<<" "<<buildings.getAddress();
}


string University::getName()
{
    return name;
}

vector<Person> University::getPeople()
{
    return people;

}

vector<Building> University::getBuildings()
{
    return buildings;
}


void University::setName(string Name)
{
    name=Name;
}


void University::addBuilding(Building building)
{
    getBuildings().push_back(building);
}

void University::addPeople(Person person)
{
    getPeople().push_back(person)
}

7) Building.cpp

#include "Building.h"

Building::Building(string name, int size, string address)
{
    this->name=name;
    this->size=size;
    this->address=address;
}

Building::~Building()
{

}

string Building::getName()
{
return name;
}

string Building::getAddress()
{
return address;
}

int Building::getSize()
{
return size;
}

8) Person.cpp

#include "Person.h"

Person::Person(string Name, int Age)
{
    setAge(Age);
    setName(Name);
}


string Person::getName()
{
    return name;
}

int Person::getAge()
{
    return age;
}


void Person::setName(string Name)
{
    name=Name;
}

void Person::setAge(int Age)
{
    age=Age;
}
int Person::genRandom()
{

    srand(time(NULL));
    int random_number = 5 + rand() % 10;
    return random_number;
}

void Person::do_work()
{
cout<<getName()<<"did"<<getRandom()<<"hours of homework."<<endl;
}


Person::~Person()
{
    //dtor
}

9)Student.cpp

#include "Student.h"

Student::Student(string name, int age, double Gpa):Person(name,age)
{
    setGpa(Gpa);
}

void Student::setGpa(doub Gpa)
{
    gpa=Gpa;

}

double Student::getGpa()
{
    return gpa;
}

Student::~Student()
{
}

10)

#include "Instructor.h"

Instructor::Instructor(string name, int age, double Rating):Person(name,age);
{
SetRating(Rating);
}

void Instructor::SetRating(double Rating)
{
rating=Rating;
}

double Instructor::getRating()
{
return rating;
}


Instructor::~Instructor()
{
    //dtor
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote