Reputation: 344
I do not understand why this program is not working. I'm new to C++, switching after three years of Java. I thought the error messages in Java made no sense but the errors I've been getting in C++ have been just straight gibberish. This is one I can actually understand.
Anyway so I have a program that has a Rectangle and Square Class. The square class inherits from the rectangle class. All my classes are all in different files.
================================(main)
#include <iostream>
#include "Rectangle.h"
#include "Square.h"
using namespace std;
int main(){
Square sq;
}//end main
================================(Rectangle.h)
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle{
public:
Rectangle (int, int);
void setLength (int);
void setWidth (int);
int getLength ();
int getWidth ();
int getArea ();
private:
int length;
int width;
};
#endif // RECTANGLE_H
=================================(Rectangle.cpp)
#include <iostream>
#include "Rectangle.h"
#include "Square.h"
using namespace std;
Rectangle :: Rectangle (int len, int wid){
length = len;
width = wid;
}//end constructor
void Rectangle :: setLength (int l){
length = l;
}//end setLength
void Rectangle :: setWidth (int w){
width = w;
}//end setWidth
int Rectangle :: getLength (){
return length;
}//end getLength
int Rectangle :: getWidth (){
return width;
}//end getWidth
int Rectangle :: getArea (){
return length * width;
}//end getArea
========================================(Square.h)
#ifndef SQUARE_H
#define SQUARE_H
class Square : public Rectangle
{
public:
Square();
};
#endif // SQUARE_H
====================================(Square.cpp)
#include <iostream>
#include "Rectangle.h"
#include "Square.h"
using namespace std;
Square :: Square {
//super :: Square(4, 3);
cout << "This is bullshit";
};
=======================================================
Upvotes: 0
Views: 3669
Reputation: 1719
Square sq;
This will call the default constructor of Square
which will first call the default constructor of the base class Rectangle
. A default constructor is a constructor that has no parameters or if it has parameters, all parameters have default values. C++ compiler would provide you one default constructor if you have not defined a constructor. Since you have defined a constructor Rectangle (int, int)
, compiler will not supply a default constructor. That is the reason for the error.
Solution is to provide a default constructor for Rectangle
class by defining a constructor which takes no parameters Rectangle()
or give default values to the parameters of the existing constructor, say Rectangle(int x=10, int y=20)
Upvotes: 2
Reputation: 1152
You need to pass parent constructor parameters via initialization list...
For example, if default Rectangle parameters are (4, 3):
Square :: Square( )
: Rectangle(4, 3)
{
//super :: Square(4, 3);
cout << "This is bullshit";
}
Or, it will be better:
Square :: Square(int len, int wid)
: Rectangle(len, wid)
{
}
And in header file:
class Square : public Rectangle
{
public:
Square(int len=4, int width=3);
};
Upvotes: 0