Skip to content

Commit 3c8ef60

Browse files
authoredJul 20, 2024
Update Abstract-class.cpp
Added comments
1 parent d985d61 commit 3c8ef60

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed
 

‎Abstract class/Abstract-class.cpp

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
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
110
#include <iostream>
11+
12+
// Using namespace
213
using namespace std;
14+
315
class polygon
416
{
517
public:
@@ -9,7 +21,7 @@ class polygon
921
cout << "Abstract base class\n";
1022
}
1123
};
12-
class triangle: public polygon
24+
class triangle : public polygon
1325
{
1426
public:
1527
void sides()
@@ -18,7 +30,7 @@ class triangle: public polygon
1830
cout << "Implementing in derived class.\n";
1931
}
2032
};
21-
class square: public polygon
33+
class square : public polygon
2234
{
2335
public:
2436
void sides()
@@ -27,6 +39,8 @@ class square: public polygon
2739
cout << "Implementing in derived class.\n";
2840
}
2941
};
42+
43+
// Main Function
3044
int main()
3145
{
3246
triangle t;
@@ -36,17 +50,3 @@ int main()
3650
s.sides();
3751
return 0;
3852
}
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

Comments
 (0)
Please sign in to comment.