Re: C++...and I'm stumped!
From: Vince Morgan (vinhar_at_NOSPAMoptusnet.com.au)
Date: 02/26/05
- Next message: Jay Nabonne: "Re: Static Objects as Struct Members"
- Previous message: Alf P. Steinbach: "Re: C++ class pointer (??) problem."
- Maybe in reply to: Vince Morgan: "Re: C++...and I'm stumped!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 26 Feb 2005 16:51:27 +1000
"Jedispy" <jedispy@no.spam.please.yahoo.com> wrote in message
news:422000b6_3@newsfeed.slurp.net...
> That helped out a lot by the way. The program compiles and works (for the
> most part) but it's not printing the right message. I'm having trouble
with
> the person_data.get_name() part of the PEmployee::get_name() member
> function.
>
Hi Jedispy,
In order to achieve what I think you are pursuing
requires a little
more restructuring of your code.
I am wondering if this is homework. If it is, and I answer all this for
you, will
you be able to explain the code to your tutor? If this was an exercise
given to
you he\she may very well wonder at your new found knowledge.
Having said that I will leave that to you.
You should treat Person as a base class.
If you look at it this way you may see my point.
Everybody, whether they are an; 'Employer', 'Employee', 'Manager',
'Housewife',
'Student', 'Football Coach', etc etc are Persons.
If you create a base class that encapsulates the properties of people, ie
Person's,
you would include only those properties that _all_ persons _must_ have,
though
you are obviously free to only include the subset of those properties you
require for
your particular solution.
All persons have at least; Height; Weight; Age, or at least they do on this
planet.
There are many others of course, but they are not required in your solution.
Your solution only requires a name and an age. These are properties common
to
_all_ persons regardless of any other considerations.
I would define your 'Person' class as follows.
class Person
{
public:
Person();
Person(string pname, int age);
string get_name() const;
int get_age() const;
protected: <<<<< search for definition of 'protected'
string name;
int age; /* 0 if unknown */
};
By changing private: to protected: we are allowing access to 'string', and
'age'
from a derived class.
Derived classes 'inherit' the methods\functions and variables of the base
class.
Therefore, there is no need for get_age() in the classes derived from
Person,
as it will be 'inherited' from Person along with the data in Person.
class PEmployee: public Person
{
public:
PEmployee();
PEmployee(string employee_name, double initial_salary);
void set_salary(double new_salary);
double get_salary() const;
<<<<<< No need for get_name here as it's inherited from
Person
private:
Person person_data; <<< I have no idea what your intentions are here. ???
double salary;
};
I hope this helps Jedispy,
Vince Morgan
- Next message: Jay Nabonne: "Re: Static Objects as Struct Members"
- Previous message: Alf P. Steinbach: "Re: C++ class pointer (??) problem."
- Maybe in reply to: Vince Morgan: "Re: C++...and I'm stumped!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|