# C++ Help plz. :p

Discussion in 'OT Technology' started by Kirlain, Mar 27, 2009.

1. ### KirlainFirst world problems

Joined:
Sep 16, 2002
Messages:
8,054
0
Location:
Charlotte, NC
Part I: Define a Cylinder class which publicly inherits from the Circle class and add a member function "display( )" which displays the content of*the x, y, radius and height data members to the screen. (remember to include the Cylinder class constructor). (50%)

#include <iostream>
#include <vector>
using std::vector;
using std::cout;
using std::endl;

class Point
{
public:
Point(float a=0, float b=0){ x = a; y = b; }
float getx( ) const{ return x; }
float gety( ) const{ return y; }
void display() const {cout<< "x = "<<x<<" y = "<<y<<endl;}
private:
float x;
float y;
};

class Circle : public Point
{
public:
Circle(float a, float b, float r) : Point(a,b)
private:
};

This is the cylinder class I created....

class Cylinder : public Circle
{
public:
Cylinder(float a, float b, float r, float h) : Circle(a,b,r)
{ height = h; }
float getHeight() const
{ return height; }
void display() const { Circle:display(); cout << "height = " << height << endl; }
private:
float height;
};

and heres the main..

int main( )
{
Cylinder c(1,2,3,4);
vector<Point *> list;
list.push_back(&c);
list.push_back(new Circle(3,4,2));
list.push_back(new Cylinder(5,6,4,10));
list.push_back(new Point);
for(int i = 0; i < list.size() ; i++)
{
cout<<endl<<endl;
list->display();
}
system("pause");
return 0;
}

Now, this is the second part that I don't understand.

art II: Copy and compile your solution to part 1 and make the necessary change to the Point class so that the following driver code displays the given output: (50%)

Desired Output: (does not have to be in this exact format)

x = 1 y = 2
height = 4

x = 3 y = 4

x = 5 y = 6
height = 10

x = 0 y = 0
Press any key to continue . . .

I have to change the POINT class to make it output the radius/height it seems. How do I do that? Halp plz.

2. ### KirlainFirst world problems

Joined:
Sep 16, 2002
Messages:
8,054
0
Location:
Charlotte, NC
Really? Nobody? Virtual classes, polymorphism?

3. ### skinjobActive Member

Joined:
Jan 6, 2001
Messages:
2,337
0
Location:
Aztlán
Go lookup virtual member functions.

4. ### KrakerjakActive Member

Joined:
Jul 7, 2003
Messages:
8,288
0
Location:
Edmonton eh
I trying to understand what needs to be done.

You need to use the point class as an abstract class, with a virtual display function
.....such that you can call the correct display function depending on what type of object Point is pointing to.

5. ### DeathClockGuest

Man, I haven't used this stuff in a while. I do know how pure virtual functions and classes work with abstract base classes though, I vaguely remember using it when I was doing some hobbyist game programming... give me some time to look at it, I might even copy it into visual studio...

*edit* it looks like what you have is correct... circle inherits from point, its constructor uses the point constructor and adds initialization for the radius... It's display function calls the point display function and then also outputs the radius... It looks like you followed this same formula to extend the cylinder class from the circle class by adding initialization for the height variable, and also printing its value after calling the circle classes print function, which calls the point classes print function and adds the radius...

Then in main you explicitly create a cylinder object, add it to a vector, after which you are adding a circle object, another cylinder object, and then a point object... The only thing I am not sure of is calling the vectors push_back function with the return result of an instantiation of a new object. It should work... I would assume the return of the constructor would be the memory address at the base of the new object, I am just wondering if you pass that into a function if it is treated as an automatic variable and lost on the return, meaning the object is lost, I am not entirely sure about that and maybe someone else has more insight here. In any case if I was writing it I would be more comfortable having an actual persistent reference to these objects you are creating in main, without relying on the vector (in other words, the way you do the first cylinder).

But other than that it appears to be correct, at least your intent is correct if anything it is probably a syntax error or something stupid that is screwing you up.

Last edited by a moderator: Mar 28, 2009
6. ### DeathClockGuest

As an aside, I don't think this has anything to do with virtual functions/classes. I dug through some of my old code, and this is from a mario 3 clone I was working on, this is creating a pure virtual abstract base class called object from which I inherit all other classes I use (goombas, bricks, mario, turtles, etc... lol)

Code:
```class Object
{
public:

int type, x, y, z, width, height;
double xvel, yvel;
bool bumped;

virtual RECT getRect() const = 0;
virtual void draw() const = 0;
virtual bool onScreen() const = 0;
virtual bool inProcessRange() const = 0;
virtual bool collision(RECT, int)=0;
virtual int process()=0;
virtual void setFlag(int flag, void* data=0)=0;    //Flags for inter-object communication
};
```
And here is a pretty simple class that inherits from it, this class defines a platform that mario can jump through from beneath but land on and walk around on top of. You'll notice I don't overload all of the functions of the base class, specifically the "process" function because this object does not need to be processed, platforms don't do anything.

