You are on page 1of 6

STRING OPERATOR

#include<iostream.h>
#include<conio.h>
#include<string.h>
class stringop
{
char str[20];
public:
stringop()
{
strcpy(str,”\0”);
}
void read()
{
cout<<”Enter the name:”;
cin>>str;
}
void display()
{
cout<<”/n”;<<str;
}
stringop operator +(stringop str2);
};
stringop stringop::operator +(stringop str2)
{
stringop str3;
int I,j;
int l1=strlen(str);
int l2=strlen(str2.str);
for(i=0;i<l1;i++)
str3.str[i]=str[i];
for(j=l1,i=0;j<l1+l2;j++,i++)
str3.str[j]=str2.str[i];
str3.str[j]=’\0’;
return(str3);
}
void main()
{
clrscr();
stringop s1,s2,s3;
s1.read();
s2.read();
s3=s1+s2;
s1.display();
s2.display();
s3.display();
getch();
}
Output:
Enter the name: pakkiri
enter the name: samy
pakkiri
samy
pakkirisamy
Operaor overloading with friend funtion:
#includ<iostream.h>
#include<conio.h>
class interest
{
int year;
float amount;
float rate;
public:
interst()
{
year=5;amount=0.0;rate=10.0;
}
void read()
{
cout<<"\n Enter the Amount:";
cin>>amout;
}
void display()
{
cou<<"\nYear:"<<year;
cou<<"\nAmount:";
cout<<"\nRate:";
cou<<"\nTotal interest:"<<(year*amount*rate)/100;
}
friend interest operator +(interest,interest);
};
interest operator +(interest1,interest2)
{
interest t;
t.amount=i1.amount+i2.amount;
return(t);
}
void main()
{
clrscr();
interest i1,i2,i3;
i1.read();
i2.read();
i3.read();
i3=i1+i2;
i1.display();
i2.display();
cout<<"\n\nAdding Two interst";
i3.display();
getch();
}
Output:

Enter the Amount:1000

Enter the Amount:2000

Year:5
Amount:1000
Rate:10
Total Interest:500

Year:5
Amount:1000
Rate:10
Total Interest:500

Adding Two Interest


Year:5
Amount:3000
Rate:10
Total Interest:1500
Mulilevel Inheritance

#includde<iostream.h>
#inlude<conio.h>
class vehicle
{
public:
char reg_no,power[20];
int model,n_gear;
void read()
{
cout<<"Enter the Reg_no";
cin>>reg_no;
cout<<"Enter the Model no:";
cin>>model;
}
void print()
{
cout<<"\n\n\tVehicle Details";
cou<<"\n----------------------";
cou<<"\nRegistration no:"<<reg_no;
cout<<"\nModel:";
}
};
class two_wheeler:public vechicle
{
public:
void read1()
{
read();
cout<<"Enter Number of gear:":
cin>>n_gear;
cout<<"Enter the power:";
cin>>power;
}
void print1()
{
print();
cout<<"no.of Gear:";<<n_gear;
cout<<"\nPower:";
}
};
class scooter:public two_wheeler
{
char manu[20],owner[20];
public:
void read2()
{
read1();
cout<<"Enter the manufacturer Name:";
cin>>manu;
cout<<"Enter the owner name:";
cin>>owner;
}
void print2()
{
print1();
cout<<"Enter the manufacturer Name:"<<manu;
cout<<"Enter the owner name:"<<owner;
}
};

void main()
{
clrscr();
scooters;
s.read2();
s.print2();
getch();
}

OUTPUT:

Enter the Reg_no:TN_0a


Enter Model no:2009
Enter the no.of gear: 4
Enter the manufacture name :hero honda
Enter the Owner name : pakkiri

VEHICLE DETAILS
---------------------------------------
The Reg_no:TN_0a
Model no:2009
The no.of gear: 4
The manufacture name :hero honda
The Owner name : pakkiri
---------------------------------------

You might also like