varrick
varrick

Reputation: 91

I keep getting an error when I use this code. I'm trying to use constructors in an inherited class. c++

So, my problem is I don't really know a lot about classes. So, I am trying to get this constructor to work. I need the base constructor and the constructor of the derived class to work without implementing it there. I can define it there i just can't implement it. The compiler is telling me it's expecting a curly brace. #ifdef SHAPE.H #endif SHAPE.H #define

#include<string>
using namespace std;
class QuizShape
{
    private:    
        char outer, inner;
        string quizLabel;

    public:
        //Constructor
        QuizShape();
};

class Rectangle : public QuizShape
{
    public:
        int height, width;

        //Getter & setter methods
        int getHeight() const;
        void setHeight(int);
        int getWidth() const;
        void setWidth(int);

        //Constructor for Rectangle
        Rectangle() : QuizShape();
};

class Square : public Rectangle
{
    public:
        //constructors
        Square() : Rectangle (); This area here is where the error comes // IT says it expects a { but I'm not allowed to define the constructor in line.
        Square(int w, int h) : Rectangle (height , width);
}; 

class doubleSquare : public Square
{
//Fill in with constructors 
};

I don't understand the error it's giving me. I'm pretty sure I'm not redefining it either.

Upvotes: 1

Views: 76

Answers (2)

Qaz
Qaz

Reputation: 61910

Move your constructor initializer lists to the definitions. For example, for Square:

//declarations
Square();
Square(int w, int h);

//definitions
Square() : Rectangle() {/*body*/}
Square(int w, int h) : Rectangle(w, h) {/*body*/} //assuming you meant w, h

Do that for the other constructors with initializer lists in the declarations as well.

Upvotes: 0

Arun
Arun

Reputation: 2092

The constructor needs to be defined. Pls observe the changes in the way constructors are defined/used.

    #include<string>
    using namespace std;
    class QuizShape
    {
        private:    
            char outer, inner;
            string quizLabel;

        public:
            //Constructor
            QuizShape();
    };

    class Rectangle : public QuizShape
    {
        public:
            int height, width;

            //Getter & setter methods
            int getHeight() const;
            void setHeight(int);
            int getWidth() const;
            void setWidth(int);

            //Constructor for Rectangle
            Rectangle() { } 
            Rectangle(int h, int w): height(h), width(w) { }
    };

    class Square : public Rectangle
    {
        public:
            //constructors
          Square() { }  // 
          Square(int w, int h) : Rectangle (h, w) {}
    }; 

    class doubleSquare : public Square
    {
    //Fill in with constructors 
    };

Upvotes: 1

Related Questions