Title: CS211 Lab7
Date: 2017-03-05T00:00:00
Tags: CS211, C++
Authors: Henry Brooks

I thought I would try a different approach to completing the assignment this week. Usually I modify the example problems and templates to fit the needs of the program.

This week I thought figured I would try giving a testing based methodology a try. Specifically I wanted to incrementally build the program up by compiling and testing as I built it from scratch.

Lab 7

The purpose of this lab is to practice working with constants, static attributes, and friend functions.

In this lab, you will design a Student class which has the following private attributes:

  • string name;
  • const long studentID;

Version 1

I start with a constructor, setter, and getter for the required class. I test to make sure that all three are being called properly in main.

main.cpp

#include <iostream>
using namespace std;

int main() {
    Student student1;

    cout << student1.getName() << endl;

    student1.setName("steve");

    cout << student1.getName() << endl;

    return 0;
}

Student.h

#ifndef STDUENT_H
#define STUDENT_H
#include <string>
using namespace std;

clast Student {
    private:
        string name;
    public:
        Student();
        string getName() const;
        void setName();
 };
#endif

Student.cpp

#include "Student.h"
#include <string>
using namespace std;

Student::Student() {
    name = "Unknown";
}

string Student::getName() const {
    return name;
}

void Student::setName(string n) {
    name = n;
}

Version 2

Since the basic skeleton of the program was running I expanded the class to include studentID, major, classification, units, and tuition. My main.cpp is also expanded to test all of the variables added to the class.

Note: I had some issues with this step. You need to remember to declare constant member values outside of the {} brackets of the constructor.

main.cpp

#include <iostream>
using namespace std;

int main() {
    Student student1;

    cout << student1.getName() << " " 
	 << student1.getID() << " "
	 << student1.getMajor() << " "
	 << student1.getClassification() << " "
	 << student1.getUnits() << endl;

    student1.setName("steve");
    student1.setMajor("MATH");
    student1.setClassification("graduate");
    student1.setUnits(8);
    student1.setTuition(1000.50);

    cout << student1.getName() << " " 
	 << student1.getID() << " "
	 << student1.getMajor() << " "
	 << student1.getClassification() << " "
	 << student1.getUnits() << endl;

    Student student2 = Student("Julia Robertson", "MATH", "undergraduate", 15);

    cout << student2.getName() << " " 
	 << student2.getID() << " "
	 << student2.getMajor() << " "
	 << student2.getClassification() << " "
	 << student2.getUnits() << endl;

    return 0;
}

Student.h

#ifndef STDUENT_H
#define STUDENT_H
#include <string>
using namespace std;

clast Student {
    private:
        string name;
        const long studentID;
        string major;
        string classification;
        int units;
        float tuition;
    public:
        Student();
        Student(string, string, string, int);
        
        string getName() const;
        long getId() const;
        string getMajor() const;
        string getClassification() const;
	int getUnits() const;
	float getTuition() const;

        void setName();
        void setMajor(string);
	void setClassification(string);
	void setUnits(int);
	void setTuition(float);
 };
#endif

Student.cpp

#include "Student.h"
#include <string>
using namespace std;

// Constructor functions
Student::Student() {
    name = "Unknown";
    major = "CS";
    classification = "undergraduate";
    units = 12;
    tuition = 0;
}
Student::Student(string theName, string theMajor, string theClassification, int theUnits) : studentID(0) {
 	name = theName;
 	major = theMajor;
 	classification = theClassification;
 	units = theUnits;
 	tuition = 0;
}

// Getter functions
string Student::getName() const {
	return name;
}
long Student::getID() const {
	return studentID;
}
string Student::getMajor() const {
	return major;
}
string Student::getClassification() const {
	return classification;
}
int Student::getUnits() const {
	return units;
}
float Student::getTuition() const {
	return tuition;
}

// Setter functions
void Student::setName(string n) {
	name = n;
}
void Student::setMajor(string m) {
	major = m;
}
void Student::setClassification(string c) {
	classification = c;
}
void Student::setUnits(int u) {
	units = u;
}
void Student::setTuition(float t) {
	tuition = t;
}

Version 3

I now add the totalNumofStudents and nextStudentID values to the class. I also add the destructor to the class sense the value it deals with has been added.

main.cpp

#include <iostream>
using namespace std;

int main() {
    Student student1;

    cout << student1.getName() << " " 
	 << student1.getID() << " "
	 << student1.getMajor() << " "
	 << student1.getClassification() << " "
	 << student1.getUnits() << " "
	 << student1.getTotalNumofStudents() << " "
	 << student1.getNextStudentID() << endl;

    student1.setName("steve");
    student1.setMajor("MATH");
    student1.setClassification("graduate");
    student1.setUnits(8);
    student1.setTuition(1000.50);

    cout << student1.getName() << " " 
	 << student1.getID() << " "
	 << student1.getMajor() << " "
	 << student1.getClassification() << " "
	 << student1.getUnits() << " "
	 << student1.getTotalNumofStudents() << " "
	 << student1.getNextStudentID() << endl;

    Student student2 = Student("Julia Robertson", "MATH", "undergraduate", 15);

    cout << student2.getName() << " " 
	 << student2.getID() << " "
	 << student2.getMajor() << " "
	 << student2.getClassification() << " "
	 << student2.getUnits() << " "
	 << student1.getTotalNumofStudents() << " "
	 << student1.getNextStudentID() << endl;

    student1.~Student();
        
    cout << student2.getName() << " " 
	 << student2.getID() << " "
	 << student2.getMajor() << " "
	 << student2.getClassification() << " "
	 << student2.getUnits() << " "
	 << student1.getTotalNumofStudents() << " "
	 << student1.getNextStudentID() << endl;

    return 0;
}

