+ 2

How to return function from an object and why we use that in c++?

18th May 2018, 10:25 PM
Hala Hala
Hala Hala - avatar
5 Answers
+ 7
#include <iostream> using namespace std; class ccc { public: int a,b; ccc func(int a,int b)//function returning object { ccc y; y.a=a; y.b=b; return y; } }; int main() { ccc p; p=p.func(5,6);//function calling cout<<p.a<<p.b; return 0; }
20th May 2018, 6:06 AM
Sachin
Sachin - avatar
+ 3
To show the result. For example, if you have an add function, you want to know its result. int add(const int a, const int b) { return a + b; }
19th May 2018, 12:52 PM
Timon Paßlick
+ 2
Do you mean returning an object from a function?
19th May 2018, 4:02 AM
Timon Paßlick
+ 1
yes that is the correct, that is what I mean
19th May 2018, 12:15 PM
Hala Hala
Hala Hala - avatar
+ 1
I mean in class I read this but didn't understand https://www.programiz.com/cpp-programming/pass-return-object-function
19th May 2018, 5:00 PM
Hala Hala
Hala Hala - avatar