-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractical5.cpp
71 lines (68 loc) · 1.84 KB
/
practical5.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*Design a simple test application to demonstrate
dynamic polymorphism and RTTI.*/
#include <iostream>
#include <typeinfo> // For RTTI (typeid)
using namespace std;
// Base class
class Shape {
public:
virtual void draw() const { // Virtual function for dynamic polymorphism
cout << "Drawing a generic shape." << endl;
}
virtual ~Shape() {} // Virtual destructor for proper cleanup in derived classes
};
// Derived class: Circle
class Circle : public Shape {
public:
void draw() const override { // Override base class method
cout << "Drawing a Circle." << endl;
}
};
// Derived class: Rectangle
class Rectangle : public Shape {
public:
void draw() const override { // Override base class method
cout << "Drawing a Rectangle." << endl;
}
};
// Derived class: Triangle
class Triangle : public Shape {
public:
void draw() const override { // Override base class method
cout << "Drawing a Triangle." << endl;
}
};
// Function to demonstrate dynamic polymorphism and RTTI
void identifyAndDraw(Shape* shape) {
// Using RTTI to identify the type of object
cout << "Type of shape: " << typeid(*shape).name() << endl;
// Call the draw method (dynamic polymorphism)
shape->draw();
}
int main() {
cout << "---- Demonstrating Dynamic Polymorphism and RTTI ----" <<
endl;
// Dynamically create objects
Shape* shape1 = new Circle();
identifyAndDraw(shape1);
delete shape1; // Clean up memory
cout << endl;
Shape* shape2 = new Rectangle();
identifyAndDraw(shape2);
delete shape2; // Clean up memory
cout << endl;
Shape* shape3 = new Triangle();
identifyAndDraw(shape3);
delete shape3; // Clean up memory
cout << endl;
return 0;
}
/*OUTPUT
---- Demonstrating Dynamic Polymorphism and RTTI ----
Type of shape: 6Circle
Drawing a Circle.
Type of shape: 9Rectangle
Drawing a Rectangle.
Type of shape: 8Triangle
Drawing a Triangle.
*/