OOP-CLC-2024 Final - Shapes
OOP-CLC-2024 Final - Shapes
(Notes: one A4 sheet of document (handwritten or printed text on both sides) is allowed)
Question 1 (1 point): Tell the differences between “struct” and “class” in C++
Question 2 (2 points): Assume all necessary libraries are included, read the C++ code below and
answer the following questions:
0 class Figure { 3 class Circle: public Figure {
1 2
protected: private:
0 3
2 string _name; 3 Point* _I; double _R;
0 public: 3 public:
3 4
virtual void showFigure() { void setI(const Point& I) {
0 3
cout << _name << endl; if (_I == nullptr) _I = new Point(I);
4 5
} else *(_I) = I;
0 3
5 virtual double calcArea() = 0; 6 }
0 }; 3 void setR(double R) {
6 7
class Point { if(R >= 0) _R = R;
0 3
7 private: 8 }
1 void setY(int y) { _y = y; } 4 };
7 8
1 int getX() { return _x; } 4 void main() {
8 9
int getY() { return _y; } Circle* c = new Circle();
1 5
9 ~Point() { 0 Point I(1, 2);
2 5
3 4
2 5
4 5
2 5
5 6
2 5
6 7
2 5
7 8
2 5
8 9
2 6
9 0
3 6
0 1
3 6
1 2
6
3
a) What are printed on the screen when compiling and executing the above program? (1 point)
b) Explain the order of execution of the program (1 point)
Question 3 (3 points): Write a C++ program to implement a Complex class to represent complex
numbers. The class should:
1. Have two private data members: real (for the real part) and imag (for the imaginary part).
o Parameterized initialization.
o >> should allow the user to input the real and imaginary parts of a complex number.
#include <iostream>
#include <vector>
void main() {
std::vector<IShape*> shapes = {
new Square(5),
new Square(3)
};
Sample output
Requirements.
1. Draw class diagram.
2. Declare and implement all the classes.
3. What if in the constructor of the two classes Rectangle and Square, the parameters are
negative? This would lead to the failure of initialization.
Propose a solution to this problem. Redraw the class diagram or rewrite the affected block of
code if needed.
4. What if we want to display these shapes into solid and hollow shapes, with customizable
display character like *, +, @, -?
*********
*********
*********
*********
+++++
+ +
+ +
+ +
+++++
@@@@@@@@
@@@@@@@@
@@@@@@@@
@@@@@@@@
@@@@@@@@
---
- -
---
Propose a solution to this problem. Redraw the class diagram or rewrite the affected block of code if
needed.
---End---