Use a class member function to assign a value to a variable

Hello everyone, this is my first post here and I've tried searching everywhere for it, but perhaps I'm wording my search incorrectly.

The problem I'm having is using a class function to assign a value to a variable. I have two classes - undergrad (base class) and grad (inherits undergrad).

Here are the two classes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#ifndef UNDERGRAD_H_INCLUDED
#define UNDERGRAD_H_INCLUDED
#include <iostream>
#include <string>
#include <vector>
using namespace std;

//Object class that holds an undergraduate student's name, tuition per course, and number of classes.
class undergrad
{
public:
	undergrad();
	undergrad(string studName, double studTuition, int  numClasses);
	void setName(string newName);
	void setTuition(double newTuition);
	void setClasses(int newClasses);
	string getName() const;
	double getTuition() const;
	int getClasses() const;
	double totalTuition() const;
private:
	string name;
	double tuition;
	int classes;
};

inline undergrad::undergrad() { name = "None"; tuition = 0.0; classes = 0; }
inline undergrad::undergrad(string studName, double studTuition, int numClasses) { name = studName, tuition = studTuition, classes = numClasses; }
inline void undergrad::setName(string newName) { name = newName; }
inline void undergrad::setTuition(double newTuition) { tuition = newTuition; }
inline void undergrad::setClasses(int newClasses) { classes = newClasses; }
inline string undergrad::getName() const { return name; }
inline double undergrad::getTuition() const { return tuition; }
inline int undergrad::getClasses() const { return classes; }
inline double undergrad::totalTuition() const { double total = tuition * classes; return total; }

#endif 


and the other:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#ifndef GRAD_H_INCLUDED
#define GRAD_H_INCLUDED
#include <iostream>
#include <string>
#include <vector>
#include "undergrad.h"
using namespace std;

//Object class that inherits undergrad but allows the addition of fees and recalculates tuition.
class grad : public undergrad
{
public:
	grad();
	grad(double studFees);
	void setFees(double newFees);
	double getFees() const;
	double totalTuition();
private:
	double fees;
};

inline grad::grad() { fees = 0.0; }
inline grad::grad(double studFees) { fees = studFees; }
inline void grad::setFees(double newFees) { fees = newFees; }
inline double grad::getFees() const { return fees; }
inline double grad::totalTuition()
{
	undergrad x; grad y;
	double newTuition;
	newTuition = x.totalTuition() + y.getFees();
	return newTuition;
}
#endif 


As you can see, I use 2 class functions to try to assign a value to newTuition and the result is 0. The requirement for the project is to use the base class function call instead of using the private members directly.

If anyone can provide any guidance, I'd greatly appreciate it. Thank you.
Last edited on
I already solved my own issue. I simply changed grad::totalTuition() to:

1
2
3
4
inline double grad::totalTuition()
{	
	return undergrad::totalTuition() + getFees();
}
Topic archived. No new replies allowed.