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 :(

Dani AI

Generated

Quick, practical clarifications that fill gaps in the thread:

  • Inheritance and access: the derived classes do inherit the base data members, but private members of Person are not directly accessible from Student/Professor. Either provide a Person constructor that takes name/birthday and call it from derived-class constructors, or provide protected members or accessors (getters). Also give Person a virtual destructor if polymorphic deletion is possible.

  • Where to put things: put class declarations (constructors, print prototypes) in human.h and full function bodies in human.cpp as the assignment requests. Small one-line functions can be inline, but the clean separation helps compilation and grading.

  • Constructors and defaults: write a no-arg constructor and a parameter constructor for each class. Use member initializer lists and call the Person constructor from Student/Professor to initialize name and birthday. For default values (like major = "Engineering" and gpa = 3.7) either give default parameters on the constructor or initialize those members in the no-arg constructor. In modern C++ you can also use in-class defaults.

  • print() design: make Person::print() virtual and const. Derived print() should call Person::print() (or use accessors) then output the derived fields so each object prints all available info. Mark overrides with override in C++11+.

Example layout (declarations in human.h, implementations in human.cpp) demonstrates the pattern: constructor overloads, initializer lists calling the base constructor, virtual print, default arguments for Student. This avoids making data public and keeps initialization robust.

Notes tied to earlier replies: was right that small functions can be in-class, but here the assignment expects header/implementation separation and initializer lists are preferable. showed setting members after construction — that works but constructors (or setters) are better practice for required state. , using the patterns above will make the constructors initialize all members properly and let print() show both base and derived information.

Recommended Answers

All 6 Replies


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.