0% found this document useful (0 votes)
28 views6 pages

Operator Overloading

opertaing overloading

Uploaded by

runebankai7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views6 pages

Operator Overloading

opertaing overloading

Uploaded by

runebankai7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

@2

#include<iostream>
#include<cstring>
using namespace std;
class String
{
private:
char *str;
public:
String(const char *s){
str=new char[strlen(s)+1];
strcpy(str,s);
}
~String()
{
delete[]str;
}
String operator!()
{
char *temp=new char[strlen(str)+1];
for(int i=0;i<strlen(str);i++)
{
if(islower(str[i]))
{
temp[i]=toupper(str[i]);
}
else if(isupper(str[i]))
{
temp[i]=tolower(str[i]);
}else{
temp[i]=str[i];
}
}
temp[strlen(str)]='\0';
return String(temp);
}
char operator[](int index)
{
if(index>=0 && index<strlen(str)){
return str[index];
}else{
return '\0';
}
}
void display(){
cout<<str<<endl;
}
};
int main()
{
String s1("Hello World");
cout<<"Original String: ";
s1.display();
String s2=!s1;
cout<<"String After Reversing Case: ";
s2.display();
cout<<"Character at index 7: "<<s1[7]<<endl;
return 0;
}
---------------------------------------

@5

#include<iostream>
#include<cstring>
using namespace std;
class String
{
private:
char *str;
public:
String(const char *s){
str=new char[strlen(s)+1];
strcpy(str,s);
}
~String()
{
delete[]str;
}
bool operator<(String & other)
{
return strlen(str)<strlen(other.str);
}
bool operator==(String & other)
{
return strcmp(str,other.str)==0;
}
String operator+(String & other)
{
char * temp=new char[strlen(other.str)+1];
strcpy(temp,str);
strcat(temp,other.str);
return String(temp);
}
void display(){
cout<<str<<endl;
}
};
int main()
{
String s1("Hi");
String s2=("World");
cout<<"Length of s1<s2: "<<(s1<s2)<<endl;
cout<<"S1==S2: "<<(s1==s2)<<endl;
String s3=s1+s2;
cout<<"Concatenated String: ";
s3.display();
return 0;
}
--------------------------------------------------

@6

#include<iostream>
using namespace std;
class Distance
{
private:
int kilometers;
int meters;
public:
Distance()
{
kilometers=0;
meters=0;
}
void acceptDistance()
{
cout<<"Enter kilometers: ";
cin>>kilometers;
cout<<"Enter meters: ";
cin>>meters;
}
void displayDistance()
{
cout<<"Distance: "<<"kilometers"<<kilometers <<"and" <<"meters"<<meters<<endl;
}
bool operator>(Distance & other){
if(kilometers>other.kilometers){
return true;
}else if(meters>other.meters){
return true;
}else{
return false;
}
}
};
int main()
{
Distance d1,d2;
cout<<"Enter Distance 1: "<<endl;
d1.acceptDistance();
cout<<"Enter Distance 2: "<<endl;
d2.acceptDistance();
cout<<"Distance 1: "<<endl;
d1.displayDistance();
cout<<"Distance 2: "<<endl;
d2.displayDistance();
if(d1>d2){
cout<<"Distance 1 is greater than Distance 2"<<endl;
}else{
cout<<"Distance 2 is greater than or equal to Distance 1"<<endl;
}
return 0;
}
4
--------------------------------------

friend function

@1

#include <iostream>
using namespace std;
class Rectangle2;
class Rectangle1
{
private:
float length;
float width;
public:
Rectangle1(float l, float w) : length(l), width(w) {}

float area()
const
{
return length * width;
}

friend void compareAreas(const Rectangle1& r1, const Rectangle2& r2);


};

class Rectangle2
{
private:
float length;
float width;

public:
Rectangle2(float l, float w) : length(l), width(w) {}

float area()
const
{
return length * width;
}

friend void compareAreas(const Rectangle1& r1, const Rectangle2& r2);


};

void compareAreas(const Rectangle1& r1, const Rectangle2& r2) {


float area1 = r1.area();
float area2 = r2.area();

cout << "Area of Rectangle1: " << area1 << endl;


cout << "Area of Rectangle2: " << area2 << endl;

if (area1 > area2)


cout << "Rectangle1 has a greater area than Rectangle2." <<endl;
else if (area1 < area2)
cout << "Rectangle2 has a greater area than Rectangle1." <<endl;
else
cout << "Both rectangles have equal area." <<endl;
}

int main() {
Rectangle1 r1(4, 5);
Rectangle2 r2(3, 6);

compareAreas(r1, r2);

return 0;
}
------------------------------

@2

#include<iostream>
using namespace std;
class Complex
{
private:
float real;
float imag;
public:
Complex(){
real = 0;
imag = 0;
}
Complex(float r, float i){
real = r;
imag = i;
}
friend Complex add(Complex c1, Complex c2);
void display(){
cout<<"("<<real<<","<<imag<<")"<<endl;
}
};
Complex add(Complex c1, Complex c2){
Complex temp;
temp.real = c1.real + c2.real;
temp.imag = c1.imag + c2.imag;
return temp;
}
int main(){
Complex c1(2, 4), c2(3, 5), c3;
c3 = add(c1, c2);
c1.display();
c2.display();
cout<<"And the sum is :"<<endl;
c3.display();
return 0;
}
----------------------------

@3

#include<iostream>
using namespace std;
class B;
class A
{
int x;
public:
void setdata(int i)
{
x=i;
}
friend void sub(A a,B b);
};
class B
{
int y;
public:
void setdata(int i)
{
y=i;
}
friend void sub(A a,B b);
};
void sub(A a,B b)
{
cout<<"Substraction is: "<<a.x-b.y;
}
int main()
{
A a;
B b;
a.setdata(15);
b.setdata(10);
sub(a,b);
return 0;
}

You might also like