-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFILE9.cpp
More file actions
65 lines (56 loc) · 1.71 KB
/
FILE9.cpp
File metadata and controls
65 lines (56 loc) · 1.71 KB
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
/*
In the program below we will see how constructors and destructors are implemented.
The program will be used to generate the area of a rectangle, give the length and width.
The program will also make use of scope resolution operator to define member functions outside the class specifier.
*/
#include <iostream>
using namespace std;
class rectangle // A simple class
{
private:
int height;
int width;
public:
rectangle(); // A constructor
int area();
void initialise(int, int);
~rectangle(); // And a destructor
};
// Constructors are used to automatically initialise an object when it is created, without the need to make a separate call to a member function.
rectangle::rectangle() // Constructor
{
cout << "Rectangle constructor in action" << endl;
height = 6;
width = 6;
}
int rectangle::area() // Area of a rectangle
{
return height * width;
}
void rectangle::initialise(int init_height, int init_width)
{
cout << "Initialising rectangle with given values" << endl;
height = init_height;
width = init_width;
}
// Following the same principle, a destructor is a function that is automatically called when an object is destroyed.
rectangle::~rectangle() // Destructor
{
cout << "Rectangle destructor in action" << endl;
}
int main()
{
rectangle box, square;
int can_ht, can_width, can_area;
cout << "The area of the box is " << box.area() << endl;
cout << "The area of the square is " << square.area() << endl;
box.initialise(12, 10);
square.initialise(8, 8);
cout << "The area of the box is " << box.area() << endl;
cout << "The area of the square is " << square.area() << endl;
can_ht = 50;
can_width = 6;
can_area = can_ht * can_width;
cout << "The area of the can is " << can_area << endl;
return 0;
}