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

c++ 1) Define the class called Student . The Student class has the following: a.

ID: 3893746 • Letter: C

Question

c++

1) Define the class called Student. The Student class has the following:

a. Private data members: name(string), age(int), units(int). The units represent the number of quarter units students is enrolled in.

b. Define a default constructor as well as a constructor with parameters for the class Student.

c. The class must have get and set functions for all private data members. The set function for the data member units must throw “out_of_range” exception if the number of units is not between 1 and 15.

d. Include a function called tuition (double feePerUnit) that computes and returns the cost of registering for the number of units (in the private data member).

e. Overload the insertion (<<) operator to display student name and age.

f. Test the class Student by writing a main program in which a Student object with name = Tim, age =20 and units = 5 is created and displayed. Call the function tution(), you may pass 100 as feePerUnit to this function and display the returned value. Demonstrate that the set function for the units data member throws an “out_of_range” exception.

Explanation / Answer

#include <iostream>

#include <stdexcept>

using namespace std;

class Student{

private:

string name;

int age;

int units;

public:

Student();

Student(string name, int age, int units);

string getName();

int getAge();

int getUnits();

void setName(string name);

void setAge(int age);

void setUnits(int units);

double tuition(double feePerUnit);

};

Student::Student(){

name = "";

age = 0;

units = 1;

}

Student::Student(string name, int age, int units){

this->name = name;

this->age = age;

this->units = units;

}

string Student::getName(){

return name;

}

int Student::getAge(){

return age;

}

int Student::getUnits(){

return units;

}

void Student::setName(string name){

this->name = name;

}

void Student::setAge(int age){

this->age = age;

}

void Student::setUnits(int units){

try {

if (units < 1 || units > 15) { // vector::at throws an out-of-range

throw std::out_of_range("Units value should be between 1 and 15");

}

this->units = units;

}

catch (const std::out_of_range& oor) {

std::cerr << "Out of Range error: " << oor.what() << ' ';

}

}

double Student::tuition(double feePerUnit){

return (units * feePerUnit);

}

int main(){

Student Tim("Tim", 20 , 5);

cout <<"Tuition fee ammount: " << Tim.tuition(100) <<endl ;

Tim.setUnits(20);

cout <<"Tims Unit Value: " << Tim.getUnits() << endl;

return 0;

}

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