Returning Objects
Returning Objects
Returning Objects:
A function can not only receive objects as arguments but also can return them.
Example: Add two complex numbers A and B to produce a third complex number C and display
all the three numbers.
#include <iostream>
#include <conio.h>
using namespace std;
class Complex
{
float x;
float y;
public:
void input (float, float);
void show(Complex);
friend Complex Sum (Complex, Complex);
};
void Complex::input(float Real, float Imag)
{
x=Real;
y=Imag;
}
void Complex::show(Complex S)
{
cout<<"\n "<<S.x<<" +j "<<S.y<<endl;
}
Complex Sum (Complex A, Complex B)
{
Complex C;
C.x=A.x+B.x;
C.y=A.y+B.y;
return C;
}
34
Object Oriented Programming Lecture 3
2nd class Asst. Lecturer Omar Nowfal
void main()
{
Complex A1,A2,A3;
A1.input(3.1, 5.65);
A2.input(2.75, 1.2);
A3=Sum(A1, A2);
cout<<endl;
cout<<"A1= ";A1.show(A1);cout<<endl;
cout<<"A2 =";A2.show(A2);cout<<endl;
cout<<"A3 =";A2.show(A3);cout<<endl;
getch();
}
Operator Overloading:
Operator overloading is one of the most exciting features of object oriented programming.
It can transform complex, obscure program listings into intuitively obvious ones.
Statement like:
d3.add_distances(d1, d2);
Or
d3 = d1.add_distances(d2);
Can be changed to the much more readable
d3 = d1 + d2;
The term operator overloading refers to giving the normal C++ operators, such as +, *, <=, and
+=. Additional meanings when they are applied to user-defined data types like classes.
35
Object Oriented Programming Lecture 3
2nd class Asst. Lecturer Omar Nowfal
void OLE::indata(int A, int B, int C)
{
x=A;
y=B;
z=C;
36
Object Oriented Programming Lecture 3
2nd class Asst. Lecturer Omar Nowfal
void OLE::outdata()
{
cout<<" "<<x<<" "<<y<<" "<<z<<endl;
}
void OLE::operator++()
{
x++;
y++;
z++;
}
void OLE::operator‐()
{
x=‐x;
y=‐y;
z=‐z;
}
void main()
{
OLE T;
T.indata(10, ‐20, 30);
cout<<" T: ";
T.outdata();
++T;
cout<<"\n T++: ";
T.outdata();
‐T;
cout<<"\n ‐T ";
T.outdata();
getch();
}
37
Object Oriented Programming Lecture 3
2nd class Asst. Lecturer Omar Nowfal
Note that statement like S2 = ++S1 will not work because the function operator++() does
not return any value. It can work if the function is modified to return an object.
Overloading Binary Operators
Binary operators act on two operands. It’s include +, -, *, and /. The mechanism of
overloading binary operators is the same as that of unary operators.
Example: By using binary operator, write a program to add two complex numbers.
#include <iostream>
#include <conio.h>
using namespace std;
class Complex
{
float x;
float y;
public:
Complex(){x=0;y=0;}
Complex(float R, float I)
{x=R; y=I;}
void Input(float, float);
void Disp();
Complex operator+(Complex);
};
void Complex::Input(float R, float I)
{
x=R;
y=I;
}
38
Object Oriented Programming Lecture 3
2nd class Asst. Lecturer Omar Nowfal
void Complex::Disp()
{
cout<<" "<<x<<" +j "<<y<<endl;
}
Complex Complex::operator+(Complex A)
{
Complex T;
T.x=x+A.x;
T.y=y+A.y;
return T;
}
void main()
{
Complex C1(2.5,3.5), C2, C3;
C2.Input(1.6, 2.7);
C3=C1+C2;
cout<<" C1= ";
C1.Disp();
cout<<endl;
cout<<" C2= ";
C2.Disp();
cout<<endl;
cout<<" C3= ";
C3.Disp();
cout<<endl;
getch();
}
39
Object Oriented Programming Lecture 3
2nd class Asst. Lecturer Omar Nowfal
Note that in overloading of binary operators, the left-hand operand is used to invoke the operator
function and the right-hand operand is passed as an argument.
Pointers to Objects
Sometimes, when we are writing the program we don’t know how many objects we want
to create. In such case, we can use new operator to create objects at run time. The new operator
returns a pointer to an unnamed object Thus, object pointers are useful in creating objects at run
time. We can also use an object pointer to access the public members of an object.
The following example using NEW to create an object to the run time:
#include <iostream>
#include <conio.h>
using namespace std;
class Item
{
int code;
float price;
public:
void input(int, float);
void disp();
};
void Item::input(int C, float P)
{
code=C;
price=P;
}
void Item::disp()
{
cout<<" Code#"<<code<<"\t";
cout<<" Price= "<<price<<endl;
}
40
Object Oriented Programming Lecture 3
2nd class Asst. Lecturer Omar Nowfal
void main()
{
int N; //No of items
float Pr; //Price of item
cout<<"input the No of item ";
cin>>N;
Item *it=new Item[N]();
for (int i=0;i<N;i++)
{
cout<<"The Price of Item #"<<i+1<<"= ";
cin>>Pr;
(it+i)‐>input(i+1,Pr);
}
cout<<"\n Item Lists:"<<endl;
for (int i=0;i<N;i++)
(it+i)‐>disp();
getch();
}
41
Object Oriented Programming Lecture 3
2nd class Asst. Lecturer Omar Nowfal
this Pointer:
C++ uses a unique keyword called this to represent an object that invokes a member
function. this is a pointer that points to the object for which this function was called. The pointer
this is automatically passed to a member function when it is called. It acts as an implicit
argument to all the member functions.
Consider the following example:
class ABC
{
private:
int a;
public:
void setdata() { a=123; }
……
};
The statement
a = 123;
can also be expressed as
this‐>a = 123;
In operator overloading, we have implicitly used this pointer. When a binary operator is
overloaded using a member function, we pass only one argument to the function. The other
argument is implicitly passed using the pointer this.
In the previous complex number program, we had the binary operator+:
Complex Complex::operator+(Complex A)
{
Complex T;
T.x=x+A.x;
T.y=y+A.y;
return T;
}
42
Object Oriented Programming Lecture 3
2nd class Asst. Lecturer Omar Nowfal
Note that the left-hand operands x and y are passed implicitly using this pointer. In fact these
operands can be written as follows:
Complex Complex::operator+(Complex A)
{
Complex T;
T.x=this‐>x+A.x;
T.y=this‐>y+A.y;
return T;
}
One important application of the pointer this is to return the object it points to. The statement
return *this;
inside a member function will return the object that invoked the function. This statement is very
useful when we want to compare two or more objects inside a member function and return the
invoking object as a result as shown the program below.
#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
class Person
{
char Name[20];
float Age;
public:
Person(char *N, float A)
{
strcpy(Name,N);
Age=A;
}
Person Grator(Person);
void Disp();
};
43
Object Oriented Programming Lecture 3
2nd class Asst. Lecturer Omar Nowfal
Person Person::Grator(Person A)
{
if(A.Age>=Age)
return A;
else
return *this;
}
void Person::Disp()
{
cout<<"\n Name= "<<Name<<"\t";
cout<<" Age= "<<Age<<endl;
}
void main()
{
Person P1("John",37.5),
P2("Ahmed",55.0),
P3("Noor",40.25);
Person P=P1.Grator(P2);
P=P.Grator(P3);
cout<<"\n The Older Man is: "<<endl;
P.Disp();
getch();
}
44
Object Oriented Programming Lecture 3
2nd class Asst. Lecturer Omar Nowfal
Copy Constructor:
It is a member function which initializes an object using another object of the same class.
A copy constructor has the following general function prototype:
class_name (const class_name&obj);
{//body of constructor}
A copy constructor takes a reference to an object of the same class as it self as an argument. We
can not pass the argument by value to copy constructor. In the absence of a copy constructor,
the C++ compiler builds a default copy constructor for each class which is doing a member
wise copy between objects. The default copy constructor will not work well if the class contains
pointer data members.
What is the main difference between the copy constructor and the assignment operator?
The copy constructor creates a new object.
The assignment operator works on an already valid object.
Example:
#include <iostream>
#include <conio.h>
using namespace std;
class code
{
int Id1;
int Id2;
int Number1;
int Number2;
public:
code(){} //Constructor
code(int a, int b) // second type of constructor
{
Id1 = a;
Id2 = b;
}
code (code &a) //Copy Constructor
{
Id1=a.Id1;
45
Object Oriented Programming Lecture 3
2nd class Asst. Lecturer Omar Nowfal
Id2=a.Id2;
}
void Disp();
void Input(int, int);
};
void code::Disp()
{
cout<<"Id1 = "<<Id1<<"\t";
cout<<"Number1 = "<<Number1<<endl;
cout<<" Id2 = "<<Id2<<"\t";
cout<<"Number2 = "<<Number2<<endl;
cout<<endl;
}
void code::Input(int a, int b)
{
Number1=a;
Number2=b;
}
void main()
{
code A(100,200);
A.Input(50,75);
code B(A);
code C = A;
code D;
D=A;
cout<<"A: ";A.Disp();
cout<<"B: ";B.Disp();
cout<<"C: ";C.Disp();
cout<<"D: ";D.Disp();
getch();
}
46
Object Oriented Programming Lecture 3
2nd class Asst. Lecturer Omar Nowfal
Constant Object:
We’ve seen that we can apply constant to variables of basic types such as int to keep them
from being modified. In a similar way, we can apply constant to objects of classes. When an
object is declared as constant, you can’t modify it. It follows that you can use only constant
member functions with it, because they’re the only ones that guarantee not to modify it.
Example:
Example:
#include <iostream>
#include <conio.h>
using namespace std;
class Dist
{
int feet;
float inches;
public:
Dist(int ft, float in):feet(ft), inches(in)
{}
void show() const;
void input(int, float);
};
47
Object Oriented Programming Lecture 3
2nd class Asst. Lecturer Omar Nowfal
void Dist::show() const
{
cout<<"Feet = "<<feet<<endl;
cout<<"Inches = "<<inches<<endl;
}
void Dist::input(int ft, float in)
{
feet=ft;
inches=in;
}
void main()
{
const Dist D(100, 3.5);
D.show();
//D.input(200, 50.55);
getch();
}
1. When we decelerate the object as constant we must also set member function as constant.
2. Can not modify any data of the object which set as constant object.
3. The 3rd line in the main program if we active it the program not work because this
function is not constant member function and if we set a constant also not work because
this function try to modify data.
4. The output of this program is:
Feet = 100
Inches = 3.5
48