| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 13789 | EE2310_Lec_14_1 |
|
| 13790 | EE2310_Lec_14_2 |
|
Description
Pure Virtual Functions & Abstract Classes
An abstract class is a class with a virtual function declaration only, i.e., no function implementation. In the example of Lab 13-3, Shape has the virtual function output(). In many cases, we actually do not need to use any object of Shape class, since we either use Rectangle or Triangledirectly instead of Shape. Therefore, we do not need to implement the output() function for Shape. We can use the following function delcaration
virtual void output() = 0;
in Shape's class definition. This means there is no function implementation and therefore we cannot define an object of Shape class any more. output() is a pure virtual function and Shape is an abstract class.
What You Need to Do
- In the class
Shape: (1) modifyoutput()to be a pure virtual function. (2) Define another pure virtual functionarea()which returns adouble. - In the class
Rectangle: (1) Add twoprotecteddata members:widthandheight, both are of typedouble. (2) Modify all member functions accordingly and implementarea(). - In the class
Triangle: (1) Add twoprotecteddata members:baseandheight, both are of typedouble. (2) Modify all member functions accordingly and implementarea(). - Define another derived class
CirclefromShapethat has oneprotecteddata memberradiusof typedoubleand three member functionsoutput(),area(), and a constructor. Use the following macro for the constant π
#define PI 3.14159
main() is given. Do no make any changes, otherwise you may get penalty points.
int main() {
double a, b;
int x, y; // location of the shape
string str;
cin >> a >> b >> x >> y >> str;
Rectangle rec(a, b, x, y, str);
cin >> a >> b >> x >> y >> str;
Triangle tri(a, b, x, y, str);
cin >> a >> x >> y >> str;
Circle cir(a, x, y, str);
// polymorphism using pointer
Shape *ptr = &rec;
ptr->output();
cout << ptr->area() << endl;
ptr = &tri;
ptr->output();
cout << ptr->area() << endl;
// polymorphism using reference
Shape &s1 = cir;
s1.output();
cout << s1.area() << endl;
return 0;
}
Input
3.54 2.7 10 7 blue
8.22 7.14 10 5 black
7.333 64 3 green
Output
blue Rectangle at (10,7)
9.558
black Triangle at (10,5)
29.3454
green Circle at (64,3)
168.932
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Templates and Generic Programing
You are going to implement a simple function GetMax that returns the bigger object of two. It works for any types/class that can be compared by a > or < operator. Check out function templates for its syntax.
main() is given. Do no make any changes, otherwise you may get penalty points.
int main () { // DO NOT CHANGE MAIN!!
int i, j;
double l, m;
string s1, s2;
cin >> i >> j >> l >> m >> s1 >> s2;
cout << GetMax(i, j) << endl << GetMax(l, m) << endl
<< GetMax<string>(s1, s2) << endl;
return 0;
}
Input
4 53 2.2 993.548 oh my!
Output
53
993.548
oh