Returning Object & Const Member Function
Returning Object & Const Member Function
INTRODUCTION :
In C++ programming, objects can be passed to function in similar way as variables
and structures.
PROCEDURE TO PASS OBJECT TO FUNCTION
Passing Object to function in C++ Programming
Example to Pass Object to Function
C++ program to add two complex numbers by passing objects to function.
#include <iostream>
using namespace std;
class Complex
{
private:
int real;
int imag;
public:
Complex(): real(0), imag(0) { }
void Read()
{
cout<<"Enter real and imaginary number respectively:"<<endl;
cin>>real>>imag;
}
void Add(Complex comp1,Complex comp2)
{
real=comp1.real+comp2.real;
imag=comp1.imag+comp2.imag;
}
void Display()
{
cout<<"Sum="<<real<<"+"<<imag<<"i";
}
};
Here, real represents the real data of object c3 because this function is called using
code c3.add(c1,c2);
Here, imag represents the imag data of object c3 because this function is called using
code c3.add(c1,c2);
int main()
{
Complex c1,c2,c3;
c1.Read();
c2.Read();
c3.Add(c1,c2);
c3.Display();
return 0;
}
OUTPUT
Enter real and imaginary number respectively:
12
3
Enter real and imaginary number respectively:
2
6
2
Sum=14+9i
RETURNING OBJECT FROM FUNCTION
The syntax and procedure to return object is similar to that of returning structure from
function.
Returning Object from function in C++ Programming
Example to Return Object from Function
This program is the modification of above program displays exactly same output as
above. But, in this program, object is return from function to perform this task.
#include <iostream>
using namespace std;
class Complex
{
private:
int real;
int imag;
public:
Complex(): real(0), imag(0) { }
void Read()
{
cout<<"Enter real and imaginary number respectively:"<<endl;
cin>>real>>imag;
}
Complex Add(Complex comp2)
{
Complex temp;
temp.real=real+comp2.real;
temp.imag=imag+comp2.imag;
return temp;
3
}
void Display()
{
cout<<"Sum="<<real<<"+"<<imag<<"i";
}
Here, real represents the real data of object c1 because this function is called using
code c1.Add(c2)
Here, imag represents the imag data of object c1 because this function is called using
code c1.Add(c2) .
CONST MEMBER FUNCTIONS IN C++
A function becomes const when const keyword is used in functions declaration. The
idea of const functions is not allow them to modify the object on which they are called. It is
recommended practice to make as many functions const as possible so that accidental
changes to objects are avoided.
A const member function is indicated by a const suffix just after the member
function's parameter list. Member functions with a const suffix are called "const member
functions" or "inspectors." Member functions without a const suffix are called "nonconst member functions" or "mutators."
Following is a simple example of const function.
#include<iostream>
using namespace std;
class Test {
int value;
public:
Test(int v = 0) {value = v;}
int getValue() const {return value;}
};
We get compiler error if we add a line like "value = 100;"
4
in this function.
int main() {
Test t(20);
cout<<t.getValue();
return 0;
}
OUTPUT:
20
When a function is declared as const, it can be called on any type of object. Non-const
functions can only be called by non-const objects.
For example the following program has compiler errors.
#include<iostream>
using namespace std;
class Test {
int value;
public:
Test(int v = 0) {value = v;}
int getValue() {return value;}
};
int main() {
const Test t;
cout << t.getValue();
return 0;
}
OUTPUT:
passing 'const Test' as 'this' argument of 'int
5
// A read-only function
}
void Date::setMonth( int mn )
{
month = mn;
}
int main()
{
Date MyDate( 7, 4, 1998 );
const Date BirthDate( 1, 18, 1953 );
MyDate.setMonth( 4 ); // Okay
BirthDate.getMonth();
// Okay
6