0% found this document useful (0 votes)
5 views11 pages

Day 10

The document contains multiple C++ code snippets demonstrating the use of classes, operators, and functions. It includes examples of operator overloading for arithmetic operations, input/output operations, and comparisons for complex numbers and arrays. Additionally, it showcases the use of constructors, friend functions, and explicit type conversions.

Uploaded by

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

Day 10

The document contains multiple C++ code snippets demonstrating the use of classes, operators, and functions. It includes examples of operator overloading for arithmetic operations, input/output operations, and comparisons for complex numbers and arrays. Additionally, it showcases the use of constructors, friend functions, and explicit type conversions.

Uploaded by

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

Day 10

27 March 2025 08:24 AM

#include<iostream>
using namespace std;
class data{
int a;
public:
data(int a1=0)
{
a=a1;
}
data signchange()
{
data temp;
temp.a = a*-1;
return temp;
}
data operator-()

JECRC phase - 1 Page 1


data operator-()
{
data temp;
temp.a = a*-1;
return temp;
}
void output()
{
cout<<a<<endl;
}
};
int main()
{
data d1=10,d2;
d1.output();
// d2=d1.signchange();
d2 = -d1;
d2.output();
d1.output();
return 0;
}

#include<iostream>
using namespace std;
class data{
int a;
public:
data(int a1=0)
{
a=a1;
}
data increment()
{
data temp;
a = a+1;
temp.a = a;
return temp;
}
data operator ++() //pre
{
data temp;
a = a+1;
temp.a = a;
return temp;
}
data operator ++(int) //post
{
data temp;
temp.a = a;
a = a+1;
return temp;
}
void output()
{
cout<<a<<endl;
}
JECRC phase - 1 Page 2
}
};
int main()
{
data d1=10,d2;
d1.output(); //10
// d2=d1.increment();
d2 = ++d1;

d2.output(); //11
d1.output(); //11
d2 = d1++;
d2.output(); //11
d1.output(); //12
return 0;
}

#include<iostream>
using namespace std;
class complex{
public:
complex(int r=0, int i=0)
{
real = r;
imag = i;
}
void output()
{
if(imag >= 0)
cout<<real<<"+"<<imag<<"i"<<endl;
else
cout<<real<<imag<<"i"<<endl;
}
complex sum(complex obj)
{
complex temp;
temp.real = real + obj.real;
temp.imag = imag + obj.imag;
return temp;
}
complex operator +(complex &obj)
{
complex temp;
temp.real = real + obj.real;
temp.imag = imag + obj.imag;
return temp;
}
private:
int real,imag;
};
int main()
{
complex c1(5,7),c2(4,3);
c1.output();
c2.output();
complex c3;

JECRC phase - 1 Page 3


complex c1(5,7),c2(4,3);
c1.output();
c2.output();
complex c3;
// c3 = c1.sum(c2);
c3 = c1+c2;
c3.output();
return 0;
}

#include<iostream>
using namespace std;
class complex{
public:
complex(int r=0, int i=0)
{
real = r;
imag = i;
}
void input()
{
cout<<"Enter real:";
cin>>real;
cout<<"Enter Imag:";
cin>>imag;
}
void output()
{
if(imag >= 0)
cout<<real<<"+"<<imag<<"i"<<end
l;
else
cout<<real<<imag<<"i"<<endl;
}
complex sum(complex obj)
{
complex temp;
temp.real = real + obj.real;
temp.imag = imag + obj.imag;
return temp;
}
complex operator +(complex &obj)
{
complex temp;
temp.real = real + obj.real;
temp.imag = imag + obj.imag;
return temp;
}
complex operator*(complex &obj)
{
complex temp;
temp.real = real*obj.real - imag *
obj.imag;
temp.imag = real * obj.imag + imag
* obj.real;
return temp;
}
private:
int real,imag;
};
int main()

JECRC phase - 1 Page 4


int main()
{
complex c1,c2;
c1.input();
c2.input();
c1.output();
c2.output();
complex c3;
// c3 = c1.sum(c2);
c3 = c1+c2;
c3.output();
c3 = c1*c2+c3;
c3.output();
return 0;
}

