hits counter
  Create Free Blog | Random Blog »   Report Abuse | Login   

 

Constructor overloading

Parameterized constructor

Constructors that take arguments are called as parameterized constructor.

Constructor overloading:

*It is possible to overload a class’s constructor function. i.e., More than one constructors declared with in a single class.

*Reasons to overload constructor function:

1.To gain flexibility

2.To support arrays

3.To create copy constructor

Program:

#include<iostream.h>

class Time

{

int Hr,Min;

public:

Time() //No Argument Constructor(default)

{
Hr=0;

Min=0;

}

Time(int H,int M) //Two Argument(int type) Constructor

{

Hr=H;

Min=M;

}

Time (float T) //One Argument (float type) Constructor

{

Hr=(int) T:

Min=(int)((T-Hr)*100);

}
~Time() //Destructor

{

cout<<”Memory is released for the class Time”<<endl;

}

void display()

{

cout<<”Hr:”<<Hr<<”<<”:”<<”Min:”<<Min;

cout<<endl;

}

};

void main()

{

Time T; //Invoke the Default Constructor

T.display();

T=Time(4,12); //Invoke the 2 argument Constructor

Time T1(5.30); //Invoke the 1 argument Constructor

T1.display();

}

Share SocialTwist Tell-a-Friend 

Constructor and destructor functions

Constructor

Constructor is a special type member function,which execute automatically whenever an object is created.

Rules of creating Constructor:

*Name              :Constructor name is same as class name.

*Scope              :Public

*Return Type    :Nil

*Argument       :Optional

*Overloading   :Possible

Use:

*Variable Initialization

*Resource Allocation

*Type Conversion

Destructor

Destructor is a special type member function,which execute automatically whenever an object is destroyed.

Rules for creating Destructor:

*Name             :Destructor name is same as class name with prefix a tilled(~) character

*Scope             :public

*Return Type   :Nil

*Argument       :Not Possible

*Overloading   :Not Possible

Use

*Resource (Memory) Deallocation.

*Cleanup process

Function with default arguments

  • The default argument allows  you to give a default value to a parameter,when no corresponding argument is specified during the function is called.
  • Default argument is a shorthand form of function overloading.
  • Example:void Max(int a=30, int b=50);
  • This function can now be called three different ways.

1.Max(); //by default a is 30 and b is 50

2.Max(200); //a is 200 and b default to 30

3.Max(10,40); //a is 10 and b is 40

Program 1:

#include<iostream.h>

void Line(int N=60,char ch=’%'); //Fn with default arguments

void main()

{

cout<<”Calling One Argument Function”<<endl;

Line();

cout<<”Calling One Argument of type int Function”<<endl;

Line(20);

cout<<”Calling Two Argument Function”<<endl;

Line(80,’*');

}
void Line(int N,char ch)

{

for (int i=0; i<N; i++)

cout<<ch;

cout<<endl;

}

Note:we can avoid overloading a constructor by giving it one or more default arguments.

Program 2:

#include<iostream.h>

class sample

{

int X;

public:

sample(int n=0)

{X=n;}

int getx() {return X;}

};

void main()

{
sample s1(20);
//declare with initial value

sample s2; //declare without initialize

cout<<”S1 is:”<<s1.getx()<<endl;

cout<<”S2 is:”<<s2.getx()<<endl’;

}

Share SocialTwist Tell-a-Friend 

Function overloading

Same function name with

*Different number of arguments

*Different type of arguments

program 1:

#include<iostream.h>

void Line()

{

for(int i=0; i<50; i++)

cout<<”%”;

cout<<endl;

}

void Line(int N)

{
for (int i=0; i<N; i++)

cout<<”#”;

cout<<endl;

}
void Line(char ch)

{

for (int i=0; i<50; i++)

cout<<ch;

cout<<endl;

}

void Line (int N,char ch)

{

for(int i=0; i<N; i++)

cout<<ch;

cout<<endl;

}

void main()

{

cout<<”\nCalling No argument function”<<endl;                Line();

cout<<”Calling One argument of type int Function”<<endl; Line(20);

cout<<”Calling No argument of type char function”<<endl;  Line(’@');

cout<<”Calling Two argument function”<<endl;   Line(80,’*');

Program 2:

#include<iostream.h>

class FindMax

{

public: void MAX(int,int);

void MAX(float,float);

};

void FindMax::MAX(int A,int B)

{

if(A>B) cout<<”Biggest of Int is:”<<A<<endl;

else

cout<<”Biggest of Int is:”<<B<<endl;

}

void FindMax:: MAX(float A,float B)

{

if (A>B)cout<<”Biggest of float is :”<<A<<endl;

else

cout<<”Biggest of Float is:”<<B<<endl;

}

void main()

{

int X=20,Y=10;

float A=12.45f,B=10.42f;

FindMax F;

F.MAX(X,Y);

F.MAX(A,B);

}

Share SocialTwist Tell-a-Friend