![]() |
|
#1
|
|||
|
|||
polymorphism...desperate stuffhi everybody.
i have some strong stuff on my hands, and it ain't easy to do. at least not for me the c++ newbie. i was handed the following problem by my instructor with a limited time to finish it. I am not even through with reading about polymorphism in c+ but the program has to be done. i would appreciate any kind of help that anyone can offer. the problem reads as follows : "Implement a shape hierarchy ( shape->twodimesnion&three dimension->circle->sphere->rectangle...blah blah blah). Each twodimesionalshape should contain function getArea to calculate the area of the two dimensional shape. Each threedimensionalshape should have member functions getArea and getVolume to return both area and volume of the three dimensional shape. Create a vector of shape pointers to objects of each concrete class in the hierarchy. the program should print the the object to wich each vector element points. Also, in the loop; that processes all the shapes in the vector, determine in an object is a atwo dimensional or a three dimensional shape." and here is so far what i have done. I have created a "shape" class and its defenition as follows: CPP / C++ / C Code:
CPP / C++ / C Code:
agian i would really appreciate any kind of help. i would like to create the twodimensionalshape classbut i am unsure what to include and how to even approach the problem. please help. thanks |
|
#2
|
||||
|
||||
|
Derive from the shape class. You HAVE TO implement each and everyone of the pure virtual methods in the derived classes (the ones with =0).
You can (and probably should) also implement the "simple" virtual ones, but you MUST implement them in the the base class too (shape). In case there is no implementation found in one or more of the subclasses, this implementation will be the one used when you call the method. Finally, to make use of polymorphism you need to have a construct like this: CPP / C++ / C Code:
If callMethod() is virtual in base and redefined in derived the code in defined will be used. If you have 2 such classes, derived1 and derived2, each with its own implementation for callMethod, the right method will be used and so on... I realise this is a very poor explanation, so if you have more questions, please feel free to ask. Kind regards, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#3
|
|||
|
|||
i am trying this...am i going wrong at this?this is another try at the problem. i am still not sure that i am doing this stuff right. kinda like stumbeling in the dark here.
i hope this is a good way to attak this multiple inheritence. i am absolutly not a pro at the subject. CPP / C++ / C Code:
i appreciate any advice. |
|
#4
|
||||
|
||||
|
Hi.
I am in a BIG hurry. Sorry about that. I modified your code so it compiles - you had lots of misspelling errors. Be careful, in C++ there is a difference between capital and lower case letters. The important thing: CPP / C++ / C Code:
Notice that I changed the names of your classes, since you can't start them with a number. I'll give you a working version, but somebody else will probably better help you (if not, I promise to do it later this evening). However, it seems to me you are on the right track: CPP / C++ / C Code:
Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#5
|
||||
|
||||
|
Oh, and try to put cout's in every function (just print the name and scope of the function). Then try them to see which is called when. Read my previous post about Base * p = new Derived. (Homework
)__________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#6
|
|||
|
|||
thank you very much guysi really appreceate the help.
i have tried to compile lucian's file and it compiled fine,i'm in the process of developping a driver to test the program and see if it works. will post the result in a few. ciao |
|
#7
|
|||
|
|||
here's the complete program with driver..this is the file..with driver. i am not sure though if the program runs..it compiles without error..but how to be sure of the results. ?
my compiler would not link the program CPP / C++ / C Code:
i get the following message when i try to link the program Linking... LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main Debug/try1.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. what does tht mean? |
|
#8
|
||||
|
||||
|
Quote:
I'm not sure I understood correctly, but did you build you main in the header? You need to have that in a .cpp file. __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#9
|
||||
|
||||
|
I tried to compile your last post and got a bunch of errors from my compiler. I am using GNU gcc (cygwin) with -Wall. As Lucian pointed out you need to make some changes.
You are more or less there as far as I can see. How about something like: CPP / C++ / C Code:
The cout in virtualviapointer had a spelling error with baseclassptr->getarea() and I had to change your vector stuff a little. I have little experience with that but I just matched the vector type to the type of your virtualviapointer() def. Since shape_main.cpp includes shape.h how about something like: CPP / C++ / C Code:
This is your code with no changes but to only have your classes information and a few inlined methods. You then just need to turn your implementation into a .cpp file and compile that with something like, Code:
Mark __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs |
|
#10
|
|||
|
|||
worked finethank you so much guys u gave precious advice. i really appreciate it.
the code works fine now. |
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| What is the best place to buy computer stuff? | Bulkhead | Computer Hardware Forum | 21 | 16-Jun-2005 06:50 |
| .o compile UNIX stuff ... help please! | crq | C++ Forum | 5 | 28-Jan-2005 07:43 |
| Invasion of Spyware and other stuff | sweetjeebus05 | Computer Software Forum - Windows | 3 | 11-Oct-2004 22:31 |
| VFW Video Formatting - Desperate Help! | surgio | MS Visual C++ / MFC Forum | 1 | 21-Jul-2004 15:19 |
| Apache multiple localhosting - desperate! | jnorris | Apache Web Server Forum | 8 | 23-Nov-2003 23:55 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The