 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Vince Morgan Guest
|
Posted: Wed Feb 23, 2005 9:54 am Post subject: Re: C++...and I'm stumped! |
|
|
"Jedispy" <jedispy (AT) no (DOT) spam.please.yahoo.com> wrote
| Quote: | Hey everyone, I need a little help here. I'm new to OOP and I'm having a
little trouble with object classes.
I'm trying to create classes for "Person" and "Employee." The "Employee"
class is supposed to store an object of type "Person".
Here is what I've got so far for my code. Any help is appreciated.
Serious
posts only please.
_____________________________
|
I think you mean that Employee should 'inherit' from Person,
as an Employee IS a person. Your code seems to demand
it actualy. You already have a name for this PersonEmployee
in Person you see.
See below.
class Person
{
public:
Person();
Person(string pname, int page);
string get_name() const;
int get_age() const;
private:
string name;
int age; /* 0 if unknown */
};
class PEmployee: public Person{
public:
PEmployee();
PEmployee(string employee_name, double initial_salary);
void set_salary(double new_salary);
double get_salary() const;
// string get_name() const; << comment this one out
private:
Person person_data;
double salary;
};
I haven't spent a lot of time on this and I'm no expert but
I'm sure this will point you in the right direction.
Regards,
Vince Morgan
|
|
| Back to top |
|
 |
Jedispy Guest
|
Posted: Fri Feb 25, 2005 4:24 am Post subject: Re: C++...and I'm stumped! |
|
|
"class PEmployee: public Person{"
Hrm.....interesting. I will try that.
Thanks
"Vince Morgan" <vinhar (AT) NOSPAMoptusnet (DOT) com.au> wrote
| Quote: |
"Jedispy" <jedispy (AT) no (DOT) spam.please.yahoo.com> wrote in message
news:421c2733_4 (AT) newsfeed (DOT) slurp.net...
Hey everyone, I need a little help here. I'm new to OOP and I'm having
a
little trouble with object classes.
I'm trying to create classes for "Person" and "Employee." The
"Employee"
class is supposed to store an object of type "Person".
Here is what I've got so far for my code. Any help is appreciated.
Serious
posts only please.
_____________________________
I think you mean that Employee should 'inherit' from Person,
as an Employee IS a person. Your code seems to demand
it actualy. You already have a name for this PersonEmployee
in Person you see.
See below.
class Person
{
public:
Person();
Person(string pname, int page);
string get_name() const;
int get_age() const;
private:
string name;
int age; /* 0 if unknown */
};
class PEmployee: public Person{
public:
PEmployee();
PEmployee(string employee_name, double initial_salary);
void set_salary(double new_salary);
double get_salary() const;
// string get_name() const; << comment this one out
private:
Person person_data;
double salary;
};
I haven't spent a lot of time on this and I'm no expert but
I'm sure this will point you in the right direction.
Regards,
Vince Morgan
|
|
|
| Back to top |
|
 |
Vince Morgan Guest
|
Posted: Sat Feb 26, 2005 6:51 am Post subject: Re: C++...and I'm stumped! |
|
|
"Jedispy" <jedispy (AT) no (DOT) spam.please.yahoo.com> wrote
| Quote: | 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 heshe 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 methodsfunctions 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
|
|
| Back to top |
|
 |
Jedispy Guest
|
Posted: Mon Feb 28, 2005 10:57 pm Post subject: Re: C++...and I'm stumped! |
|
|
I appreciate all of the advice, as well as your concern for integrity. This
is part of a homework project. I assure you, you are in no way giving the
answer away. I am doing lots of
research in online tutorials, other OOP & C++ books, and in newsgroups like
this. My professor encourages such research. I understand the concept of
OOP and I have a fair grasp of C++. My biggest problem is my inexperience.
That's why I value assistance from veterans like yourself. You can be
confident that I'm not just asking people to do my homework for me. I know
that such services are available on the web, and I have elected to not use
them. I want to improve as a programmer, not just get an A in my class.
I've heard of object inheritance, but due to inexperience I haven't gotten
that far into my code. I agree with you though that since employees are
people, that class PEmployee ought to inherit from class Person. It did
seem odd to me for both classes to have get_name functions.
I was able to finish that project (at least for the time being.)
Anyway, I am working on another project. It deals with vectors. I'm just
trying to get the whole concept down. I've used arrays in other languages
(like VB 6.0) with relative ease. It's not working for me in C++ though.
I'm trying to declare a vector with a simple user input. There is a loop
"i" that counts up to the maximum "max". I can make this work easily in VB,
but in C++ it's a trip!
/* BEGIN CODE */
int main()
{
int i;
vector<int> a;
int max = a[10];
do
{
for (i = 1; i < 10; i++)//LINE 36
cout << "Enter a number.n";
cin >> a[i];
/*if (a[i] > max)
break;*/
}
while (a[i] <= max);
return(0);
}
/* END CODE */
This is not the program in its entirety. I am however stuck at this point.
Thanks.
|
|
| Back to top |
|
 |
