Virtual functions with pseudo-shape Objective: Create series of derived classes
ID: 3645058 • Letter: V
Question
Virtual functions with pseudo-shapeObjective: Create series of derived classes and use pointers to instantiate derived classes and use constructors and destructors. You will be adding three classes with constructors and destructors, and one function draw() for each of the three class.
1) Verify the program attached at the end of this document works. Verify the output of the program to be what is given. Understand the base class and the derived class line.
2) Add the class circle. The circle class should have additional parameter of radius. Add draw() function to circle to print out the information similar to given in line, but containing startx, starty, color, and radius.
Note: you do not have to implement the physical functions to draw on the screen. Just generate the text output to the screen. such as:
Circle(0.5, 0.5, green, 15.2) //where 15.2 is the radius
When the instant of the circle is deleted the following output
should be generated:
~Circle
3) Add class Text. The text should have additional parameter of [char *str]. Add draw() function for the text to print out the information similar to given in line, but containing startx, starty,
color, and text_it_self. Note: you do not have to implement the physical functions to draw the text on the screen. Just generate the text output to the screen. such as:
Text(1.5, 1.5, White, Hello) //where "hello" is the string
When the instant of the text is deleted the following output should
be generated:
~Text
4) Add class Square. The square should have additional parameter of length. Output the text output to the screen. such as:
Square(10.5, 9.5, green, 10); // where 10 is the length of side
When the instance of the Square is deleted the following output
should be generated to the screen:
~Square
5) Turn in the program listing, and the output of the program listing.
Note: The sample listing contains the base class and one derived class:
Line. You will need to add above three derived class with the
constructors and destructors for each of the derived class.
Note 2: The line 24 is the part of the constructor for Line which send
the variables x,y,c (xstart, ystart, col in the base class Shape) to the
base class.
Note 3: these three variables startx, starty, and col are/should be
common variables to all the derived classes. Therefore, these
three variables are declared within the "protected" to be available
to all its derived classes.
Note 4: The line 15, a function is defined as draw()=0; This creates a
"pure virtual function". i.e. that function is not called directly
from the program. This function is actually defined in the derived classes.
The key word "new" and "delete" are used to create instances of and
delete the instances of the class. They are used to create and erase
the memory usage via request to the operating system.
====================================================================
Your output of the program should be something similar to the below,
Note: your numbers within each line will be different.
[d99001411@puma cpp]$ group6b.out
Line(0.1, 0.1, blue, 0.4, 0.5)
Line(0.3, 0.2, red, 0.9, 0.75)
Circle(0.5, 0.5, green, 0.3)
Text(0.7, 0.4, yellow, Howdy!)
Circle(0.3, 0.3, white, 0.1)
Square(0.3, 0.3, white, 5.1)
~Line
~Line
~Circle
~Text
~Circle
~Square
[d99001411@puma cpp]$
Explanation / Answer
#include #include using namespace std; class a { protected: int test; public: virtual void SetTest(int arg) {test = arg;} int GetTest() {return test;} }; class b : public a { public: void SetTest(int arg) {test = arg+1;} }; class c : public a { public: void SetTest(int arg) {test = arg+2;} }; int main() { vector derivedClassHolder; derivedClassHolder.push_back(new b); derivedClassHolder.push_back(new c); derivedClassHolder.push_back(new a); derivedClassHolder.push_back(new c); derivedClassHolder.push_back(new b); for(int i = 0; i < (int)derivedClassHolder.size() ; i++) { derivedClassHolder[i]->SetTest(5); } for(int i = 0; i < (int) derivedClassHolder.size() ; i++) { coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.