Function Overloading

Alright, so I'm having issues compiling this program, and I only have one error and I've been stuck on it for hours trying different things. It says "no overloaded function takes two arguments" and it is referring to "Rectangle::Rectangle" in the file called "squaredefinition.h". It's on line 9. I'll post all the files below.

"ShapeDeclaration.h"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

class Shape
{
protected:
	bool filled;
public:
	Shape();
	~Shape();
	Shape(bool);
	void setFilled(bool);
	bool getFilled();
};



"RectangleDeclaration.h"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "ShapeDeclaration.h"
#include <iostream>
using namespace std;

class Rectangle: public Shape
{
protected:
	double length;
	double width;
public:
	Rectangle();
	~Rectangle();
	Rectangle(bool, double, double);
	void setSides(double, double);
	double getLength();
	double getWidth();
	double getPerimeter();
	double getArea();
	double getDiagonal();
};



"SquareDeclaration.h"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "RectangleDeclaration.h"
#include <string>
#include <iostream>
using namespace std;

class Square : public Shape, public Rectangle
{
private:
	string color;
public:
	Square();
	~Square();
	Square(bool, double, double, string);
	void setColor(string);
	string getColor();
	double getSquareDiagonal();
};



"ShapeDefinition.h"
1
2
3
4
5
6
7
8
9
10
11
12
#include "ShapeDeclaration.h"
#include <iostream>
using namespace std;

Shape::Shape() {}

Shape::Shape(bool filled)
{
	this-> filled = filled;
}

Shape::~Shape() {}



"RectangleDefinition.h"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include "RectangleDeclaration.h"
using namespace std;

Rectangle::Rectangle() {}

Rectangle::Rectangle(bool filled, double length, double width) : Shape(filled)
{
	this-> length = length;
	this-> width = width;
}

double Rectangle::getPerimeter()
{
	double P;
	P = 2 * (length + width);
	return P;
}

double Rectangle::getArea()
{
	double A;
	A = length*width;
	return A;
}

double Rectangle::getDiagonal()
{
	double D;
	D = sqrt(length*length + width*width);
	return D;
}

Rectangle::~Rectangle () {}



"SquareDefinition.h"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "SquareDeclaration.h"
#include <string>
#include <iostream>
using namespace std;

Square::Square() {}

Square::Square(bool filled, double length, double width, string color) : Shape(filled), Rectangle(length, width)
{
	this-> color = color;
}

double Square::getSquareDiagonal()
{
	double D;
	D = getLength()*sqrt(2);
	return D;
}

Square::~Square() {}



"ShapeMain.h"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "SquareDeclaration.h"
#include <iostream>
using namespace std;


int main()
{
	bool filled;
	double a;
	string color;
	cout<<"Is the shape filled? If it is true, enter 1, otherwise, enter 0."<<endl;
	cin>>filled;
	cout<<"Enter the side of the square: "<<endl;
	cout<<"a="; 
	cin>>a;
	cout<<"Enter the color of the square: "<<endl;
	cout<<"color="; 
	cin>>color;

	if(filled)
		{
			cout<<"The shape is filled."<<endl;
		}
	else
	{
		cout<<"The shape is not filled."<<endl;
	}

	Square mySquare(filled, a, a, color);
	cout<<"\nObject Square: a="<<mySquare.getLength()<<endl;
	cout<<"Perimeter: "<<mySquare.getPerimeter()<<endl;
	cout<<"Area: "<<mySquare.getArea()<<endl;
	cout<<"Diagonal: "<<mySquare.getSquareDiagonal()<<endl;
	cout<<"Color: "<<mySquare.getColor()<<endl;

	cout<<endl;
	system("pause");
	return 0;
}
The error seems quite spelled out to me. In Square::Square() you're calling the Rectangle constructor and passing it only two parameters:

Square::Square(bool filled, double length, double width, string color) : Shape(filled), Rectangle(length, width)

Rectangle only has two constructors; one taking no parameters and one taking three parameters:

1
2
Rectangle();
Rectangle(bool, double, double);