Student.h

#ifndef STDUENT_H
#define STUDENT_H
#include <string>
using namespace std;

clast Student {
    static int totalNumofStudents;
    static long nextStudentID;
    
    private:
        string name;
        const long studentID;
        string major;
        string classification;
        int units;
        float tuition;
    public:
        Student();
        Student(string, string, string, int);
        ~Student();
        
        string getName() const;
        long getId() const;
        string getMajor() const;
        string getClassification() const;
	int getUnits() const;
	float getTuition() const;
        int getTotalNumofStudents() const;
        long getNextStudentID() const;

        void setName();
        void setMajor(string);
	void setClassification(string);
	void setUnits(int);
	void setTuition(float);
};
#endif

Student.cpp

#include "Student.h"
#include <string>
using namespace std;

int Student::totalNumofStudents = 0;
long Student::nextStudentID = 10000;

// Constructor functions
Student::Student() : studentID(nextStudentID) {
    name = "Unknown";
    major = "CS";
    classification = "undergraduate";
    units = 12;
    tuition = 0;
}
Student::Student(string theName, string theMajor, string theClassification, int theUnits) : studentID(nextStudentID) {
 	name = theName;
 	major = theMajor;
 	classification = theClassification;
 	units = theUnits;
 	tuition = 0;
}
Student::~Student() {
    totalNumofStudents--;
}

// Getter functions
string Student::getName() const {
	return name;
}
long Student::getID() const {
	return studentID;
}
string Student::getMajor() const {
	return major;
}
string Student::getClassification() const {
	return classification;
}
int Student::getUnits() const {
	return units;
}
float Student::getTuition() const {
	return tuition;
}
int Student::getTotalNumofStudents() const {
    return totalNumofStudents;
}
long Student::getNextStudentID() const {
    return nextStudentID;
}

// Setter functions
void Student::setName(string n) {
	name = n;
}
void Student::setMajor(string m) {
	major = m;
}
void Student::setClassification(string c) {
	classification = c;
}
void Student::setUnits(int u) {
	units = u;
}
void Student::setTuition(float t) {
	tuition = t;
}

Version 4

Next I add the printStudent, setData, and setTuition functions to finish off the assignment. I also clean up the files to align with the specs given in the written assignment.

main.cpp

#include <iostream>
using namespace std;

void setTuition(Student &stu) {
    if (stu.classification == "graduate")
        stu.tuition = stu.units * 1000;
    else
        stu.tuition = stu.units * 700;
}

int main() {
    Student student1;
    student1.printStudent();    

    Student student2 = Student("unknown", "PHYS", "graduate", 0);
    student2.setData(); // "Julia Robertson", "MATH", "undergraduate", 15

    setTuition(student2);
    student2.printStudent();

    return 0;
}

Student.h

#ifndef STDUENT_H
#define STUDENT_H
#include <string>
using namespace std;

clast Student {
    static int totalNumofStudents;
    static long nextStudentID;

    friend void setTuition(Students&);
    
    private:
        string name;
        const long studentID;
        string major;
        string classification;
        int units;
        float tuition;
    public:
        Student();
        Student(string, string, string, int);
        ~Student();
        
        string getName() const;
        long getId() const;
        string getMajor() const;
        string getClassification() const;
	int getUnits() const;
	float getTuition() const;
        int getTotalNumofStudents() const;
        long getNextStudentID() const;

        void setName();
        void setMajor(string);
	void setClassification(string);
	void setUnits(int);
	void setTuition(float);
};
#endif

Student.cpp

#include "Student.h"
#include <string>
using namespace std;

int Student::totalNumofStudents = 0;
long Student::nextStudentID = 10000;

// Constructor functions
Student::Student() {
    name = "Unknown";
    major = "CS";
    classification = "undergraduate";
    units = 12;
    tuition = 0;
}
Student::Student(string theName, string theMajor, string theClassification, int theUnits) : studentID(0) {
 	name = theName;
 	major = theMajor;
 	classification = theClassification;
 	units = theUnits;
 	tuition = 0;
}
Student::~Student() {
    totalNumofStudents--;
}

// Getter functions
string Student::getName() const {
	return name;
}
long Student::getID() const {
	return studentID;
}
string Student::getMajor() const {
	return major;
}
string Student::getClassification() const {
	return classification;
}
int Student::getUnits() const {
	return units;
}
float Student::getTuition() const {
	return tuition;
}
int Student::getTotalNumofStudents() const {
    return totalNumofStudents;
}
long Student::getNextStudentID() const {
    return nextStudentID;
}

void Student:printStudent() const {
    cout << "Student name:   " << name << endl
	 << "Student id:     " << studentID << endl
	 << "Student major:  " << major << endl
	 << "Student class:  " << classification << endl
	 << "Student units:  " << units << endl
	 << "Tuition:        " << tuition << endl;
}

// Setter functions
void Student::setName(string n) {
	name = n;
}
void Student::setMajor(string m) {
	major = m;
}
void Student::setClassification(string c) {
	classification = c;
}
void Student::setUnits(int u) {
	units = u;
}
void Student::setTuition(float t) {
	tuition = t;
}

void Student::setData() {
    string n, m, c; 		// Name, Major, and Classification
    int u;			// Units
    cout << "Please enter the students name: ";
    getline(cin, n);
    name = n;
    cout << "Please enter the students major: ";
    cin >> m;
    major = m;
    cout << "Please enter the students classification: ";
    cin >> c;
    classification = c;
    cout << "Please enter the students units: ";
    cin >> u;
    units = u;	
}