#include<iostream>
using namespace std;
class complex{
public:
complex(int r=0, int i=0)
{
real = r;
imag = i;
}
void input()
{
cout<<"Enter real:";
cin>>real;
cout<<"Enter Imag:";
cin>>imag;
}
void output()
{
if(imag >= 0)
cout<<real<<"+"<<imag<<"i"<<endl;
else
cout<<real<<imag<<"i"<<endl;
}
complex sum(complex obj)
{
complex temp;

JECRC phase - 1 Page 5


complex temp;
temp.real = real + obj.real;
temp.imag = imag + obj.imag;
return temp;
}
complex operator +(complex &obj)
{
complex temp;
temp.real = real + obj.real;
temp.imag = imag + obj.imag;
return temp;
}
complex operator*(complex &obj)
{
complex temp;
temp.real = real*obj.real - imag * obj.imag;
temp.imag = real * obj.imag + imag * obj.real;
return temp;
}
bool operator>(complex &obj)
{
if(real > obj.real and imag >obj.imag)
return true;
else
return false;
}
bool operator<(complex &obj)
{
if(real < obj.real and imag < obj.imag)
return true;
else
return false;
}
bool operator==(complex &obj)
{
if(real == obj.real and imag == obj.imag)
return true;
else
return false;
}
bool operator >=(complex &obj)
{
if(real > obj.real && imag>obj.imag || real==obj.real && imag== obj.imag)
return true;
return false;
}
bool operator <=(complex &obj)
{
if(real < obj.real && imag < obj.imag || real==obj.real && imag== obj.imag)
return true;
return false;
}
private:
int real,imag;
};
int main()
{
complex c1,c2;
c1.input();
c2.input();
c1.output();
c2.output();
complex c3;

JECRC phase - 1 Page 6


complex c3;
// c3 = c1.sum(c2);
c3 = c1+c2;
c3.output();
c3 = c1*c2+c3;
c3.output();
if(c1>c2)
cout<<"C1 is greater";
else
cout<<"C1 is not greater";
return 0;
}

\ \

#include<iostream>
using namespace std;
class Array{
int arr[10];

JECRC phase - 1 Page 7


int arr[10];
int n;
public:
int& at(int ind)
{
return arr[ind];
}
void output()
{
for(int i=0;i<5;i++)
cout<<arr[i]<<" ";
cout<<endl;
}
};
int main(){
Array a1;
int &r = a1.at(0);
r=10;
int &r1 = a1.at(1);
r1=20;
int &r2 = a1.at(2);
r2=30;
int &r3 = a1.at(3);
r3=40;
int &r4 = a1.at(4);
r4=50;
a1.output();
return 0;
}

#include<iostream>
using namespace std;
class Array{
int arr[10];
int n;
public:
Array(int n1=0)
{
n=n1;
}
int& at(int ind)
{
return arr[ind];
}
int& operator[](int ind)
{
return arr[ind];
}
void output()
{
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
cout<<endl;
}
};
int main(){
int n;
cout<<"Enter number of elements:";
cin>>n;
Array a1=n;
for(int i=0;i<n;i++)
{
// int &r = a1.at(i);
cout<<"Enter value of "<<i+1<<" element:";
cin>>a1[i];
}
for(int i=0;i<n;i++)
{
cout<<a1[i]<<" ";
}
return 0;

JECRC phase - 1 Page 8


return 0;
}

#include<iostream>
using namespace std;
class Data
{
int a;
float b;
public:
Data()
{
}
explicit Data(int a1)
{
a=a1;
}
explicit Data(float b1)
{
b=b1;
}
explicit Data(int a1, float b1)
{
a=a1;
b=b1;
}
void output()
{
cout<<a<<'\t'<<b<<endl;
}
explicit operator int()
{
return a;
}
explicit operator float()
{
return b;
}
};
int main()
{
JECRC phase - 1 Page 9
{
Data d1=Data(10);
d1.output();
Data d2=Data(7.5f);
d2.output();
Data d3=Data(10,7.5);
d3.output();
int x = int(d1);
cout<<x<<endl;
float y = float(d2);
cout<<y<<endl;
}

#include<iostream>
using namespace std;
class Data
{
int a;
float b;
public:
Data(int a1=0, float b1=0)
{
a=a1;
b=b1;
}
friend istream& operator>>(istream& ,
Data&);
friend ostream& operator<<(ostream&,
Data&);
};
ostream& operator<<(ostream &out, Data &obj)
{
out<<obj.a<<'\t'<<obj.b<<endl;
return cout;
}
istream& operator>>(istream& in, Data &obj)
{
cout<<"Enter Int:";
cin>>obj.a;
cout<<"Enter Float:";
cin>>obj.b;
return cin;
}
int main()
{
Data d1;

JECRC phase - 1 Page 10


Data d1;
Data d2;
Data d3;
cin>>d1>>d2>>d3;
cout<<d1<<d2<<d3;
}

JECRC phase - 1 Page 11

You might also like