GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Apr-2005, 10:57
ap6118 ap6118 is offline
Junior Member
 
Join Date: Feb 2005
Posts: 62
ap6118 is on a distinguished road

polymorphism...desperate stuff


hi 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:
#ifndef SHAPE_H
#define SHAPE_H

#include <cstring>

using std::string;

class shape {
public:
	//virtual function that returns shape area
	virtual double getarea() const;
	//virtual function that returns shape volume
	virtual double getvolume() const;
	//pure virtual overrriden functions
	virtual string getName() const = 0;//to return the name
	virtual void print() const = 0;//outputs the shape

};

#endif

CPP / C++ / C Code:
#include <iostream>

using std::cout;
#include "shape.h"

//return area
double getarea() const
{ return 0.0; }
//return volume
double getvolume() const
{ return 0.0; }

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  
Old 13-Apr-2005, 15:15
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 924
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
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:
class base
{
    virtual void callMethod();
};

//...

class derived : public base
{
    void callMethod();
};

//...

base * pc_TestClass = new derived();
pc_TestClass->callMethod();

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  
Old 18-Apr-2005, 19:32
ap6118 ap6118 is offline
Junior Member
 
Join Date: Feb 2005
Posts: 62
ap6118 is on a distinguished road

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:
#include <cstring>

using std::string;

class shape {
public:
virtual bool get_type() = 0;
};

class 2D : public shape {
public:
 virtual double getArea() const { return 0.0; }
 virtual void print () const = 0;
};

class 3D : public 2D : public shape {
public:
 virtual double  getVolume() const { return 0.0; }
 virtual void print() const = 0;
};

class point : public 2D {
public :
 point ( int = 0, int = 0 );//default const

 void setX( int )//set x in coordinate pair
 int getX() const;//return x from coordinate pair

 void setY ( int ); //set y in coordinate pair
 int getY() const; // return y from coordinate pair

 virtual bool get_type();
 virtual void print() const;//output point object

 private:
 int x;
 int y;
};//end class point

point::point ( int xvalue, int yvalue )
          : x( xvalue ), y( yvalue )
		  {
		  //empty body
		  }
void point ::setX( int xvalue )
{
   x = xvalue;
}

int point::getX() const
{
   return x;
}

void pont::setY( int Yvalue )
{
  y = yvalue;
}
int point::getY() const
{
  return y;
}
bool point::get_type()
{
  return true;
}
void point::print() const
{
  cout << '['<<getX() <<", "<<getY() <<']':
}

i appreciate any advice.
  #4  
Old 18-Apr-2005, 22:41
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 924
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
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:
class D3 : public D2 : public shape //wrong
class D3 : public D2 // this will do, since D2 already inherits shape

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:
#include <cstring>

using std::cout;
using std::endl;
using std::string;

class shape {
public:
	virtual bool get_type() = 0;
};

class D2 : public shape {
public:
	virtual double getArea() const { return 0.0; }
	virtual void print () const = 0;
};

class D3 : public D2 {
public:
	virtual double getVolume() const 
	{
		return 0.0; 
	}
	virtual void print() const = 0;
};

class point : public D2 {
public :
	point ( int = 0, int = 0 );//default const

	void setX( int );//set x in coordinate pair
	int getX() const;//return x from coordinate pair

	void setY ( int ); //set y in coordinate pair
	int getY() const; // return y from coordinate pair

	virtual bool get_type();
	virtual void print() const;//output point object

private:
	int x;
	int y;
};//end class point

point::point ( int xvalue, int yvalue ) : x( xvalue ), y( yvalue )
{
	//empty body
}

void point ::setX( int xvalue )
{
	x = xvalue;
}

int point::getX() const
{
	return x;
}

void point::setY( int yvalue )
{
	y = yvalue;
}

int point::getY() const
{
	return y;
}
bool point::get_type()
{
	return true;
}
void point::print() const
{
	cout << "[" << getX() <<", "<< getY() <<']';
}
Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #5  
Old 18-Apr-2005, 22:43
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 924
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
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  
Old 19-Apr-2005, 09:41
ap6118 ap6118 is offline
Junior Member
 
Join Date: Feb 2005
Posts: 62
ap6118 is on a distinguished road

thank you very much guys


