Ok, so im doing an assignment and Here is the question:

Using the base class Person which has two member variables called name and birthday [both of type string], derive two separate classes from Person, called Student and Professor. The Student class has a major [string] and a gpa [double] and the Professor class has research [string] and a salary [double].
Write two constructors for each of the three classes; in each case one will be the no-argument constructor, and the other constructor will initialize all member variables for that particular class.
Page 3 of 4
Create/write the void print() function for each of the three classes; this will print out the all the available information about each object type.

Write the class definitions in the following way: put the three class interfaces [just the declaratioins] in the file human.h and put the three class implementations in the file human.cpp
I started on the code and here is what i have so far.
class Person //base class Person
{
public:
void print() const
{
    cout <<"This is the Person Class.n";
}
private:
    string name;
    string birthday;
};
class Student : public Person //class Student derived from Person
{
public:
    string major;
    double gpa;
void print() const
    {
        cout <<"Hello my Major is Engineering.n";
    }
private:
};
class Professor : public Person //class professor derived from Person
{
public:

    string research;
    double salary;
void print() const
    {
        cout <<"Hello my Research is in the field of Fluids.n";
    }
private:
};

--------------------------------------------------------------------------------------------------
Im having problems figuring out certain things.
1 - Am i deriving the member variables correctly? If not how should it be done?
2 - Im not sure how to do this.
"write two constructors for each of the three classes; in each case one will be the no-argument constructor, and the other constructor will initialize all member variables for that particular class."
3 - Where do the "Create/write the void print() function for each of the three classes" go? I know how to use them just don't know where they belong.

Any help is appreciative. Thank you. Ive been reading alot on classes and Inheritance but i just don't seem to understand it :(


Im having problems figuring out certain things:
1 - Am i deriving the member variables correctly? If not how should it be done?

Yup.

2 - Im not sure how to do this.
"write two constructors for each of the three classes; in each case one will be the no-argument constructor, and the other constructor will initialize all member variables for that particular class."

I guess he means something like this:

class foo
{
private:
    int bar;
public:
    foo(){} // the no-arg constructor
    foo(int in)// second constructor that inits the vars
    {
        bar = in;
    }
};

3 - Where do the "Create/write the void print() function for each of the three classes" go? I know how to use them just don't know where they belong.

To use my previous example:

class foo
{
private:
    int bar;
public:
    foo(){} // the no-arg constructor
    foo(int in)// second constructor init vars
    {
        bar = in;
    }
    void print()
    {
        std::cout << bar;
    }
};

I've put all the code for the functions in the class-declaration , which is fine when a function is one or two lines of code, but for bigger functions this isn't considered good coding-practice ;) (just a word of advice...)

Thank's alot, Ill give it a try and get back to you.

Alright, so this mostly works but i have a last question.

I would like to do this. I wanna set the variable to values like (const double gpa = 3.7) and (const string major = engineering)

How would i do this and how would i get it to print out using the void print function?

well, you'd want to change your print function

void print()
{
cout <<"hello, my major is " << major;
cout << ", and my GPA is " << gpa << endl;
}

and, to set it, whenever you make your object you can do something like:

Student x;
x.major = "whatever";
x.gpa = 1.337;

thanks alot. I will try this code

All right i got it working now. Thanks a lot for you help guys it really helped me understand a part of classes i didn't get before :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.