Code:
```class Platform : public Object
{
public:

Texture* texture;            //Texture file
int x, y, width, height;    //Coordinates and dimensions
RECT rect;                    //Bounding rect

Platform(Texture* t, int xa, int ya, int w, int h) :
texture(t), x(xa), y(ya), width(w), height(h)
{
z=1;
type=PLATFORM;
}

Platform(Texture* t, int xa, int ya) :
texture(t), x(xa), y(ya)
{
z=1;
type=PLATFORM;
width = texture->getWidth();
height = texture->getHeight();
}

bool onScreen() const
{
if(x+width>worldX && x<worldX+SCR_WIDTH)return true;
return false;
}

bool inProcessRange() const
{
return 0;
}

bool collision(RECT objBB, int dir)
{
RECT thisBB;
thisBB.left = x;
thisBB.top = y;
thisBB.right = x+width;
thisBB.bottom = y+height;

if(dir==DIRDOWN)
{
if(objBB.right-4 > thisBB.left &&
objBB.left+4 < thisBB.right-40 &&
objBB.bottom > thisBB.top &&
objBB.bottom < thisBB.top+2)
return true;
}

return false;
}

void draw() const
{
texture->Blit(x-worldX,y-worldY);
}

RECT getRect() const
{
return rect;
}

//Unused
int process()
{
return 0;
}

void setFlag(int flag, void* data=0)
{
}
};
```
But interestingly enough like the code you have for an example I also use a vector to store all of my objects

Code:
```int worldX=0, worldY=0;
double bkgX=0, bkgY=0;

#include <vector>
#include <fstream>
#include <fmod.h>
#include <time.h>
#include "defines.h"
#include <MyD3DGame.h>
#include "textures.h"
#include "object.h"
#include "main.h"
using namespace std;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{

MakeWindow(hInstance,"Super Mario XL");

InitD3D();
InitDirectInput();
InitTextures();

ID3DXFont* titleFont = CreateD3DFont("arial", 48);
ID3DXFont* subtitleFont = CreateD3DFont("arial", 24);

//Title Screen
while(1)
{
//Handle OS Messages
serviceOS();

BeginDrawing();
ClearBuffer();
DrawText("Super Mario XL", 375,200,500,500,titleFont);
DrawText("Preview Release 1",430,300,500,500,subtitleFont);
DrawText("Written by Edited for my protection",400,500,500,500,subtitleFont);
EndDrawing();
Present();

GetInput();
if(keys[ENTER])break;
}

while(1)
{
//Handle OS Messages
serviceOS();

//Get and Process User Input
GetInput();
if(keys[ESCAPE])break;

//Process Game Objects
for(int i=0; i<objectVector.size(); i++)
if(objectVector[i]->inProcessRange())
objectVector[i]->process();

//Draw Game Objects
BeginDrawing();
sky.Blit(0,0);

for(i=0; i<objectVector.size(); i++)
if(objectVector[i]->z==0 && objectVector[i]->onScreen())
objectVector[i]->draw();

for(i=0; i<objectVector.size(); i++)
if(objectVector[i]->z==1 && objectVector[i]->onScreen())
objectVector[i]->draw();

for(i=0; i<objectVector.size(); i++)
if(objectVector[i]->z==2 && objectVector[i]->onScreen())
objectVector[i]->draw();

for(i=0; i<objectVector.size(); i++)
if(objectVector[i]->z==3 && objectVector[i]->onScreen())
objectVector[i]->draw();

for(i=0; i<objectVector.size(); i++)
if(objectVector[i]->z==4 && objectVector[i]->onScreen())
objectVector[i]->draw();

EndDrawing();
Present();

//Sleep for 1ms to prevent 100% CPU usage
Sleep(1);
}

CloseD3D();

return 0;
}
```

Last edited by a moderator: Mar 28, 2009
7. ### ge0New Member

Joined:
Oct 31, 2005
Messages:
8,398
0
Location:
JERSEY
if this shit wa formatted better, I would look at it.

8. ### aethyrOT Supporter

Joined:
Aug 27, 2007
Messages:
160
0
Location:
Cali
Whats the driver code?

9. ### skinjobActive Member

Joined:
Jan 6, 2001
Messages:
2,337
0
Location:
Aztlán
Wow. The code is all there folks. Making the program work as desired requires the addition of only one keyword.

Joined:
Sep 16, 2002
Messages:
8,054