i 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  
Old 19-Apr-2005, 12:37
ap6118 ap6118 is offline
Junior Member
 
Join Date: Feb 2005
Posts: 62
ap6118 is on a distinguished road

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:
#ifndef SHAPE_H
#define SHAPE_H
#include<iostream>
#include <cstring>
#include< vector >
using namespace std;
//using std::endl;
//using std::string;

class shape {
public:
  virtual bool get_type() = 0;
};

class D2 : public shape {
public:
  virtual double getArea() const { return 0.0; }
  virtual void print () const = 0;
};

class D3 : public D2 {
public:
  virtual double getVolume() const 
  {
    return 0.0; 
  }
  virtual void print() const = 0;
};

class point : public D2 {
public :
  point ( int = 0, int = 0 );//default const

  void setX( int );//set x in coordinate pair
  int getX() const;//return x from coordinate pair

  void setY ( int ); //set y in coordinate pair
  int getY() const; // return y from coordinate pair

  virtual bool get_type();
  virtual void print() const;//output point object

private:
  int x;
  int y;
};//end class point

point::point ( int xvalue, int yvalue ) : x( xvalue ), y( yvalue )
{
  //empty body
}

void point ::setX( int xvalue )
{
  x = xvalue;
}

int point::getX() const
{
  return x;
}

void point::setY( int yvalue )
{
  y = yvalue;
}

int point::getY() const
{
  return y;
}
bool point::get_type()
{
  return true;
}
void point::print() const
{
  cout << "[" << getX() <<", "<< getY() <<']';
}
 void virtualviapointer( const point * );
// void virtualviareference( const shape & );
 
 int main()
{
 point point( 7, 11 );

 point.print();
 cout << '\n';

 vector< point * > pointvector (1);
 shapevector[ 0 ] = &point;

 for ( int i =0;i < pointvector.size(); i++ )
       virtualviapointer( pointvector[ i ] );

	   return 0;
}//end main

void virtualviapointer( const point *baseclassptr )
{
cout << baseclassptr->print();
cout << "\narea is "<<baseclassptr->getarea();
}

#endif

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  
Old 19-Apr-2005, 13:00
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 924
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by ap6118
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/try1.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

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  
Old 19-Apr-2005, 15:51
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
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.
  1. create a .cpp file for your main code
  2. create a header file for your classes
  3. I suggest also an implementation file (header_name.cpp) for your methods

You are more or less there as far as I can see. How about something like:

CPP / C++ / C Code:
// shape_main.cpp
#include <iostream>
#include <vector>
#include "shape.h"
using std::cout;
using std::endl;
using std::vector;

void virtualviapointer( const point * );
 
int main()
{
  point mypoint( 7, 11 );

  mypoint.print();
  cout << '\n';

  vector <const point*> pointvector (1);
  pointvector[ 0 ] = &mypoint;

  for ( unsigned int i =0;i < pointvector.size(); i++ )
    virtualviapointer( pointvector[i] );

  return 0;
}//end main

void virtualviapointer( const point *baseclassptr )
{
  baseclassptr->print();
  cout << "\narea is " << baseclassptr->getArea() << endl;
}

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:
#ifndef SHAPE_H
#define SHAPE_H

class shape {
  public:
    virtual bool get_type() = 0;
};

class D2 : public shape {
  public:
    virtual double getArea() const { return 0.0; }
    virtual void print () const = 0;
};

class D3 : public D2 {
  public:
    virtual double getVolume() const
    { return 0.0; }
    virtual void print() const = 0;
};

class point : public D2 {
  public :
    point ( int = 0, int = 0 );//default const

    void setX( int );//set x in coordinate pair
    int getX() const;//return x from coordinate pair

    void setY ( int ); //set y in coordinate pair
    int getY() const; // return y from coordinate pair

    virtual bool get_type();
    virtual void print() const;//output point object

  private:
    int x;
    int y;
};//end class point
#endif

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:
g++ shape_main.cpp shape.cpp -o ShapeMain
or whatever your particular compiler insists upon.

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  
Old 19-Apr-2005, 19:38
ap6118 ap6118 is offline
Junior Member
 
Join Date: Feb 2005
Posts: 62
ap6118 is on a distinguished road

worked fine


thank you so much guys u gave precious advice. i really appreciate it.
the code works fine now.
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 21:02.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.