Robert W Hand Guest
|
Posted: Tue Mar 01, 2005 12:31 am Post subject: Re: C++...and I'm stumped! |
|
|
On Mon, 28 Feb 2005 14:57:50 -0800, "Jedispy"
<jedispy (AT) no (DOT) spam.please.yahoo.com> wrote:
| Quote: | I'm trying to declare a vector with a simple user input. There is a loop
"i" that counts up to the maximum "max". I can make this work easily in VB,
but in C++ it's a trip!
|
It's not clear what you are trying to do. You seem to have the do and
for loops overlapping. One needs to nest within the other.
You need some headers.
| Quote: | int main()
{
int i;
vector<int> a;
|
std::vector<int> a(11, 100); // perhaps
This looks like UB to me. How big is the vector, and how was it
initialized?
| Quote: |
do
{
for (i = 1; i < 10; i++)//LINE 36
{ // I believe that you wanted a block.
cout << "Enter a number.n";
cin >> a[i];
|
Do you know how large your vector is? It may not have 11 slots.
}
| Quote: |
/*if (a[i] > max)
break;*/
}
while (a[i] <= max);
|
Hard to know what you are trying to do with i now.
| Quote: | return(0);
}
/* END CODE */
This is not the program in its entirety. I am however stuck at this point.
|
The above is not completely a language issue. The two loops are
intertwined and should be separated. I doubt that VB has intertwined
loops like that.
--
Best wishes,
Bob
|
|
| Back to top |
|
 |
Jedispy Guest
|
Posted: Tue Mar 01, 2005 1:05 am Post subject: Re: C++...and I'm stumped! |
|
|
Yeah what I submitted earlier is just a snippet. That's why there were no
headers or namespace. I've modified the code since my earlier posting.
I've made progress since then, but now I'm stuck on function
remove_duplicates() (see code below). The purpose of this program is to
remove duplicate entries in a vector consisting of numbers. (this is not the
whole assignment, but it's the part I'm stuck on).
Below is my code (including headers so as not to confuse):
/* BEGIN CODE */
#include <iostream>
#include <string>
#include <vector>
using namespace std;
/* Removes duplicate elements from vector a */
void remove_duplicates()
{
int x = 0;
int y = 0;
while (cin >> a[x])
{
if a[i] = a[x]
a[x] = 0;
};
}
int main()
{
int i;
int n = 1;
vector<int> a(10);
int max = 10;
// predefined entries for vector a
a[0] = 1;
a[1] = 4;
a[2] = 9;
a[3] = 16;
a[4] = 9;
a[5] = 7;
a[6] = 4;
a[7] = 9;
a[8] = 11;
// Display elements of vector a for clarification
for (i = 0; i < 10; i++)
{
cout << a[i] << "n";
};
remove_duplicates(); // See function remove_duplicates()
return(0);
}
/* END CODE */
|
|
| Back to top |
|
 |
