1
+ /* *
2
+ * The code defines an abstract base class "polygon" with a pure virtual function "sides" and two
3
+ * derived classes "triangle" and "square" that implement the sides function to print the number of
4
+ * sides for each shape.
5
+ *
6
+ * @return The program returns 0, which indicates successful execution.
7
+ */
8
+
9
+ // Including Header Files
1
10
#include < iostream>
11
+
12
+ // Using namespace
2
13
using namespace std ;
14
+
3
15
class polygon
4
16
{
5
17
public:
@@ -9,7 +21,7 @@ class polygon
9
21
cout << " Abstract base class\n " ;
10
22
}
11
23
};
12
- class triangle : public polygon
24
+ class triangle : public polygon
13
25
{
14
26
public:
15
27
void sides ()
@@ -18,7 +30,7 @@ class triangle: public polygon
18
30
cout << " Implementing in derived class.\n " ;
19
31
}
20
32
};
21
- class square : public polygon
33
+ class square : public polygon
22
34
{
23
35
public:
24
36
void sides ()
@@ -27,6 +39,8 @@ class square: public polygon
27
39
cout << " Implementing in derived class.\n " ;
28
40
}
29
41
};
42
+
43
+ // Main Function
30
44
int main ()
31
45
{
32
46
triangle t;
@@ -36,17 +50,3 @@ int main()
36
50
s.sides ();
37
51
return 0 ;
38
52
}
39
- /* polygon p;
40
- Abstract-class.cpp:20:9: error: cannot declare variable 'p' to be of abstract type 'polygon'
41
- polygon p;
42
- ^
43
- Abstract-class.cpp:3:7: note: because the following virtual functions are pure within 'polygon':
44
- class polygon{
45
- ^~~~~~~
46
- Abstract-class.cpp:5:14: note: virtual void polygon::sides()
47
- virtual void sides()=0;
48
- ^~~~~*/
49
- /* Triangle has 3 sides.
50
- Implementing in derived class.
51
- Square has 4 sides.
52
- Implementing in derived class.*/
0 commit comments