I am currently learning C++ and working on a assignment which asks me to create:
Class student (must be separated by .h and .cpp) that inherits class person that has:
a. Attributes: dynamic array of struct holding courses and their grades and other attributes
b. Member functions: display the grade of a given course and other member functions as as needed
So, to start, I have already created the class person; so don't worry about the inheritance nor classes, my only problem is with structs, here is my code:
student.h
#include <string>
#ifndef student_h
#define student_h
#include "person.h"
class student: public person {
private:
struct stud {
int age;
};
public:
student();
int getAge();
};
#endif
student.cpp
#include <iostream>
#include <string>
using namespace std;
#include "student.h"
student::student() {
}
int student::getAge() {
return stud.age;
}
So, my logic is, in the same way, that if you define a private integer in the .h file and use it freely in the .cpp file, I should do it for the struct. When I try to compile student.cpp for synthax error before running main.cpp, I get this error:
.\student.cpp(14) : error C2275: 'student::stud' : illegal use of this type as an expression
which is referring to return stud.age;. I am using (and forced to use) Visual Studio 2005.
How do I retrieve the age of the struct using a function? Also, what does my teacher mean by an array of structs? Does it mean I have to create the array in the main and how?
getis just annoying verbiage in C++. You would not writegetCos(x)for the cosine function, would you? Likewise, justageis fine to access or compute the age. Oh, and better store the birth year, not the age. Or else the data will get outdated fast.