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;
}
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.