0% found this document useful (0 votes)
12 views5 pages

OOP-CLC-2024 Final - Shapes

The document outlines the final examination for the course CSC10003 Object-Oriented Programming, including a set of questions covering C++ concepts such as differences between structs and classes, code analysis, and implementation of a Complex class. It also includes tasks related to class diagrams, handling negative parameters in constructors, and displaying shapes with customizable characters. The exam is structured to test both theoretical knowledge and practical programming skills within a 100-minute timeframe.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

OOP-CLC-2024 Final - Shapes

The document outlines the final examination for the course CSC10003 Object-Oriented Programming, including a set of questions covering C++ concepts such as differences between structs and classes, code analysis, and implementation of a Complex class. It also includes tasks related to class diagrams, handling negative parameters in constructors, and displaying shapes with customizable characters. The exam is structured to test both theoretical knowledge and practical programming skills within a 100-minute timeframe.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

FINAL EXAMINATION

Course: CSC10003 OBJECT-ORIENTED PROGRRAMMING

Time: 100 minutes (10 points) Term: 1 – Academic year: 2024-2025

(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 }

0 int _x, _y; 3 double getR() { return _R; }


8 9
public: double calcArea() { return 3.14 * _R * _R;
0 4 }
Point(int x, int y) {
9 0
Circle() {
cout << “1st point constructor” <<
1 4
endl; cout << “Circle constructor” << endl;
0 1
_x = x; _name = “Circle”; _I = nullptr; _R = 0;
1 4
1 _y = y; 2 }
1 } 4 ~Circle() {
2 3
Point(const Point& I) { if (_I != nullptr)delete _I;
1 4
3 cout << “2nd point constructor” << 4 cout << “Circle destructor” << endl;
endl;
1 4 }
4 _x = I._x; 5
void showFigure() {
1 _y = I._y; 4
5 6 cout << _name << “: ” << calcArea() <<
} endl;
1 4
6 void setX(int x) { _x = x; } 7 }

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 cout << “Point destructor” << endl; 5 c->setI(I); c->setR(2);


0 1
} c->showFigure();
2 5
}; delete c;
1 2
}
2 5
2 3

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

2. Provide constructors for:

o Default initialization (0 + 0i),

o Parameterized initialization.

3. Overload the following operators:

o + to add two complex numbers,


o - to subtract two complex numbers,

o * to multiply two complex numbers.

4. Overload the stream operators << and >>:

o >> should allow the user to input the real and imaginary parts of a complex number.

o << should display a complex number in the format: a + bi.

Question 4 (4 points): Given this main function

#include <iostream>

#include <vector>

void main() {

std::vector<IShape*> shapes = {

new Rectangle(10, 6),

new Square(5),

new Rectangle(8, 5),

new Square(3)

};

for (const IShape* shape : shapes) {

std::cout << shape->toString() << "\n";

Sample output

Rectangle Width=10, Height=6


Square Side=5
Rectangle Width=8, Height=5
Square Side=3

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 *, +, @, -?

*********
*********
*********
*********

+++++
+ +
+ +
+ +
+++++

@@@@@@@@
@@@@@@@@
@@@@@@@@
@@@@@@@@
@@@@@@@@

---
- -
---

This line of code will not be relevant.

std::cout << shape->toString() << "\n";

Propose a solution to this problem. Redraw the class diagram or rewrite the affected block of code if
needed.
---End---

You might also like