Re: C++...and I'm stumped!

From: Vince Morgan (vinhar_at_NOSPAMoptusnet.com.au)
Date: 02/26/05


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



Relevant Pages

  • Re: C++...and Im stumped!
    ... You should treat Person as a base class. ... Your solution only requires a name and an age. ... string get_nameconst; ...
    (alt.comp.lang.learn.c-cpp)
  • Re: CodeDOM, const strings and abstract base classes
    ... went with a readonly string instead of a const. ... public abstract class Instrument { ... The methods in the base class will parse the contents of the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: IF statement
    ... StrSize as String ... That would work if the Age was a string field and was never null. ... AgeCate = "Don't Know" ...
    (microsoft.public.access.queries)
  • Re: n-Tier/Layer with NULL and persistence
    ... Larry if what you need to check the NULL values in he DB use ... Public MustOverride Property Persist(ByVal key As String) As Object ... the database to the screen and back? ... Public Property Age() As Integer ...
    (microsoft.public.dotnet.framework.aspnet)
  • RE: VB formula
    ... Set up your table sheet initially in the following fashion. ... Const minAge = 10 ... Dim lastRow As Long ...
    (microsoft.public.excel.misc)

Loading