2704 - EE2310_Lab_13 Scoreboard

Time

2022/12/19 08:10:00 2022/12/19 10:30:00

Clarification

# Problem Asker Description Reply Replier Reply Time For all team

# Problem Pass Rate (passed user / total user)
13781 EE2310_Lab_13_1
13783 EE2310_Lab_13_2
13784 EE2310_Lab_13_3

13781 - EE2310_Lab_13_1   

Description

Basic Inheritance

What You Need to Do

  1. 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).
  2. Shape contains 3 member functions get_x(), get_y(), get_color(), and a constructor Shape(int, int, string).
  3. 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.
  4. Both Rectangle and Triangle have a member function output().
  5. 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




13783 - EE2310_Lab_13_2   

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

  1. Modify the class Shape by changing its privated data members to protected data members.
  2. We can remove all 3 member functions get_x(), get_y(), get_color(). Keep the constructor Shape(int, int, string).
  3. Define another member function output() in Shape that simply outputs I'm a shape\n.
  4. 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




13784 - EE2310_Lab_13_3   

Description

Virtual Functions & Dynamic Binding

What You Need to Do

  1. In the class Shape, add keyword virtual to the output() function as follows: virtual void output(); This allows the output() function of Shape to be overridden by the output() of Rectangle and Triangle in dynamic binding (when it's called from reference or a pointer).

  2. 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

Sample Input  Download

Sample Output  Download

Tags




Discuss