Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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";

    };

=======================================================

share|improve this question
 
C++ doesn't have a super keyword. It uses constructor initialization lists to initialize things (unless you count in-class member initialization, introduced in C++11). –  chris Jun 11 at 3:59
 
IMPORTANT NOTE: You are using OOP wrong. You just demonstrated a variant of the circle-ellipse-problem: en.wikipedia.org/wiki/Circle-ellipse_problem –  phresnel Jun 12 at 7:20

3 Answers

up vote 0 down vote accepted

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);
};
share|improve this answer
 
could be a little more informative? Where? –  Chuck Onwuzuruike Jun 11 at 4:00
 
holy crap thank you so much dude. Ive been at this for almost a week now. =) –  Chuck Onwuzuruike Jun 11 at 4:07

C++ is not having any super class like java. so you have to pass value to its parent class explicitly. While defining square class constructor initialization list pass width and height to parent class(Rectangle).

class Square : public Rectangle
    {

        public:
            Square(int x,int y):Rectangle(x,y)
         {   
         }

    };

int main()
{
Square s(4,4);
return 0;
}
share|improve this answer

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)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.