Vince Morgan Guest
|
Posted: Tue Mar 01, 2005 2:02 am Post subject: Re: C++...and I'm stumped! |
|
|
"Jedispy" <jedispy (AT) no (DOT) spam.please.yahoo.com> wrote
| Quote: | I appreciate all of the advice, as well as your concern for integrity.
This
is part of a homework project. I assure you, you are in no way giving the
answer away. I am doing lots of
research in online tutorials, other OOP & C++ books, and in newsgroups
like
this. My professor encourages such research. I understand the concept of
OOP and I have a fair grasp of C++. My biggest problem is my
inexperience.
That's why I value assistance from veterans like yourself. You can be
confident that I'm not just asking people to do my homework for me. I
know
that such services are available on the web, and I have elected to not use
them. I want to improve as a programmer, not just get an A in my class.
|
Hi Jedispy,
I'm pleased to see the above. Good attitude
Only one thing, I'm no verteran of cc++. I'm very new to
this language too. I've been writing VB6 apps for the last
6 years or so.
The difference between VB and C arrays[] is primarily
that much of the work is done behind a curtain in VB.
In VB, say you declare 'dim a(9) as integer'
You know this is an array of 10 elements by default.
There is a little confusion when you see the same thing
in C, as the declaration is the number of elements, counting
from 1, not 0.
So, in C, a[10] is an array of 10 elements.
However, the first element is actualy 0. So a loop count
over all elements would be 0 to 9. Same as default VB,
except the declaration is the number of elements.
This is an array of 10 elements, as you already knew
| Quote: |
do
{
for (i = 1; i < 10; i++)//LINE 36
|
Here you have begun the count at 1, instead of 0.
The actual array index begins at 0 in C.
This is a little confusing when you are used to the count
and the index being equivelant, but once you know this
things get much easier.
HTH,
Vince Morgan
|
|
| Back to top |
|
 |
Robert W Hand Guest
|
Posted: Tue Mar 01, 2005 2:04 am Post subject: Re: C++...and I'm stumped! |
|
|
On Mon, 28 Feb 2005 17:05:44 -0800, "Jedispy"
<jedispy (AT) no (DOT) spam.please.yahoo.com> wrote:
| Quote: | /* BEGIN CODE */
#include <iostream
#include
#include
using namespace std;
/* Removes duplicate elements from vector a */
void remove_duplicates()
{
int x = 0;
int y = 0;
while (cin >> a[x])
{
if a[i] = a[x]
a[x] = 0;
};
|
A bookkeeping moment: You do not need the semicolon after block
statements such as a function body, for statements and the like.
This function is very confused in design - sorry. It lacks generality
since it does not take or return values. The a[x] is not defined.
There is confusion of data input and an algorithm. The 'if condition'
is not correct syntax, and I'm not sure how the compiler would treat
it. I would have expected an error message.
So I would suggest that you take a piece of paper and write down what
goes into the function and what comes out. Be sure to fix those
requirements before you start to code. The function name should be
self-documenting as best you can do.
There are further complications when you ask yourself what it really
means to have duplicates removed. Are you going to have a smaller
container? Do you want a new container rather than changing the
original in situ? Is the order of the elements important or can they
be sorted?
For example, you could use standard containers such as set. Then such
a function could be written:
std::vector<int> remove_duplicates(const std::vector<int> & a)
{
std::set<int> s(a.begin(), a.end());
return std::vector<int>(s.begin(), s.end());
}
It could be invoked in main as
std::vector<int> b = remove_duplicates(a);
But my point is more design than implementation. My function takes a
vector as a parameter and returns another vector that has the
duplicates removed. If you want to maintain the same order in the
vector, then a more complex algorithm would be needed for the body of
the function. But the design point is the same.
--
Best wishes,
Bob
|
|
| Back to top |
|
 |
Vince Morgan Guest
|
Posted: Tue Mar 01, 2005 2:15 am Post subject: Re: C++...and I'm stumped! |
|
|
"Jedispy" <jedispy (AT) no (DOT) spam.please.yahoo.com> wrote
| Quote: | int i;
int n = 1;
vector<int> a(10); *****
|
Your VB is showing
This is an easy mistake to make.
a(10) should b, a[10];
I haven't looked too closely at the rest of it yet,
but you seem to be going well Jedispy.
Vince Morgan
|
|
| Back to top |
|
 |
Vince Morgan Guest
|
Posted: Tue Mar 01, 2005 2:31 am Post subject: Re: C++...and I'm stumped! |
|
|
"Jedispy" <jedispy (AT) no (DOT) spam.please.yahoo.com> wrote
| Quote: | a[0] = 1;
a[1] = 4;
a[2] = 9;
a[3] = 16;
a[4] = 9;
a[5] = 7;
a[6] = 4;
a[7] = 9;
a[8] = 11;
|
This can be done as below,
A[10] = {1, 4, 9, 16, 9, 7, 4, 9, 11};
However, I noticed you haven't initialized the last element of
your array.
One thing to be conscious of in C is that an array has to be
initialized by the programmer, unlike VB which automaticaly
sets the initial val to zero for generic data types.
In the above you haven't initialized the last element.
I realize this may not be an actual implementation, but if it is
that should be taken care of.
// Display elements of vector a for clarification
for (i = 0; i < 10; i++)
{
cout << a[i] << "n";
};
The above is perfectly correct.
Vince Morgan
|
|
| Back to top |
|
 |
Edd Guest
|
Posted: Tue Mar 01, 2005 3:07 am Post subject: Re: C++...and I'm stumped! |
|
|
Vince Morgan wrote:
| Quote: | "Jedispy" <jedispy (AT) no (DOT) spam.please.yahoo.com> wrote in message
news:4223c05e_4 (AT) newsfeed (DOT) slurp.net...
int i;
int n = 1;
vector<int> a(10); *****
Your VB is showing
This is an easy mistake to make.
a(10) should b, a[10];
|
It's not a mistake.
std::vector<T> a(10);
declares a vector of 10 elements of type T, each assembled by the
default constructor for that type.
See what your compiler says when you give it this program:
#include <vector>
int main() { std::vector<int> a[10]; }
If it's anything like mine, it won't be pretty :)
Edd
--
Edd Dawson www.nunswithguns.net | "Arthur! My mustache is
To email me, cut the crap. | touching my brain!"
[email]edd (AT) nunswithcrapguns (DOT) net[/email] | - The Tick
|
|
| Back to top |
|
 |
Vince Morgan Guest
|
Posted: Tue Mar 01, 2005 3:07 am Post subject: Re: C++...and I'm stumped! |
|
|
"Robert W Hand" <rwhand (AT) NOSPAMoperamail (DOT) com> wrote
| Quote: | if a[i] = a[x]
a[x] = 0;
This function is very confused in design - sorry. It lacks generality
since it does not take or return values. The a[x] is not defined.
There is confusion of data input and an algorithm. The 'if condition'
is not correct syntax, and I'm not sure how the compiler would treat
it. I would have expected an error message.
Bob
|
In VB '=' is both an assignment and comparison opperator,
not so in C. In C '=' is assignment, '==' is comparison.
if a[i] = a[x];
This is going to assign a[i] the value of a[x] and then test a[i] which
as Bob pointed out is NOT likely to be what you want.
Vince Morgan
|
|
| Back to top |
|
 |
Edd Guest
|
Posted: Tue Mar 01, 2005 3:13 am Post subject: Re: C++...and I'm stumped! |
|
|
Edd wrote:
| Quote: | Vince Morgan wrote:
"Jedispy" <jedispy (AT) no (DOT) spam.please.yahoo.com> wrote in message
news:4223c05e_4 (AT) newsfeed (DOT) slurp.net...
int i;
int n = 1;
vector<int> a(10); *****
Your VB is showing
This is an easy mistake to make.
a(10) should b, a[10];
It's not a mistake.
std::vector<T> a(10);
declares a vector of 10 elements of type T, each assembled by the
default constructor for that type.
See what your compiler says when you give it this program:
#include <vector
int main() { std::vector
If it's anything like mine, it won't be pretty
|
I just love publicly humiliating myself.
In fact my compiler eats it up just fine and so should yours. However,
it declares an array of 10 std::vector<int>s which I don't believe is
the desired effect. My compiler was actually babbling the rest of the
program that I foolishly left behind.
Edd
--
Edd Dawson www.nunswithguns.net | "Arthur! My mustache is
To email me, cut the crap. | touching my brain!"
[email]edd (AT) nunswithcrapguns (DOT) net[/email] | - The Tick
|
|
| Back to top |
|
 |
Ron House Guest
|
Posted: Tue Mar 01, 2005 3:34 am Post subject: Re: C++...and I'm stumped! |
|
|
Jedispy wrote:
| Quote: | /* BEGIN CODE */
int main()
{
int i;
vector<int> a;
|
A is a vector, but it contains no elements.
a[10] doesn't exist - possible segfault, certain error.
You should:
a.resize(11);
to create elements 0 through 10.
--
Ron House [email]house (AT) usq (DOT) edu.au[/email]
http://www.sci.usq.edu.au/staff/house
|
|
| Back to top |
|
 |
Jedispy Guest
|
Posted: Tue Mar 01, 2005 5:19 am Post subject: Re: C++...and I'm stumped! |
|
|
<replying generally to all>
WOW!! Thanks for all the good input. Seriously, don't feel like you're
insulting me if you say my code looks odd or confusing, or if my "VB is
showing." LOL. Hey guys...that's why I'm here. I want to learn.
About that VB showing, yeah I'm still suffering that. I remember in VB you
an declare an array of 100 elements two ways. One is as name(100) or name(0
to 99). However that's neither here nor there.
There are so many times I get an ambiguous compiler error. My response is
usually "WTF?!" I then pull my head out, and realize the ambiguous errors
are my compiler's way of saying "WTF?!" Learning to program is a good way
to learn humility. No matter what the issue, it's my fault, not the
compiler's.
I'll try to take all your suggestions to mind.
Michael
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|