C++ returning objects

C++ RETURNING OBJECTS

A function can’t only receive objects as arguments but also can return them.

in the below program take how an object can be created(with in function ) and return to another function

#include<iostream>
using namespace std;
class complex // x+iy form
{
float x; // real part
float y; // imaginary part
public:
void input(float real, float img)
{
x=real;
y=img;
}
friend complex sum(complex, complex);
void show(complex);
};
complex sum(complex c1, complex c2)
{
complex c3; // in this statement object c3 is created
c3.x=c1.x+c2.x;
c3.y=c1.y=c2.y;
return(c3); // returns object c3
}
void complex:: show(complex c)
{
std::cout<<c.x<<"+j"<<c.y<<"\n";
}
int main()
{
complex a,b,c;
a.input(3.1,5.65);
b.input(2.75,1.2);
c=sum(a,b); // c=a+b;
std::cout<<"a=";
a.show(a);
std::cout<<"b=";
b.show(b);
std::cout<<"c=";
c.show(c);
}

Programme will generate this output
a=3.1+j5.65
b=2.75+j1.2
c=5.85+j6.85

The program adds two complex number a and b to produce a third complex number c and displays all the three numbers

const MEMBER FUNCTION

if a member function does not change(alter) any data in the class, then we may declare it as a const member function as follows

void mul(int,int) const;
double get_balance() const;

the qulaifier const is appended to the function prototypes(in the both declaration and definition) The compiler will give an error message if such functions try to alter the data values

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise