Nesting of a member function in C++
In this article, we will discuss the nesting of a member function in C++ with its implementation.
Introduction:
Nesting of member functions in C++ refers to defining and using one member function within another member function of the same class. This notion enables you to write better-organized and modular code by dividing complicated jobs into smaller and more manageable portions.
Implementation:
Let's see the implementation of the nesting of member functions in C++.
Program:
#include <iostream>
class MathOperations {
private:
int result; // Store the result of operations
public:
MathOperations() : result(0) {}
// Member function to add two numbers
void add(int a, int b) {
result = a + b;
}
// Member function to subtract two numbers
void subtract(int a, int b) {
result = a - b;
}
// Member function to multiply two numbers
void multiply(int a, int b) {
result = a * b;
}
// Member function to perform a complex operation using the above functions
void performComplexOperation(int x, int y, int z) {
add(x, y); // Call the add function
subtract(result, z); // Call the subtract function with the result of add
multiply(result, 2); // Call the multiply function with the result of subtracting
}
// Member function to get the result
int getResult() {
return result;
}
};
int main() {
MathOperations calculator; // Create an object of the MathOperations class
// Use the nested functions to perform a complex operation
calculator.performComplexOperation(10, 5, 3);
// Get the result and print it
int result = calculator.getResult();
std::cout << "Result: " << result << std::endl;
return 0;
}
Output:
Result: 24
Explanation:
1. In this example, we start by including the necessary header files for input and output operations.
2. We define a C++ class called MathOperations:
- Inside the class, we have a private data member result, which will store the result of mathematical operations.
- The constructor MathOperations() initializes the result to 0 when an object of this class is created.
- The class has several public member functions to perform basic mathematical operations:
- add(int a, int b) adds two integers, a and b, and stores the result in the result member.
- Subtract (int a, int b) subtracts b from a and stores the result.
- Multiply (int a, int b) multiplies a by b and stores the result.
- There's also a performComplexOperation(int x, int y, int z) member function, demonstrating member functions' nesting. It uses the add, subtract, and multiply functions to perform a complex operation.
- Lastly, we have a getResult() member function to retrieve the value stored in the result.
3. In the main() function:
- We create an object named calculator of the MathOperations class. This object will allow us to perform mathematical operations and store results.
- We call the performComplexOperation(10, 5, 3) method on the calculator object. This complex operation is defined as adding 10 and 5, subtracting 3 from the result, and multiplying the final result by 2.
- After performing the complex operation, we use the getResult() method to retrieve the result, which is 24 in this case.
- At last, we print the result to the console using std::cout.
4. The program returns 0 from the main function, indicating successful execution.
When you run this program, it performs the complex mathematical operation defined in the performComplexOperation function, which is (10 + 5) - 3 * 2, resulting in 24. The output displayed will be: (Result: 24)
Complexity analysis:
Time Complexity:
- The constructor MathOperations() initializes the result variable. This simple assignment operation has a constant time complexity, denoted as O(1).
- The member functions add(int a, int b), subtract(int a, int b), and multiply (inta, int b) each performs basic arithmetic operations, which are also O(1) in terms of time complexity.
- The performComplexOperation(int x, int y, int z) function sequentially calls the above three functions. Since each function has O(1) time complexity, the entire perform complex operation function also has O(1) time complexity.
- In the main function, we use performComplexOperation(10,5,3), an O(1) time complex operation.
- After that, the getResult() function is used to retrieve the value previously placed in the result. Another O(1) operation is this one.
- Considering these variables, the program's time complexity is O(1), which means that its execution time is independent of the input size and the number of loops.
Space Complexity:
- The class MathOperations contains only one integer member variable result, and no matter how many objects of this class you create, the space required for this member variable remains constant. Therefore, the space complexity of the class is O(1).
- In the main() function, we create an object of the MathOperations class. This object requires some memory to store its member variables, including the result, but the memory used by this object is also constant and does not depend on the input size. The space complexity of the main() function is O(1).
Method 2: By using Operator Overloading.
Introduction:
Operator Overloading is a powerful feature in C++ that allows you to define custom behaviors for standard operators when used with user-defined data types or objects of custom classes. This feature enables you to extend the functionality of C++ operators to work seamlessly with your data structures, making your code more expressive, intuitive, and flexible.
Operator overloading is achieved by providing special member functions within your class called operator functions. These operator functions are named according to the overloaded operator and provide the logic for the custom operation. For example, overloading the + Operator involves defining a + Operator function inside your class.
Implementation:
Let's see the implementation of the operator overloading in C++.
Program:
#include <iostream>
class MathOperations {
private:
int result; // Store the result of operations
public:
MathOperations() : result(0) {}
//Operator overloading for addition
MathOperations operator+(const MathOperations& other) {
MathOperations temp;
temp.result = this->result + other.result;
return temp;
}
//Operator overloading for subtraction
MathOperations operator-(const MathOperations& other) {
MathOperations temp;
temp.result = this->result - other.result;
return temp;
}
//Operator overloading for multiplication
MathOperations operator*(const MathOperations& other) {
MathOperations temp;
temp.result = this->result * other.result;
return temp;
}
// Member function to set the result
void setResult(int value) {
result = value;
}
// Member function to get the result
int getResult() {
return result;
}
};
int main() {
MathOperations operand1, operand2, result; // Create objects of the MathOperations class
// Set values for operand1 and operand2
operand1.setResult(10);
operand2.setResult(5);
// Perform complex operations using operator overloading
result = operand1 + operand2 - operand2 * operand1;
// Get the result and print it
int finalResult = result.getResult();
std::cout << "Result: " << finalResult << std::endl;
return 0;
}
Output:
Result: -35
Explanation:
1. First, define a C++ class called MathOperations.
2. Inside the class:
- Declare a private integer variable result to store the result of operations.
- Define a constructor MathOperations() that initializes the result to 0.
- Overload the +, -, and * operators as member functions for the class, where each operator function takes a const MathOperations& parameter representing the right operand.
3. In the operator+ function:
- Create a temporary MathOperations object, temp.
- Compute the addition of the result of the current object (this->result) and the result of the right operand (other.result).
- Set the temp. Result to the computed sum.
- Return the temp object.
4. In the Operator- function:
- Create a temporary MathOperations object, temp.
- Compute the subtraction of the result of the current object (this->result) and the result of the right operand (other.result).
- Set the temp. Result in the computed difference.
- Return the temp object.
5. In the Operator* function:
- Create a temporary MathOperations object, temp.
- Compute the multiplication of the result of the current object (this->result) and the result of the right operand (other.result).
- Set the temp. Result in the computed product.
- Return the temp object.
6. Define member functions setResult(int value) to set the result value and getResult() to retrieve the result value.
7. In the main() function:
- Create objects of the MathOperations class: operand1, operand2, and result.
- Set values for operand1 and operand2 using the setResult method.
- Perform complex mathematical operations using operator overloading, such as result = operand1 + operand2 - operand2 * operand1.
- Retrieve the final result using the getResult method.
- Print the final result to the console.
8. End the program.
Complexity analysis:
Time complexity:
The time complexity of the provided code, which uses operator overloading for mathematical operations, is O(1) because the operations are performed in constant time, and the execution time does not depend on the input size.
Space complexity:
The space complexity is also O(1) as it uses a constant amount of memory regardless of the input or the number of objects created.