Ex No.2a PROGRAM USING OPERATOR OVERLOADING
7-8-10
AIM:
To write a c++ program to perform arithmetic operations like addition, subraction, multiplication and division of two complex no.s using operator overloading,
ALGORTHIM:
Step 1:Start the program.
Step 2:In order to perform the basic arithmetic operation like addition, subraction, multiplication and division of two complex no.s using operator overloading function.
Step 3:Call the operator overloading function '+','-','*','/' operators to perform addition,subraction,multiplication and division of two complex no.s and display the results respectively.
Step 4:Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class complex
{
float x;float y;
public:
complex(){}
complex(float real,float imag)
{x=real;y=imag}
complex operator+(complex c);
complex operator-(complex c);
complex operator*(complex c2);
complex operator/(complex c2);
void display(void);
};
void complex::display(void)
{
cout<<x<<"+j"<<y;
};
complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return temp;
}
complex complex::operator-(complex c)
{
complex temp;
temp.x=x-c.x;
temp.y=y-c.y;
return temp;
}
complex complex::operator*(complex c2)
{
complex temp;
temp.x=x*2.x-y*c2.y;
temp.y=x*2.y+y*c2.x;
return temp;
}
complex complex::operator/(complex c2)
{
complex temp;
float d;
d=c2.x*c2.x+c2.y*c2.y;
temp.x=(x*c2.y+y*c2.y)/d;
temp.y=(y*c2.x-x*c2.y)/d;
return temp;
}
int main()
{
complex c1,c2,c3;
c1=complex(3.1,5.1);
c2=complex(6.2,7.1);
clrscr();
cout<<"c1:";c1.display();
cout<<"c2:";c2.display();
c3=c1+c2;cout<<"\n sum:"c3.display();
c3=c1-c2;cout<<"\n difference:";c3.display();
c3=c1*c2;cout<<"\n product:";c3.display();
c3=c1/c2;cout<<"\n division:";c3.display();
getch();
return 0;
}
RESULT:Thus a c++ program for performing arithmetic operation of two complex no. using operator overloading is executed and verified successfully.
Tuesday, October 5, 2010
PROGRAM USING FRIEND FUNCTION
Ex No.1b PROGRAM USING FRIEND FUNCTION
4-8-10
AIM:To add two complex numbers using c++.
ALGORTHIM:
Step 1: Start the program.
Step 2: The operation of sum is declared as friend function.
Step 3: Call the functions from the main functions.
Step 4: As the function has arguments, the value are given in main function itself.
Step 5: The sum of two complex no.is calculated.
Step 6: End the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>.
class complex
{
float x; float y;
public:
void input(float real,float image)
{
x=read;y=image;
}
friend complex sum(complex,complex);
void show(complex);
};
complex sum(complex c1,complex c2)
{
complex c3;
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.y;
return c3;
}
void complex::show(complex c)
{
cout<<c.x<<"+j"<<c.y;
}
int main()
{
clrscr();
complex A,B,C;
A.input(3.1,5.5);
B.input(2.7,1.2);
C=sum(A,B);
cout<<"\n A=";A.show(A);
cout<<"\n B=";B.show(B);
cout<<"\n C=";C.show(C);
getch();
return 0;
}
RESULT:
Thus the c++ program to find the sum of two complex number using friend function us executed and output is verified successfully.
4-8-10
AIM:To add two complex numbers using c++.
ALGORTHIM:
Step 1: Start the program.
Step 2: The operation of sum is declared as friend function.
Step 3: Call the functions from the main functions.
Step 4: As the function has arguments, the value are given in main function itself.
Step 5: The sum of two complex no.is calculated.
Step 6: End the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>.
class complex
{
float x; float y;
public:
void input(float real,float image)
{
x=read;y=image;
}
friend complex sum(complex,complex);
void show(complex);
};
complex sum(complex c1,complex c2)
{
complex c3;
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.y;
return c3;
}
void complex::show(complex c)
{
cout<<c.x<<"+j"<<c.y;
}
int main()
{
clrscr();
complex A,B,C;
A.input(3.1,5.5);
B.input(2.7,1.2);
C=sum(A,B);
cout<<"\n A=";A.show(A);
cout<<"\n B=";B.show(B);
cout<<"\n C=";C.show(C);
getch();
return 0;
}
RESULT:
Thus the c++ program to find the sum of two complex number using friend function us executed and output is verified successfully.
DISPLAYING OF STUDENT DETAILS USING STATIC FUNCTION
Ex N0.:1c DISPLAYING OF STUDENT DETAILS USING STATIC FUNCTIONS
4-8-10
AIM:
To display a no.of student details using static functions.
ALGORTHIM:
Step 1: Start the program.
Step 2: Get the no. of students from the user.
Step 3: Get the name and roll no.
Step 4: Enter the no. of subjects required.
Step 5: Get the marks.
Step 6: The total and average are displayed
Step 7: The step 3 to step 6 is repeated for the given no. of students.
Step 8: End the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class stud
{
int i,n,roll no.;
char a[20];
int marks[50],total;
float avg;
public:
int k;
int j;
static int count;
void getdetails()
{
cout<<”\nEnter the name and roll no,”;
cin>>a;
cin>>roll no.;
cout<<”\n Enter the number of subjects”;
cin>>n;
total=0;
for(i=0;i<n;i++)
{
cin>>marks[i];
cout<<endl;
total+=marks[i];
}
avg=(total n);
}
void display()
{
cout<<”STUDENT”<<cout<<endl;
cout<<”NAME”<<a<<endl<<”ROLL NO.”<<roll no<<endl<<endl;
for(i=0;i<n;i++)
{
cout<<”MARK”<<i+1<<”:”<<marks[i]<<endl;
}
cout<<”TOTAL”<<total;
cout<<”AVERAGE”<<avg;
}
static void showcount(void)
{
count++;
cout<<endl<<” STUDENT “<<count<<end;
}
};
int student::count;
void main()
{
clrscr();
student 5;
cout<<”\nEnter the number of students”;
cin>>s.j;
for(s.k=0;s.k<s.j;s.k++)
{
student::showcount();
s.getdetails();
s.display();
}
getch();
}
RESULT:
Thus a no. of student details are obtained using static members and the output is verified successfully.
4-8-10
AIM:
To display a no.of student details using static functions.
ALGORTHIM:
Step 1: Start the program.
Step 2: Get the no. of students from the user.
Step 3: Get the name and roll no.
Step 4: Enter the no. of subjects required.
Step 5: Get the marks.
Step 6: The total and average are displayed
Step 7: The step 3 to step 6 is repeated for the given no. of students.
Step 8: End the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class stud
{
int i,n,roll no.;
char a[20];
int marks[50],total;
float avg;
public:
int k;
int j;
static int count;
void getdetails()
{
cout<<”\nEnter the name and roll no,”;
cin>>a;
cin>>roll no.;
cout<<”\n Enter the number of subjects”;
cin>>n;
total=0;
for(i=0;i<n;i++)
{
cin>>marks[i];
cout<<endl;
total+=marks[i];
}
avg=(total n);
}
void display()
{
cout<<”STUDENT”<<cout<<endl;
cout<<”NAME”<<a<<endl<<”ROLL NO.”<<roll no<<endl<<endl;
for(i=0;i<n;i++)
{
cout<<”MARK”<<i+1<<”:”<<marks[i]<<endl;
}
cout<<”TOTAL”<<total;
cout<<”AVERAGE”<<avg;
}
static void showcount(void)
{
count++;
cout<<endl<<” STUDENT “<<count<<end;
}
};
int student::count;
void main()
{
clrscr();
student 5;
cout<<”\nEnter the number of students”;
cin>>s.j;
for(s.k=0;s.k<s.j;s.k++)
{
student::showcount();
s.getdetails();
s.display();
}
getch();
}
RESULT:
Thus a no. of student details are obtained using static members and the output is verified successfully.
Ex No.1 PROGRAM USING DEFAULT ARGUMENTS
28-7-10
AIM:
To write a program using c++ to find the amount for tax percentage using default arguments.
ALGORTHIM:
Step 1:Start the program.
Step 2:Read the amount to calculate the tax percent.
Step 3:Read the tax percent
Step 4: Calculate tax amount for default tax percent
Step 5:Display the value
Step 6:Calculate tax amount for given tax amount
Step 7:Display the value
Step 8:Stop the program
PROGRAM:
RESULT:
Thus the c++ program to calculate tax amount using default argument has been executed and the output is verified successfully.
28-7-10
AIM:
To write a program using c++ to find the amount for tax percentage using default arguments.
ALGORTHIM:
Step 1:Start the program.
Step 2:Read the amount to calculate the tax percent.
Step 3:Read the tax percent
Step 4: Calculate tax amount for default tax percent
Step 5:Display the value
Step 6:Calculate tax amount for given tax amount
Step 7:Display the value
Step 8:Stop the program
PROGRAM:
#include<iostream.h>
#include<conio.h>
class tax
{
int t;
public:
int cal(int a,int b=2)
{
t=(a*b)/100;
cout<<t<<endl;
return t;
}
};
void main();
{
int x,y;
clrscr();
tax s;
cout<<”\n Enter the amount to calculate tax amount <<”endl;
cin>>x;
cout<<”\nEnter the percentage of tax for the given amount “<<endl;
cin>>y;
cout<<”\nTax amount for default percent 2”<<s.cal(x)<<endl;
cout<<”\nTax amount for given amount of the given percent”<<s.cal(x,y);
getch();
}
RESULT:
Thus the c++ program to calculate tax amount using default argument has been executed and the output is verified successfully.
Wednesday, September 29, 2010
(OOPS)Sorting Algorithm Using Templates
EX NO. SORTING ALGORITHM USING TEMPLATES
AIM: To create a c++ program to implement sorting algorithm using templates.
ALGORITHM:
Step 1:Start the program.
Step 2: Define the functions for bubble sort,insertion sort,selection sort and merge sort.
Step 3:Call the respective functions to perform the sorting operations.
Step 4:Display the results.
Step 5:Stop the program.
PROGRAM:
AIM: To create a c++ program to implement sorting algorithm using templates.
ALGORITHM:
Step 1:Start the program.
Step 2: Define the functions for bubble sort,insertion sort,selection sort and merge sort.
Step 3:Call the respective functions to perform the sorting operations.
Step 4:Display the results.
Step 5:Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
template<class T>
class sort
{
public:
T a[20];
int n;
sort()
{
}
void display(T*a,int n)
{
for(int i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
cout<<endl;
}
void bubble(T*a,int n)
{
T tmp;
for(int i=0;i<n-i;i++)
{
for(intj=i+1;j<n;j++)
{
if(a[i]>a[j])
{
tmp=a[i];
a[i]=a[j];
a[j]=tmp;
}
}
}
}
void insertion(T*a,int n)
{
for(int p=1,p<n;p++)
{
T tmp=a[p];
for(int j=p;j>0&&tmp<a[j-1];j--)
a[j]=a[j-1]
a[j]=tmp;
}
}
void selection(T*a,int n)
{
T tmp,min;
for(int i=0,i<n-1;i++)
{
min=i;
for(int j=i+1;j<w,j++)
{
if(a[min]>a[j])
min=j;
}
tmp=a[min];
a[min]=a[i];
a[i]=tmp;
}
}
void merge(T*a,int n)
{
T.tmp array[20];
merge(a,tmparray,0,n-1);
}
void merg(T*a,T*tmparray,int left,int right)
{
if(left<right)
{
int centre=(left+right)/2
merge(a,tmparray,left,center);
merge(a,tmparray,centre+1,right);
{
merge1(a,tmparray,left,center+1,right);
}
}
void merge1(T*a,T*tmparray,int left pos,int right pos,int right end)
{
int left end=right pos-1;
int tmppos=leftpos;
int numelement=right end-left pos+1;
while(leftpos<=leftend&&rightpos<=right end)
{
if(a[leftpos]<=a[right pos])
tmparray[tmppos++]=a[leftpos++];
else
tmparray[tmpos++]=a[rightpos++];
}
while(leftpos<=leftend)
tmparray[tmppos++]=a[leftpos++];
while(rightpos<=rightend)
tmparray[tmppos++]=a[rightpos++];
for(int i=0;i<numelement;i++;rightend--)
a[rightend]=tmparray[rightend];
}
};
void main()
{
sort<int>s;
inth.a[20],choice,b[20],c[10],d[20],e[20],f[20],g[20],dec;
clrscr();
cout<<"Enter the no: of elements:"<<endl;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
b[i]=a[i];
c[i]=a[i];
d[i]=a[i];
e[i]=a[i];
}
do
{
clrscr();
cout<<"\t\t\t***MENU***\n";
cout<<"1.bubble\n2.insertion\n3.selection\n4.merge\n"<<endl;
cout<<"\t\t\tenter your choice:";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\n element before sorting:\n"<<endl;
s.display(a,n);
s.bubble(a,n);
cout<<"\n elements after sorting:\n"<<endl;
s.display(a,n);
getch();
break;
case 2:
cout<<"elements before sorting:\n"<<endl;
s.display(b,n);
s.insertion(b,n);
cout<<"\nelement after sorting:\n"<<endl;
display(b,n);
getch();
break;
case 3:
cout<<"\n element before sorting:\n"<<endl;
s.display(c,n);
s.selection(c,n);
cout<<"elements after sorting:\n"<<endl;
s.display(c,n);
getch();
break;
case 4:
cout<<"elements before sorting:\n"<<endl;
s.display(d,n);
s.mergesort(d,n);
cout<<"\n Element after sorting:\n"<<endl;
s.displsy(d,n);
getch();
break;
}
}
while(choice<4);
getch();
}
RESULT :
Thus the c++ program to perform various sorting operations using templates has been executed and the output is verified successfully.
RESULT :
Thus the c++ program to perform various sorting operations using templates has been executed and the output is verified successfully.
(OOPS)Overloding new & delete operator(longitude,latitude)
EX NO.4 PROGRAM TO OVERLOAD NEW AND DELETE OPERATOR
AIM: To write a c++ program to perform type conversion from basic to class type and class to basic type.
ALGORITHM:
Step 1: Start the program.
Step 2: A member function,parametrized constructor is created to initialize the value of latitude and longitude.
Step 3: Another member function is created to display latitude and longitude values.
Step 4: New operator overloading function is created to allocate the memory.
Step 5: Delete operator overloading function is created to deallocate the memory.
Step 6: A main function is created to implement new and delete operator overloading.
Step 7:End the program.
PROGRAM:
#include<iostream.h>
AIM: To write a c++ program to perform type conversion from basic to class type and class to basic type.
ALGORITHM:
Step 1: Start the program.
Step 2: A member function,parametrized constructor is created to initialize the value of latitude and longitude.
Step 3: Another member function is created to display latitude and longitude values.
Step 4: New operator overloading function is created to allocate the memory.
Step 5: Delete operator overloading function is created to deallocate the memory.
Step 6: A main function is created to implement new and delete operator overloading.
Step 7:End the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class position
{
float longitude,latitude;
public:
position()
{
};
position (float a, float b)
{
latitude=a;
longitude=b;
}
void *operator new(size_t size)
{
cout<<"\nin new";
return malloc(sizeof(size));
}
void display()
{
cout<<"\n\nlatitude:"<<latitude<<"\n\nlongitude:"<<longitude<<"\n\n";
}
void operator delete(void *p)
{
cout<<"\nin delete \n";
}
};
main()
{
clrscr();
position a(23.4,78.6);
position *b;
cout<<"A value:\n";
a.display();
cout<<"B value:\n";
b=new position;
b->display();
*b=a;
delete b;
getch();
}
RESULT:
Thus a c++ program for performing type conversion is written,executed and verified successfully.
RESULT:
Thus a c++ program for performing type conversion is written,executed and verified successfully.
(OOPS)Implementation of linked list using template
EX NO. IMPLEMENTATION OF LINKED LIST USING TEMPLATES
AIM:To write a c++ program to implement the linked list using templates.
ALGORITHM:
Step 1: Start the program.
Step 2: Create and declare the template class.
Step 3:To create,insert,delete,search and display the elements of linked list corresponding functions are created and called respectively.
Step 4:Define the main function
Step 5:Stop the program.
PROGRAM:
#include<iostream.h>
AIM:To write a c++ program to implement the linked list using templates.
ALGORITHM:
Step 1: Start the program.
Step 2: Create and declare the template class.
Step 3:To create,insert,delete,search and display the elements of linked list corresponding functions are created and called respectively.
Step 4:Define the main function
Step 5:Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
template<class T>
class list
{
T item;
public:
struct node
{
T data;
struct node*next;
};
struct node*start,*p,*new1[25];
void create();
void insert();
void delete1();
void search();
void display();
};
int pos,num,n,i,j;
template<class T>
void list<T>::create()
{
cout<<"\nenter the size";
cin>>n;
for(i=0;i<n;i++)
{
new1[i]=(struct node*)malloc(sizeof(struct node));
cout<<"enter the data";
cin>>item;
new1[i]->data=item;
new1[i]->next=NULL;
if(i==0)
{
p=new1[i];
start=new1[i];
}
else
{
p->next=new1[i];
p=new1[i];
}
}
}
template<class T>
void list<T>::insert()
{
p=start;
cout<<"enter the pos";
cin>>pos;
new1[pos]=(struct node*)malloc(sizeof(struct node));
cout<<"enter the data";
cin>>item;
new1[pos]->data=item;
new1[pos]->next=NULL;
for(i=1;i<pos;i++)
{
p=p->next;
}
new1[pos]->next=p->next;
p->next=new1[pos];
n=n+1;
}
template<class T>
void list<T>::delete1()
{
p=start;
cout<<"enter the position to be deleted";
cin>>pos;
for(i=0;i<pos-1;i++)
{
p=p->next;
}
p->next=p->next->next;
cout<<endl<<"node deleted";
n-=1;
}
template<class T>
void list<T>::search()
{
p=start;
pos=0;
cout<<"enterthe element to be search";
cin>>num;
for(i=0;i<=n;i++)
{
if(p->data==num)
{
pos=i;
cout<<"element found at"<<pos<<"pos";
break;
}
else{
p=p->next;
}
}
if(pos==0)
cout<<"\nelement not found";
}
template<class T>
void list<T>::display()
{
p=start;
for(i=1;i<=n;i++)
{
cout<<p->data<<"->";
p=p->next;
}
cout<<"NULL";
}
void main()
{
int c;
clrscr();
list<int>lis;
for(;;)
{
cout<<"\nmenu...\n1.create\n2.insert\n3.delete\n4.search\n5.display\n6.exit";
cout<<"\nenter the choice";
cin>>c;
switch(c)
{
case 1:
lis.create();
break;
case 2:
lis.insert();
break;
case 3:
lis.delete1();
break;
case 4:
lis.search();
break;
case 5:
lis.display();
break;
case 6:
exit(0);
break;
default:
cout<<"\nenter the correct choice";
}
}
}
RESULT:
Thus the program to implement linked list using templates is executed and the output is verified.
Thus the program to implement linked list using templates is executed and the output is verified.
(OOPS) Implementation of run time polymorphism
#include<iostream.h>
#include<conio.h>
class shapes
{
public:
virtual void area()
{
cout<<"\n program for calculating shapes";
}
};
class circle:public shapes
{
public:
int a,r;
void area()
{
cout<<"\n enter the radius";
cin>>r;
a=3.14*r*r;
cout<<"\n the area of the circle is"<<a;
}
};
class rectangle:public shapes
{
public:
float l,b,a;
void area()
{
cout<<"\n rectangle";
cout<<"\n enter the length";
cin>>l;
cout<<"\n enter the breadth";
cin>>b;
a=l*b;
cout<<"\n the area of the rectangle"<<a;
}
};
class triangle:public shapes
{
public:
float b,h,a;
void area()
{
cout<<"\n triangle";
cout<<"\n enter the breadth";
cin>>b;
cout<<"\n enter the height";
cin>>h;
a=0.5*b*h;
cout<<"\n area of the triangle is"<<a;
}
};
class square:public shapes
{
public:
float s,a;
void area()
{
cout<<"\n square";
cout<<"\n enter the side of the square";
cin>>s;
a=s*s;
cout<<"\n the area of the square is"<<a;
}
};
void main()
{
int ch=0;
shapes *p;
circle a1;
rectangle b1;
triangle c1;
square d1;
do{
cout<<"\n ....Menu....";
cout<<"\n1.circle";
cout<<"\n2.Rectangle";
cout<<"\n3.Triangle";
cout<<"\n4.Square";
cout<<"\n Enter your choice";
cin>>ch;
switch(ch)
{
case 1:
p=&a1;
p->area();
break;
case 2:
p=&b1;
p->area();
break;
case 3:
p=&c1;
p->area();
break;
case 4:
p=&d1;
p->area();
break;
}
}while(ch!=5);
getch();
}
#include<conio.h>
class shapes
{
public:
virtual void area()
{
cout<<"\n program for calculating shapes";
}
};
class circle:public shapes
{
public:
int a,r;
void area()
{
cout<<"\n enter the radius";
cin>>r;
a=3.14*r*r;
cout<<"\n the area of the circle is"<<a;
}
};
class rectangle:public shapes
{
public:
float l,b,a;
void area()
{
cout<<"\n rectangle";
cout<<"\n enter the length";
cin>>l;
cout<<"\n enter the breadth";
cin>>b;
a=l*b;
cout<<"\n the area of the rectangle"<<a;
}
};
class triangle:public shapes
{
public:
float b,h,a;
void area()
{
cout<<"\n triangle";
cout<<"\n enter the breadth";
cin>>b;
cout<<"\n enter the height";
cin>>h;
a=0.5*b*h;
cout<<"\n area of the triangle is"<<a;
}
};
class square:public shapes
{
public:
float s,a;
void area()
{
cout<<"\n square";
cout<<"\n enter the side of the square";
cin>>s;
a=s*s;
cout<<"\n the area of the square is"<<a;
}
};
void main()
{
int ch=0;
shapes *p;
circle a1;
rectangle b1;
triangle c1;
square d1;
do{
cout<<"\n ....Menu....";
cout<<"\n1.circle";
cout<<"\n2.Rectangle";
cout<<"\n3.Triangle";
cout<<"\n4.Square";
cout<<"\n Enter your choice";
cin>>ch;
switch(ch)
{
case 1:
p=&a1;
p->area();
break;
case 2:
p=&b1;
p->area();
break;
case 3:
p=&c1;
p->area();
break;
case 4:
p=&d1;
p->area();
break;
}
}while(ch!=5);
getch();
}
(OOPS)PROGRAM USING STATIC MEMBERS
EX.NO: PROGRAM USING STATIC MEMBERS
DATE:
AIM:
To write a c++ program to display the students details using static members.
ALGORITHM:
STEP1: Start the process.
STEP2: Get the number of students.
STEP3: Get the name, roll no, marks of students.
STEP4:
STEP5: The total and averages are calculated.
STEP6: Step3 to step5 is repeated for the given number of students.
STEP7: Display the details.
STEP8: Stop the process.
(OOPS)PROGRAM USING CONSTRUCTOR, DESTRUCTOR AND ASSIGNMENT OPERATOR OVERLOADING
EX.NO: PROGRAM USING CONSTRUCTOR, DESTRUCTOR
DATE: AND ASSIGNMENT OPERATOR OVERLOADING
AIM:
To create two strings and concatinate using default constructors,parametrized constructors,copy constructor,destructor and overloaded assignment operator.
ALGORITHM:
STEP1: Start the process.
STEP2: A default constructor is created for string length allocation.
STEP3: A parameterized constructor is created in which parameter name is copied to another variable.
STEP4: An assignment operator is overloaded to copy one object name or value
to another.
STEP5: A copy constructor is created which refers to the address of another
Object value.
STEP6: Concatenation is performed with two strings.
STEP7: Many objects are created and made to access the different constructors and the overloaded assignment operator
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
class string
{
char *name;
int length;
public:
string()
{
length=0;
name=new char[length +1]
}
string(char*s)
{
length=strlen(s);
name= new char[length +1];
strcpy(name,s);
}
string(string &s)
{
length=strlen(s.name);
name=new char(length +1);
strcpy(name,s.name);
}
void display(void)
{
cout<<"\n"<<name;
}
void operator=(string s);
string operator+(string s);
~string()
{
free(name);
}
};
void string::operator=(string s)
{
length=strlen(s.name);
name=new char[length +1];
strcpy(name,s.name);
}
string string::operator+(string s)
{
string tm(strcat(name,s.name));
return tm;
}
void main()
{
clrscr();
char*first="JOSEPH";
string name1(first),name2("LOUIS"),name3(name 2);
string name 4=name1,name 5=name1+name2;
cout<<"\n in copy constructor";
name3.display ();
cout<<"\n\n assignment operator";
name4.display();
cout<<"\n Concatinated name";
name5.display();
getch();
}
RESULT:
Thus the two strings are created and concatinated using default constructor,parametrized constructor,copy constructor,overloading of assignment operator, destructor and output is verified succesfully.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
class string
{
char *name;
int length;
public:
string()
{
length=0;
name=new char[length +1]
}
string(char*s)
{
length=strlen(s);
name= new char[length +1];
strcpy(name,s);
}
string(string &s)
{
length=strlen(s.name);
name=new char(length +1);
strcpy(name,s.name);
}
void display(void)
{
cout<<"\n"<<name;
}
void operator=(string s);
string operator+(string s);
~string()
{
free(name);
}
};
void string::operator=(string s)
{
length=strlen(s.name);
name=new char[length +1];
strcpy(name,s.name);
}
string string::operator+(string s)
{
string tm(strcat(name,s.name));
return tm;
}
void main()
{
clrscr();
char*first="JOSEPH";
string name1(first),name2("LOUIS"),name3(name 2);
string name 4=name1,name 5=name1+name2;
cout<<"\n in copy constructor";
name3.display ();
cout<<"\n\n assignment operator";
name4.display();
cout<<"\n Concatinated name";
name5.display();
getch();
}
RESULT:
Thus the two strings are created and concatinated using default constructor,parametrized constructor,copy constructor,overloading of assignment operator, destructor and output is verified succesfully.