Skip to content

Commit dc933cc

Browse files
committed
add: cpp-programming-questions
1 parent e9311f8 commit dc933cc

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"files.associations": {
3+
"iostream": "cpp"
4+
}
5+
}

10_this_pointer/00_questions.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
One of the functions should be setData() to set the properties of the object. Make
99
sure the names of formal arguments are the same as names of instance variables.
1010

11-
02. Use this pointer to get the address of calling object. define a class A and a member function that returns the address of calling object.
11+
02. Use this pointer to get the address of caller object. define a class A and a member function that returns the address of caller object.
1212

1313
03. Define a class Complex with appropriate instance variables and member functions. Overload binary (+) to add two complex numbers and use this pointer to access members
1414

15-
04. 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.
15+
04. 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.
16+
17+
05. Use this pointer to get the reference of caller object. define a class A and a member function that returns the reference of caller object.
44.3 KB
Binary file not shown.

10_this_pointer/02_address_of_calling.cpp renamed to 10_this_pointer/05_reference_of_caller_object.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,20 @@ class A
1111
{
1212

1313
public:
14-
// // instance member function that returns the address of calling or current object
15-
A *getAddress()
14+
// // instance member function that returns the reference of caller or current object
15+
A &getReference()
1616
{
17-
return this; // this pointer points to the current object
17+
return *this; // this pointer points to the current object
1818
}
1919
};
2020

2121
// // Main Function Start
2222
int main()
2323
{
24-
A a1, *ptr;
24+
A a1;
2525

26-
// // get address of a1
27-
ptr = a1.getAddress();
28-
29-
cout << "\nAddress of Object (a1) => " << ptr;
26+
// // get reference of a1
27+
A refOfA1 = a1.getReference();
3028

3129
cout << endl; // Add new line
3230
cin.ignore();

0 commit comments

Comments
 (0)