-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSouvik File
57 lines (47 loc) · 1.47 KB
/
Souvik File
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
# Example Code For Abstract Class
// An abstract class is a class that is designed to be specifically used as a base class.
// An abstract class contains at least one pure virtual function.
// You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration.
/* A function declaration cannot have both a pure specifier and a definition.
struct A {
virtual void g() { } = 0;
};
*/
// Abstract class is used to use same characteristic with different classes and functions hiding the backend of the abstract class
#include <iostream>
using namespace std;
class Shape {
public:
virtual int Area() = 0; // Pure virtual function is declared as follows.
// Function to set width.
void setWidth(int w) {width = w;}
// Function to set height.
void setHeight(int h) {height = h;}
protected:
int width;
int height;
};
// A rectangle is a shape; it inherits shape.
class Rectangle: public Shape {
public:
int Area() {
return (width * height);
}
};
// A triangle is a shape too; it inherits shape.
class Triangle: public Shape {
public:
int Area() {
return (width * height)/2;
}
};
int main() {
Rectangle R;
Triangle T;
R.setWidth(5);
R.setHeight(10);
T.setWidth(20);
T.setHeight(8);
cout << "The area of the rectangle is: " << R.Area() << endl;
cout << "The area of the triangle is: " << T.Area() << endl;
}