In: Computer Science
HW 8-1a
1.) Referring to the UML class diagram, create a program that utilizes the Student class.
- id : Integer
- units : Integer
- name : String
+ Student ( ) :
+ Student (id : Int, name : String, units : Int) :
+ ~Student( ) :
+ setID(id : Integer) : void
+ setName(name: String) : void
+ setUnits(units : Integer ) : void
+ displayRecord() : void
2.) Include 3 files:
- Source.cpp
- Student.h
- Student.cpp
3.) In main(), declare two objects of Student type, named s1 and s2.
4.) In main(), the s1 object calls its displayRecord() function. Here is the output:
ID: 0
Name:
Units: 0
5.) Then in main(), the s2 object calls its displayRecord() function. Here is the output:
ID: 100
Name: Tom P. Lee
Units: 12
6.) Then in main(), use the set functions to set to assign these values to the s1 object:
ID: 101
Name: John Lee Hooker
Units: 15
7.) Finally, the s1 object calls its displayRecord() function again. Here is the output:
ID: 100
Name: John Lee Hooker
Units: 15
/* OUTPUT
Here is student #1:
ID: 0
Name:
Units: 0
Here is student #2:
ID: 100
Name: Tom P. Lee
Units: 12
Here is student #1 after the set functions:
ID: 100
Name: John Lee Hooker
Units: 15
1) Referring to the following UML class diagram, create a program that utilizes three class specifications:
Student, UnderGrad, and Grad
- Include these 7 files in your project:
- main.cpp
- Student.h
- Student. cpp
- UnderGrad.h
- UnderGrad.cpp
- Grad.h
- Grad.cpp
2.) In main(), declare two objects of UnderGrad and Grad type, respectively, as show below:
int main()
{
UnderGrad underGrad(100, 9, "Tom Lee", "Freshman");
Grad grad(101, 12, "Jim Jones", "PHD");
3.) In main(), the object, underGrad, calls its displayRecord() function. Here is the output:
ID: 100
Name: Tom Lee
Units: 9
Class: Freshman
4.) In main(), the object, grad, calls its displayRecord() function. Here is the output
of the function:
ID: 101
Name: Jim Jones
Units: 12
Degree: PHD
5.) Then, in main(), the object, grad, calls its setUnits() function, and changes the number of units
from 12 to 15.
6.) Finally, in main(),the object, grad, calls its displayRecord() function. Here is the output
of the function:
ID: 101
Name: Jim Jones
Units: 15 ß (Units has changed to 15)
Degree: PHD
Please make a code on C++ language, and please add comments for each codes.
1.
// Student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
using namespace std;
class Student
{
private:
int id;
int units;
string name;
public:
Student();
Student(int id, string name, int units);
~Student();
void setID(int id);
void setName(string name);
void setUnits(int units);
void displayRecord();
};
#endif
//end of Student.h
// Student.cpp
#include "Student.h"
#include <iostream>
// default constuctor to initialize the fields to default
values
Student::Student() : id(0), units(0), name("")
{}
// parameterized constuctor to initialize the fields to
specified values
Student::Student(int id, string name, int units) : id(id),
units(units), name(name)
{}
// destructor to destroy the Student object
Student::~Student()
{}
// function to set the id
void Student::setID(int id)
{
this->id = id;
}
// function to set the name
void Student::setName(string name)
{
this->name = name;
}
// function to set the units
void Student::setUnits(int units)
{
this->units = units;
}
// function to display student information
void Student::displayRecord()
{
cout<<"ID: "<<id<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Units: "<<units<<endl;
}
//end of Student.cpp
// main.cpp
#include "Student.h"
#include <iostream>
using namespace std;
int main()
{
Student s1;
cout<<"Here is student #1:"<<endl;
s1.displayRecord();
Student s2(100, "Tom P. Lee",12);
cout<<"Here is student #2:"<<endl;
s2.displayRecord();
cout<<"Here is student #1 after the set
functions:"<<endl;
s1.setID(101);
s1.setName("John Lee Hooker");
s1.setUnits(15);
s1.displayRecord();
return 0;
}
//end of main.cpp
Output: