C++ Inheritance and Constructors
- by DizzyDoo
Hello, trying to work out how to use constructors with an inherited class. I know this is very much wrong, I've been writing C++ for about three days now, but here's my code anyway:
clientData.h, two classes, ClientData extends Entity :
#pragma once
class Entity
{
public:
int x, y, width, height, leftX, rightX, topY, bottomY;
Entity(int x, int y, int width, int height);
~Entity();
};
class ClientData : public Entity
{
public:
ClientData();
~ClientData();
};
and clientData.cpp, which contains the functions:
#include <iostream>
#include "clientData.h"
using namespace std;
Entity::Entity(int x, int y, int width, int height)
{
this->x = x;
this->y = y;
this->width = width;
this->height = height;
this->leftX = x - (width/2);
this->rightX = x + (width/2);
this->topY = y - (height/2);
this-bottomY = y + (height/2);
}
Entity::~Entity()
{
cout << "Destructing.\n";
}
ClientData::ClientData()
{
cout << "Client constructed.";
}
ClientData::~ClientData()
{
cout << "Destructing.\n";
}
and finally, I'm creating a new ClientData with:
ClientData * Data = new ClientData(32,32,32,16);
Now, I'm not surprised my compiler shouts errors at me, so how do I pass the arguments to the right classes?
The first error (from MVC2008) is
error C2661: 'ClientData::ClientData' : no overloaded function takes 4 arguments
and the second, which pops up whatever changes I seem to make is
error C2512: 'Entity' : no appropriate default constructor available
Thanks.