Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question: Please solve my parametrized constructor and other error, explain usin

ID: 3541330 • Letter: Q

Question

Question:



Please solve my parametrized constructor and other error, explain using comment // what is my wrong.


My code:


#include <iostream>

using namespace std;

class Shape
{

    public:
            virtual void Draw()
            {

            }

};

class Rectangle: public Shape
{

    protected:
              float length, width;

    public:
            Rectangle(float leng, float widt)
            {

                length=leng;
                width=widt;

            }

            Rectangle()
            {

            }

            void Draw()
            {

                cout<<"* * * * * *"<<endl;
                cout<<"* * * * * *"<<endl;
                cout<<"* * * * * *"<<endl;
                cout<<"* * * * * *"<<endl;

            }

};

class Square: public Rectangle
{

    public:
            Square(float leng, float widt)
            {

                length=leng;
                width=widt;

            }

            void Draw()
            {

                cout<<"* * * * * *"<<endl;
                cout<<"* * * * * *"<<endl;
                cout<<"* * * * * *"<<endl;
                cout<<"* * * * * *"<<endl;
                cout<<"* * * * * *"<<endl;
                cout<<"* * * * * *"<<endl;

            }

};

int main()
{

    int decision;

    cout<<"Output:  (1)Rectangle  (2)Square  (3)Quit: ";
    cin>>decision;

    Shape * shape = NULL;

    switch(decision)
    {

        case 1:
                shape = new Rectangle;

                shape->Draw();

                break;

        case 2:
                shape = new Square;

                shape->Draw();

                break;

        case 0:
                return 0;

        default:

                cout<<"Type Wrong"<<endl;


    }

    delete shape;

    return 0;
}

Create a Shape class. No data member Member function virtual void Draw(). (empty) Create class Rectangle which is derived from class Shape Data member length and width Member function parameterized constructor to set length and width default constructor void Draw() - to draw a rectangle using' *' Create class Square which is derived from class Rectangle No data member Member function parameterized constructor to set length. void Draw() - to draw a square using' *' Each of the derived classes will override the virtual function Draw() Write a C++ program to produce the output depend on user choice. Output: Regtangle. Square Quit: 1 Output: egtangle. Square Quit: 1

Explanation / Answer

#include <iostream>
using namespace std;
class Shape
{
public:
virtual void Draw()
{
}
};

class Rectangle: public Shape
{
protected:
float length, width;
public:
Rectangle(float leng, float widt)
{
length=leng;
width=widt;
}
Rectangle()
{
length=0;
width=0;
}
virtual void Draw()
{
for(int i=0; i<length; i++)
{
for(int j=0; j<width; j++)
cout<<"* ";
cout << endl;
}
}
};

class Square: public Rectangle
{
public:
Square(float leng):Rectangle(leng, leng)
{

}
/*
void Draw()
{
for(int i=0; i<length; i++)
{
for(int j=0; j<width; j++)
cout<<"* ";
cout << endl;
}
}*/
};
int main()
{
int decision;
cout<<"Output: (1)Rectangle (2)Square (3)Quit: ";
cin>>decision;
Shape * shape = NULL;
cout << endl;
switch(decision)
{
case 1:
shape = new Rectangle(3,6);
shape->Draw();
break;
case 2:
shape = new Square(6);
shape->Draw();
break;
case 0:
return 0;
default:
cout<<"Type Wrong"<<endl;
}
delete shape;
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote