Q2. Modify Q1 so draw( )is a pure virtual function. Try creating an object of ty
ID: 3906400 • Letter: Q
Question
Q2. Modify Q1 so draw( )is a pure virtual function. Try creating an object of type Shape. Try to call the pure virtual function inside the constructor and see what happens. Leaving it as a pure virtual, give draw( ) a definition.
here is Q1
#include <iostream>
using namespace std;
class Shape{
public:
virtual void draw(){
cout<<"Drawing Shape ";
}
};
class Circle : public Shape{
void draw(){
cout<<"Drawing Circle ";
}
};
class Square : public Shape{
void draw(){
cout<<"Drawing Square ";
}
};
class Triangle :public Shape{
void draw(){
cout<<"Drawing Triangle ";
}
};
int main()
{
// creating pointers for base class
Shape *s[5];
s[0] = new Shape();
// creating objects for child class
Circle *c = new Circle();
Square * sq = new Square();
Triangle * t = new Triangle();
// peforming upcasting
s[1] = c;
s[2] = sq;
s[3] = t;
// calling draw
s[1]->draw();
s[2]->draw();
s[3]->draw();
return 0;
}
Explanation / Answer
#include using namespace std; class Shape { public: virtual void draw() = 0; // pure virtual function }; class Circle : public Shape { void draw() { cout draw(); return 0; }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.