Skip to content

Commit 548cf30

Browse files
committed
add: this_pointer 04
1 parent 46063ea commit 548cf30

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// // Create a class Rectangle and include a member function area and a function setDimensions to set width and height of the rectangle. Enable method chaining by returning the this pointer from the setDimensions function.
2+
3+
// // Header files
4+
#include <iostream>
5+
6+
// // use namespace
7+
using namespace std;
8+
9+
// // define class Rectangle
10+
class Rectangle
11+
{
12+
13+
private:
14+
// // instance member variables
15+
double length;
16+
double breadth;
17+
18+
public:
19+
// // instance member function to set dimensions
20+
Rectangle &setDimensions(double length, double breadth)
21+
{
22+
this->length = length;
23+
this->breadth = breadth;
24+
return *this;
25+
}
26+
27+
// // instance member function to find area of rectangle
28+
void area()
29+
{
30+
cout << "\nArea => " << length * breadth;
31+
}
32+
};
33+
34+
// // Main Function Start
35+
int main()
36+
{
37+
38+
// // create an instance of Rectangle
39+
Rectangle r1;
40+
41+
// // set dimensions and calculate area
42+
r1.setDimensions(5, 10).area();
43+
44+
cout << endl; // Add new line
45+
cin.ignore();
46+
return 0;
47+
}
48+
// // Main Function End
44.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)