|
Time |
Memory |
Case 1 |
1 sec |
32 MB |
Description
Basic Inheritance
What You Need to Do
- Define a class
Shape
that contains two data members of int
type: location_x
and location_y
. It also contains another data member color
of string
class (#include <string>
for using the string
class).
Shape
contains 3 member functions get_x()
, get_y()
, get_color()
, and a constructor Shape(int, int, string)
.
- Define another two derived classes
Rectangle
and Triangle
that inherit from Shape
. 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 call Shape
's constructor, otherwise only the default constructor of Shape
will be called.
- Both
Rectangle
and Triangle
have a member function output()
.
- 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
Tags