A constructor is a special member funct whose task is to
initialize the objects of its class. It is special because its name is same as
the class name. The constructor is
invoked whenever an object of its associated class is created. It is
called constructor because it constructs the values of data member of the
calls. A constructor is declared and defined as follows
class A
{
int a;
public:
A ( );
{
a=100;
}
int A::setvalue()
{
return a;
}
void main()
{
Ax;
cout<<x.setvalue()
getch();
}
The constructor function
has some special characteristic
A. They should be declared in public section.
B. They are invoked automatically when the objects are
created.
C. They do not have return type, not even void.
Therefore they cannot return values.
D. They cannot be inherited through derived class can
call the base class constructors.
E. Like other C++ functions, they can have default
arguments.
#include<iostream.h>
#include<conio.h>
class A
{
int a,b;
public:
A ( );
int sum( );
};
A::A( )
{
a=10;
b=20;
}
int A::sum ( )
{
return (a+b);
}
void main ( )
{
Ax;
cout<< “\n sum =” <<x.sum(
);
getch( );
}
Types of constructors
1) Parameterless as default constructor:- A constructor that accepts no parameter is called parameterless or default constructor. The default constructor for class A is A::A( ). If no such constructor is defined then the compiler supplied default constructor.Therefore a statement such as A x; invoked the default constructor of the compiler to create the object x.
2) Parameterized constructor
1) Parameterless as default constructor:- A constructor that accepts no parameter is called parameterless or default constructor. The default constructor for class A is A::A( ). If no such constructor is defined then the compiler supplied default constructor.Therefore a statement such as A x; invoked the default constructor of the compiler to create the object x.
class A
{
int a;
public:
A ( );
{
a=100;
}
int A::setvalue()
{
return a;
}
void main()
{
Ax;
cout<<x.setvalue()
getch();
}2) Parameterized constructor
A constructor that can
take arguments is called parameterized constructor.
Example: sum of two
numbers
class A
{
int a,b;
public:
A (int i, int j );
int sum( );
};
A::A(int i, int j ) //
constructor definitation
{
a=i;
b=j;
}
int A::sum ( )
{
return (a+b);
}
void main ( )
{
int p,q;
cout<< “The value for p and
q\n”;
cin>>p>>q;
A x(p,q); // object implicit call
cout<<”sum”<<x.sum(
);
getch( );
}
Write a program
to calculate factorial of a given number using parameterized constructor
#include<iostream.h>
#include<conio.h>
class A
{
int n;
public:
A (int i);
int fact( );
};
A::A(int i) //
{
n=i;
}
int A::fact ( )
{
int f=1;
if(n==0)
return (f)
else
while(n>0)
{
f=f*n;
n--
}
return (f);
}
void main ( )
{
int p;
cout<< “The value of p ”;
cin>>p;
A(p);
cout<< “fact”<<r.fact(
);
getch( );
}
Write a program
to swap 2 values using parameterized constructor.
#include<iostream.h>
#include<conio.h>
class swap
{
public:
swap (int &a,int &b)
{
int c( ); //local variable
c=a;
a=b;
b=c;
};
void main ( )
{
clrscr ( );
int x,y;
cout<< “Enter 2 No.”;
cin>>x>>y;
swap obj(x,y);
cout<< “\n x=”<<x<<
“\t y=”y;
getch( );
}
3)
Copy constructor
A copy constructor
takes a reference to an object of the same class as itself as an argument.
#include<iostream.h>
#include<conio.h>
class A
{
int x;
public:
A (int i)
{
x=i;
}
A (A&q)
{
x=q.x;
}
void display ( )
{
cout<<x;
}
};
void main ( )
{
clrscr ( )
A ob1(100);
A ob2(ob1);
ob1.display( );
Ob2.display( );
getch( );
}
Destructor:
A destructor is used to
destroy the objects that have been created by a constructor. Like a
constructor, a destructor is a member function whose name is same as the class
name but is preceeded by tilde (~).
The destructor for a
class A cab be defined as
~A ( )
A destructor never
takes any argument nor does it return any value. It will be invoked implicitly
by the compiler upon exit from a program to clean up storage that is no longer
accessible.
Function overloading
Overloading refers to
the use of the same things for different purposes. Function overloading means
to use the same function name to create function that performs variety of
different tasks. This is known as function overloading in object oriented programming.
The function would
perform different operations depending in the arguments list in the function
call. The correct function to be invoked is determined by checking the number
and type of the argument but not on the function type ( i.e. return type ). E.g.
int sum
A function call first
matches the prototype having the same number and type of argument and then
calls the appropriate function for execution.
#include<iostream.h>
#include<conio.h>
class over
{
int x,y,z;
public:
over (int a,int b, int c);
int sum (int i, int j, int k);
float sum (float p, float q);
int sum( );
};
over::over(int a, int b, int c)
{
x=a;
y=b;
z=c;
}
int over::sum(int I, int j, int
k)
{
x=i;
y=j;
z=k;
return (x+y+z);
}
float over::sum (float p, float
q)
{
return (p+q);
}
int over::sum( )
{
return (x+y+z);
}
void main ( )
{
clrscr ( )
over obj(5,10,15);
cout<<obj.sum();
cout<<obj.sum(5,7,2);
cout<<obj.sum(3.5,7.2);
getch( );
}
Operator overloading:
C++ permits us to add
two variables of user defined types with the same syntax that is applied to the
basic type. This means that C++ has the ability to provide the operators with a
special meaning for the datatypes. The mechanism of giving such special meaning
s to an operator is known as operator overloading.
The process of
overloading involves the following steps:
1) Create a class that defines the datatypes that is to
be used in the overloading operators.
2) Declare the operator function operator op( ) in the
public part if the class. It may be either a member function or a friend
function.
3) Defines the operator function to implement the
required operation.
#include<iostream.h>
#include<conio.h>
class complex
{
float x;
float y;
public:
complex (float real, float imag)
// constructor
{
x=real;
y=imag;
}
complex operator+ (complex c);
//overloading member function
void display( );
complex( )
{
}
};
complex complex::operator+(complex
c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return (temp);
}
void complex::display( )
{
cout<< x<< “+j”<<y;
void main ( )
{
clrscr ( )
complex c1(2.5,3.5),
c2(5.7,7.5),c3;
c3=c1+c2;
c1.display( );
c2.display( );
c3.display( );
getch( );
}
We know that a member function
can be invoked only by an object of the same class. Here, the object c1 takes
the responsibility of invoking the member function and c2 plays role of an
arguments that is passes to the function. The above invocation statement (c3=c1+c2)
is equivalent to c3=c1.opeator+(c2); Therefore in the operator +( ) function
the data member of c1 are accessed directly and the data member of c2 ( that is
passed as an argument) are accessed using the dot(.) operator. Thus both the
objects are available to the function.
#include<iostream.h>
#include<conio.h>
class complex
{
float x;
float y;
public:
complex (int real, float imag) //
constructor
{
x=real;
y=imag;
}
complex operator* (complex c);
//overloading member function
void display( );
complex( )
{
}
};
complex complex::operator*(complex
c)
{
complex temp;
temp.x=x*c.x;
temp.y=y*c.y;
return (temp);
}
void complex::display( )
{
cout<< “\n” << x<< “+j”<<y;
void main ( )
{
clrscr ( )
complex c1(2,3.5), c2(4,7.5),c3;
c3=c1*c2;
c1.display( );
c2.display( );
c3.display( );
getch( );
}
Overloading binary
operator using friend
A friend function may
be used in the place of member functions for overloading a binary operators,
the only difference is that a friend required two arguments to be explicitly
passed to it while a member function requires only one. The complex number
program using a friend operator function as follows
#include<iostream.h>
#include<conio.h>
class complex
{
float x;
float y;
public:
complex ( )
{
}
Complex (float real, float imag)
{
x=real;
y=imag;
}
friend complex operator+ (complex
a, complex b);
void display( );
};
complex operator+(complex a, complex
b)
{
complex temp;
temp.x=a.x+b.x;
temp.y=a.y=b.y;
return (temp);
}
void complex::display( )
{
cout<< “\n” << x<< “+j”<<y;
void main ( )
{
clrscr ( )
complex c1(2.5,1.5), c2(3.5.4.5),c3;
c3=operator+(c1,c2);
c1.display( );
c2.display( );
c3.display( );
getch( );
}
Rules for overloading
operators
1) Only existing operators can be overloaded new
operators cannot be created.
2) The overloaded operators must have at least one
operand that is of user defined type.
3) We cannot change the basic meaning of an operator
i.e. we cannot redefine the plus (+) operator to subtract one value from
another.
4) There are some operators that cannot be overloaded
Operator
|
Name
of the operator
|
Sizeof
|
Size
of operator
|
::
|
Scope
resolution operators
|
?
|
Conditional
operator
|
5) Binary operators overloaded through a member
function take one explicit argument and those which are overloaded through a
friend function take two explicit argument.
6) When using binary operators overloaded through a member
function, the left hand operand must be an object of the relevant class.
No comments:
Post a Comment