Classes for multiple files in C ++

I am almost 100% sure that I have syntax in both of these classes, however I am getting the following errors:

For CShape.cpp - "error C2011: 'CShape': override of type 'class' For CCircle.cpp -" error CS2504: "CShape": base class undefined "

Here is the complete code for CShape.cpp

#include <iostream>
using namespace std;

class CShape
{
protected:
    float area;
    virtual void calcArea();
public:
    float getArea()
    {
        return area;
    }
}

And here is the code for CCircle.cpp

#include <iostream>
#include "CShape.cpp"
#define _USE_MATH_DEFINES
#include "math.h"
using namespace std;

class CCircle : public CShape
{
protected:
    int centerX;
    int centerY;
    float radius;
    void calcArea()
    {
        area = M_PI * (radius * radius);
    }
public:
    CCircle(int pCenterX, int pCenterY, float pRadius)
    {
        centerX = pCenterX;
        centerY = pCenterY;
        radius = pRadius;
    }
    float getRadius()
    {
        return radius;
    }
}

As you can see, CShape is the base class that CCircle suppsoed inherits. I'm new to C ++, so I may have the wrong file structures (maybe the base should be in the header file?), If something like that.

+5
source share
2 answers

#include.cpp files; , . #include, .cpp.

// CShape.h
class CShape
{
protected:
    float area;
    virtual void calcArea();
public:
    float getArea();
}

.cpp :

// CShape.cpp
#include "CShape.h"
#include <iostream>
using namespace std;

float CShape::getArea() {
    return area;
}

CCircle - CCircle.h # CShape.h, CCircle.cpp # CCircle.h.

+10

, ( ) ( .cpp). ( ) () . , .

CShape.h:

#ifndef __CSHAPE_H__
#define __CSHAPE_H__
class CShape
{
protected:
    float area;
    virtual void calcArea();
public:
    float getArea()
    {
        return area;
    }
};
#endif

CShape.cpp:

#include "CShape.h"

void CShape::calcArea()
{
    // Your implementation
}

CCircle.h:

#ifndef __CCIRCLE_H__
#define __CCIRCLE_H__
#include "CShape.h"

class CCircle : public CShape
{
protected:
    int centerX;
    int centerY;
    float radius;
    virtual void calcArea();
    {
        area = M_PI * (radius * radius);
    }
public:
     CCircle(int pCenterX, int pCenterY, float pRadius);
     inline float getRadius()
     {
         return radius;
     }
};
#endif

CCircle.cpp:

#include "CCircle.h"

CCircle::CCircle(int pCenterX, int pCenterY, float pRadius)
: centerX(pCenterX)
, centerY(pCenterY)
, radius(pRadius)
{
}
+4

All Articles