0

I keep getting this error message every time I try to compile my shape.h file:

Previous Declaration of 'class shape'. Base class not declared correctly?

The shape.h header file:

#include <iostream>
/*
  1. class Shape will have virtual functions called area() and perimeter()
  2. derived classes will encapsulate dimensions of the sides or radius depending on class used
  3. derived classes will have default constructors as well as overloaded constructors to initialize dimensional values
  4. each derived class will have own area and perimeter functions that will be used in polymorphic manner
  5. write class headers, implementations, and test files separately
*/
class shape //parent class
{

public:
  shape();
  virtual int area();
  virtual int perimeter();
};

This is implementation of the parent class shape.cpp:

#include "shape.h"
#include<iostream>
using namespace std;
shape::shape(){}

This is the derived class declaration of shape rectangle.h:

#include "shape.h"

class rectangle: public shape
{
public: 
  rectangle();//constructor
  rectangle(int l, int w ); //stands for length and width
  int perimeter(int, int); //function shared w/shape parent class
  int area(int, int);    //function shared w/shape parent class

private:
  int longside, wideside;           
};    

This is the implementation for rectangle class rectangle.cpp:

#include "rectangle.h"

rectangle::rectangle(){}

rectangle::rectangle(int l, int w)
{
  longside = l;

  wideside=w;
};

int rectangle::area(int wideside, int longside)
{
  int totarea;
  totarea = (wideside*longside);

  return totarea;
};

int rectangle::perimeter(int side1, int side2)
{
  int perim = ((wideside)+(longside));
  return perim;
};

This is the test file testfile.cpp:

#include <iostream>
#include "shape.h"
#include "rectangle.cpp"
using namespace std;
int main()
{
  rectangle rect1;  
  int side1, side2;
  cout<<"\n Find Area of an Rectangle" << endl;
  cout<<"\nChoose a length for sides 1 abd 2" << endl;
  cout<<"\nSide 1: "<< endl;
  cin >> side1;
  cout<<"\nSide 2: "<< endl;

  cout<<"\nRectangle's Area is:   "<< rect1.area( side1, side2 ) << endl;
  cout<<"\nRectangle's Perimeter is:   "<<rect1.perimeter( side1, side2 )<< endl;

  return 0;
}

The class shape is a base class for other shape objects. I'm just testing out functionality with a derived class called class rectangle. Aren't I supposed to make a header file every time a class is defined? I included all of my files from this program to see if that would help.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257

0 Answers0