EDIT: By the way, there's no point in making Square inherit directly from both Rectangle and Shape. If you make Square inherit only from Rectangle then you're also making it inherit from Shape, indirectly.
Last edited on
I only have one error and I've been stuck on it
A novice plumber was working on a house when the journeyman plumber came to see how it was going. "Great!" said the novice. "I just have this one pipe to solder and them I'm done." The journeyman watched as the novice finished the last pipe. "Now turn on the water," he said. The notice turned on the water and 30 leaks appeared.

I'm afraid that your one error is hiding 30. I tried fixing them but gave up after a while. The errors mostly stem from the fact that everything is in include files. If you break it up into separate header and cpp files, it's much better. This code compiles and runs. See "dmh" in the comments for the changes I had to make.

Rectangle.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#pragma once					 // dmh
#include "Shape.h"
#include <iostream>
using namespace std;

class Rectangle:public Shape
{
  protected:
    double length;
    double width;
  public:
      Rectangle():length(0), width(0)
    {
    };						 // dmh
    // ~Rectangle(); // dmh
    Rectangle(bool, double, double);
    void setSides(double, double);
    double getLength()		// dmh
    {
	return length;
    }
    double getWidth()		// dmh
    {
	return width;
    }
    double getPerimeter();
    double getArea();
    double getDiagonal();
};


Shape.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#pragma once
#include <iostream>
using namespace std;

class Shape
{
  protected:
    bool filled;
  public:
    Shape();
    ~Shape();
    Shape(bool);
    void setFilled(bool);
    bool getFilled();
};

Square.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#pragma once
#include "Rectangle.h"
#include <string>
#include <iostream>
using namespace std;

class Square:public Rectangle	// dmh
{
  private:
    string color;
  public:
    Square();
    ~Square();
    Square(bool, double, double, string);
    void setColor(string);
    string getColor()		// dmh
    {
	return color;
    }
    double getSquareDiagonal();
};

main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

#include "Square.h"				 // dmh
using namespace std;

int
main()
{
    bool filled;
    double a;
    string color;
    cout << "Is the shape filled? If it is true, enter 1, otherwise, enter 0." << endl;
    cin >> filled;
    cout << "Enter the side of the square: " << endl;
    cout << "a=";
    cin >> a;
    cout << "Enter the color of the square: " << endl;
    cout << "color=";
    cin >> color;

    if (filled) {
	cout << "The shape is filled." << endl;
    } else {
	cout << "The shape is not filled." << endl;
    }

    Square mySquare(filled, a, a, color);
    cout << "\nObject Square: a=" << mySquare.getLength() << endl;
    cout << "Perimeter: " << mySquare.getPerimeter() << endl;
    cout << "Area: " << mySquare.getArea() << endl;
    cout << "Diagonal: " << mySquare.getSquareDiagonal() << endl;
    cout << "Color: " << mySquare.getColor() << endl;

    cout << endl;
    system("pause");
    return 0;
}

Rectangle.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include "Rectangle.h"
#include <cmath>
using namespace std;

// Rectangle::Rectangle() {}  // dmh

Rectangle::Rectangle(bool filled, double length, double width):Shape(filled)
{
    this->length = length;
    this->width = width;
}

double
Rectangle::getPerimeter()
{
    double P;
    P = 2 * (length + width);
    return P;
}

double
Rectangle::getArea()
{
    double A;
    A = length * width;
    return A;
}

double
Rectangle::getDiagonal()
{
    double D;
    D = sqrt(length * length + width * width);
    return D;
}

// Rectangle::~Rectangle () {}  // dmh 

Shape.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "Shape.h"
#include <iostream>
using namespace std;

Shape::Shape()
{
}

Shape::Shape(bool filled)
{
    this->filled = filled;
}

Shape::~Shape()
{
}

Square.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "Square.h"
#include <string>
#include <iostream>
#include <cmath>

using namespace std;

Square::Square()
{
}

Square::Square(bool filled, double length, double width, string color):
Rectangle(filled, length, width)		 // dmh
{
    this->color = color;
}

double
Square::getSquareDiagonal()
{
    double D;
    D = getLength() * sqrt(2);
    return D;
}

Square::~Square()
{
}

Last edited on
Topic archived. No new replies allowed.