Thursday, 7 March 2013

constructer overloading (string)

#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
int len;
char *str;
public:
string()
{
len=0;
str=new char[len+1];
strcpy(str,"");
}
string(char *ch)
{
len=strlen(ch);
str=new char[len+1];
strcpy(str,ch);
}
void showstring ()
{
cout<<"\nstring is : "<<str;
}
void getlength()
{
cout<<"\nlength of given string : "<<len;
}
void getat(int pos)
{
cout<<"\ncharacter at given position is : "<<str[pos-1];
}
void setupper()
{
strupr(str);
cout<<"\nuppercase string is : "<<str;
}
void setlower()
{
strlwr(str);
cout<<"\nlower case string is : "<<str;
}
};
void main()
{
clrscr();
int x;
char z;
string s,s1("dynamic initialisation of string");
cout<<"\ndefault constructor: ";
s.getlength();
s.showstring();
cout<<"\nparameterized constructor: ";
s1.showstring();
s1.getlength();
cout<<"\nenter the position for finding character : ";cin>>x;
s1.getat(x);
s1.setupper();
s1.setlower();
getch();
}

output:

default constructor:
length of given string : 0
string is :
parameterized constructor:
string is : dynamic initialisation of string
length of given string : 32
enter the position for finding character : 6
character at given position is : i
uppercase string is : DYNAMIC INITIALISATION OF STRING
lower case string is : dynamic initialisation of string

No comments:

Post a Comment