Topic1: Passing and Returning Objects in C++: Passing An Object As Argument
Topic1: Passing and Returning Objects in C++: Passing An Object As Argument
In C++ we can pass class’s objects as arguments and also return them from a
function the same way we pass and return other variables. No special keyword or
header file is required to do so.
To pass an object as an argument we write the object name as the argument while
calling the function the same way we do it for other variables.
Syntax:
function_name(object_name);
class Example {
public:
int a;
// Create objects
Example E1, E2;
return 0;
}
Topic 2: Returning Object as argument
Syntax:
object = return object_name;
function returns an object of type class
#include <bits/stdc++.h>
using namespace std;
class Example {
public:
int a;
return 0;
}
Topic 3: Inline Function
Any change to an inline function could require all clients of the function to
be recompiled because compiler would need to replace all the code once
again otherwise it will continue with old functionality.
To inline a function, place the keyword inline before the function name
and define the function before any calls are made to the function. The
compiler can ignore the inline qualifier in case defined function is more
than a line.
#include <iostream>
using namespace std;
inline int Max(int x, int y) {
return (x > y)? x : y;
}
// Main function for the program
int main() {
cout << "Max (20,10): " << Max(20,10) << endl;
cout << "Max (0,200): " << Max(0,200) << endl;
cout << "Max (100,1010): " << Max(100,1010) << endl;
return 0;
}