-
January 21st, 2004, 02:22 PM
#1
an array of objects?
This is a school project I'm having trouble with... so don't give me the direct answer, but simply some help to understand what is asked from this following question:
A program to calculate the area of shapes.
I have 3 classes. 2 are derived classes of the first one. The base class is named Shape, the derived classes are Circle and Square. (they contain member functions such as setRadius, getRadius, setSide, getSide, and a overriding base class function named getArea that calculates the area for the corresponding shape)
I was able to go at about 90% of the project when it suddenly asked this:
"Create a driver class called Project2. The driver class should contain an array type Shape. The first two elements of the array will contain Circle objects with a radius of 1 and 3. The third array element should contain a Square object with a side of 2." ... to finally display the area of the three shapes on screen.
How am I suppose to declare an array of type Shape and then be able to use member functions of the derived classes afterwards for the corresponding shape (ie square or circle).
Maybe all I need is a good english teacher!
any help? thanks in advance
-
January 21st, 2004, 02:28 PM
#2
I think you answered one of your own questions.
question:
How am I suppose to declare an array of type Shape and then be able to use member functions of the derived classes afterwards for the corresponding shape (ie square or circle).
answer:
and a overriding base class function named getArea that calculates the area for the corresponding shape)
This overriding base class function is called polymorphism so the getArea function is implemented differently in your derived classes.
One part are you having trouble with? Do you know how to create arrays of objects? Do you know how to create an overriding base class function?
Last edited by bluesource; January 21st, 2004 at 04:11 PM.
-
January 21st, 2004, 04:27 PM
#3
Re: an array of objects?
Originally posted by h0t1ce
This is a school project I'm having trouble with... so don't give me the direct answer, but simply some help to understand what is asked from this following question:
I really appreciate the above statement...since you are more or less the first one who started a homework question like this...
Originally posted by h0t1ce
How am I suppose to declare an array of type Shape and then be able to use member functions of the derived classes afterwards for the corresponding shape (ie square or circle).
As bluesource indicated...you are already on the right track...the key is 'polymophism'...for further information you might want to take a look at the following FAQs:
-
January 21st, 2004, 04:52 PM
#4
One thing that it's necessary to point out about those two FAQs: I left one very important piece of information implicit in there (since probably 99% of people know it already). Without wanting to give too much away, consider the type of the argument to the function in this example from the first of those FAQs:
Code:
void migrate(bird* tweetiepie)
{
// ...
tweetiepie->fly();
// ...
}
The part of the question that you posted that says "The driver class should contain an array [of] type Shape" cannot work exactly as stated. You can't solve the problem with an array of Shape, you need an array of.... (see hint above).
Sorry to be obtuse, but free answers don't help the learning process (as I'm sure you're aware). Have a think about it, and come back if you're still stuck.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
January 21st, 2004, 05:06 PM
#5
Hey thank you guys! Thanks for the very useful pointers!
I definitely see that I was lacking knowledge on polymorphism! I noticed I hadn't captured the whole meaning of polymorphism and virtual functions after fast read of the links provided by Andreas... Even though I still don't fully comprehend yey, I felt I needed to reply as soon as possible just to thank all of you.
I should state that learning a programming language with helpful people around to help makes it a much more interesting experience for me.
I'll keep you aware if I get by with this project!
-
January 21st, 2004, 06:39 PM
#6
I got the program working like it should with polymorphism. All I'd like to know know is if you guys think I have found the solution the question was asking for....
Code:
void main() {
Circle firstShape(1);
Circle secondShape(3);
Square thirdShape(2);
Shape* pShape[3];
pShape[0] = &firstShape;
pShape[1] = &secondShape;
pShape[2] = &thirdShape;
cout << "first shape's area is " << pShape[0]->getArea() << endl;
cout << "second shape's area is " << pShape[1]->getArea() << endl;
cout << "third shape's area is " << pShape[2]->getArea() << endl;
}
and thanks again for the hints!
-
January 21st, 2004, 06:45 PM
#7
That's exactly how I would do it... in fact I did. I implemented your project for fun...
Good job, and way to use the forum as it is intended!
-
January 21st, 2004, 09:58 PM
#8
Originally posted by h0t1ce
I got the program working like it should with polymorphism. All I'd like to know know is if you guys think I have found the solution the question was asking for....
One thing. It is "int main()", not "void main()". In C++, the main() function returns an int. Only broken or non-standard compilers screw this rule up by making main() have a void return. If on a job interview you were asked to write a main() function, and you started out with "void main()", you've started on the wrong foot.
Someone teaching C++ should know this, and if this is what you were taught (that void main() is legal C++), I wonder what other incorrect things were taught.
Regards,
Paul McKenzie
-
January 21st, 2004, 10:07 PM
#9
ok thanks for the pointer Paul...
I thought it was only WinMain that used a return int type.
I looked back at the notes and the books we have on the structure the main function should have, and it is always void main()...
but since you point this out I will start using the int return type and make sure I let my teachers know.
P.S. I sure wouldn't want to do a stupid mistake like that on an interview
-
January 22nd, 2004, 04:20 AM
#10
And if they give you any grief over it, quote them chapter and verse from the ISO C++ standard:
ISO C++ standard 1998, section 3.6.1:
2. An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined.
Also, if your teachers have any real knowledge, they'll soon be pointing out why
Code:
std::vector<Shape*> shapes;
is a much better option than
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|