Given the following Shape base class with a virtual function undefined Create a
ID: 3575528 • Letter: G
Question
Given the following Shape base class with a virtual function undefined Create a derived class called, Square, with the necessary attributes/Properties and virtual function from the base class Create a constructor with all the attributes and implement the virtual function Create an object and initialize the pointer variable, called myShape, Print the area using the shape object Change the property to your choice and print the area again #include using namespace std; class Shape {public: void printArea(); virtual double getArea()=0;}; void Shape::printArea(){coutExplanation / Answer
#include <iostream>
#include <conio.h>
using namespace std;
// Base class
class Shape {
public:
void printArea();
virtual double getArea()=0;
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
void Shape::printArea(){
cout<<"Area"<<getArea();
}
// Derived class
class Square: public Shape {
public:
double getArea() {
return (width * height);
}
};
int main(void) {
Square sqr;
cout<<"CS|| lab final exam"<<endl;
sqr.setWidth(5);
sqr.setHeight(7);
// Print the area of the object.
cout << "Total area: " << sqr.getArea() << endl;
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.