1

and thank you for your help. This is my first time posting!

I'm trying to pass a Square class constructor to the Rectangle class constructor, and I get the following error:

Square.cpp:9: error: redefinition of ‘Square::Square(double)’ Square.hpp:19: error: ‘Square::Square(double)’ previously defined here

The objective is to pass a single parameter (twice, I presume) from the Square class to the parent Rectangle class (which accepts 2 parameters).

Here is my code:

// Square.hpp

#ifndef SQUARE_HPP
#define SQUARE_HPP
#include "Rectangle.hpp"
using namespace std;

class Square : public Rectangle
{
//private:

public:
Square(double sideSquare):Rectangle(sideSquare, sideSquare){};
//Square(double sideSquare):Rectangle(double, double){}

void setLength(double);
void setWidth(double);
};
#endif

// Square.cpp

#include "Square.hpp"
using namespace std;

Square::Square(double sideSquare) : Rectangle (sideSquare, sideSquare){}

void Rectangle::setLength(double lengthIn)
{length = lengthIn; width = lengthIn;}

void Rectangle::setWidth(double widthIn)
{width = widthIn; length = widthIn;} 

// squareMain.cpp
#include "Square.hpp"
#include <iostream>
using namespace std;

int main ()
{
Rectangle box1(14, 11.5);
cout << "box1 area: " << box1.area() << endl;
cout << "box1 perimeter: " << box1.perimeter() << endl;

return 0;  
}

I haven't include Rectangle.hpp and Rectangle.cpp. I really appreciate the help, thanks in advance

EGHM
  • 2,144
  • 23
  • 37
KernelBomb
  • 11
  • 1

0 Answers0