| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 13781 | EE2310_Lab_13_1 |
|
| 13783 | EE2310_Lab_13_2 |
|
| 13784 | EE2310_Lab_13_3 |
|
Description
Basic Inheritance
What You Need to Do
- Define a class
Shapethat contains two data members ofinttype:location_xandlocation_y. It also contains another data membercolorofstringclass (#include <string>for using thestringclass). Shapecontains 3 member functionsget_x(),get_y(),get_color(), and a constructorShape(int, int, string).- Define another two derived classes
RectangleandTrianglethat inherit fromShape. Both of them have a constructor with 3 arguments(int, int, string). Note that in these constructors you need to use the C++ initializer list to callShape's constructor, otherwise only the default constructor ofShapewill be called. - Both
RectangleandTrianglehave a member functionoutput(). - The main function is given as follows. Do not change it.
// Do NOT CHANGE main()! You may get penalty points.
int main() {
int x, y;
string str;
cin >> x >> y >> str;
Rectangle rec(x, y, str);
cin >> x >> y >> str;
Triangle tri(x, y, str);
rec.output();
tri.output();
return 0;
}
Input
10 7 blue
10 5 black
Output
I'm a Rectangle: location_x = 10, location_y = 7, color = blue
I'm a Triangle: location_x = 10 , location_y = 5, color = black
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Protected Members & Static Binding
In 13-1, Rectangle and Triangle do not have access to Shape's private members and we have to use access functions get_x(), get_y(), get_color(). This makes our program quite messy. On one hand, a derived class do not have access to the private members of its base class, which makes programming cumbersome. On the other hand, granting a derived class access to their private members is not a good idea either (since we can always access the private members of any class by simply inheriting it!) How to solve this problem? Using the protected members.
What You Need to Do
- Modify the class
Shapeby changing its privated data members to protected data members. - We can remove all 3 member functions
get_x(),get_y(),get_color(). Keep the constructorShape(int, int, string). - Define another member function
output()inShapethat simply outputsI'm a shape\n. - The main function is given as follows. Do not change it.
// Do NOT CHANGE main()! You may get penalty points.
int main() {
int x, y;
string str;
cin >> x >> y >> str;
Rectangle rec(x, y, str);
cin >> x >> y >> str;
Triangle tri(x, y, str);
rec.output();
tri.output();
Shape *p = &rec;
p->output();
p = &tri;
p->output();
return 0;
}
Input
10 7 blue
10 5 black
Output
I'm a Rectangle: location_x = 10, location_y = 7, color = blue
I'm a Triangle: location_x = 10 , location_y = 5, color = black
I'm a Shape
I'm a Shape
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Virtual Functions & Dynamic Binding
What You Need to Do
-
In the class
Shape, add keywordvirtualto theoutput()function as follows:virtual void output();This allows theoutput()function ofShapeto be overridden by theoutput()ofRectangleandTrianglein dynamic binding (when it's called from reference or a pointer). -
That's it. No need to change anything. The main function is the same as Lab 13-2. Do not change it.
Input
10 7 blue
10 5 black
Output
I'm a Rectangle: location_x = 10, location_y = 7, color = blue
I'm a Triangle: location_x = 10 , location_y = 5, color = black
I'm a Rectangle: location_x = 10, location_y = 7, color = blue
I'm a Triangle: location_x = 10 , location_y = 5, color = black