0% found this document useful (0 votes)
128 views

C++ Assignment-4

The document contains 4 C++ programs demonstrating operator overloading. The first program overloads the function "area()" to calculate the area of different shapes. The second program overloads operators like "-" and "*" to perform subtraction and multiplication of complex numbers. The third program overloads the "+" operator to concatenate two strings. The fourth program overloads the "=" operator to compare two strings and check if they are equal or not.

Uploaded by

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

C++ Assignment-4

The document contains 4 C++ programs demonstrating operator overloading. The first program overloads the function "area()" to calculate the area of different shapes. The second program overloads operators like "-" and "*" to perform subtraction and multiplication of complex numbers. The third program overloads the "+" operator to concatenate two strings. The fourth program overloads the "=" operator to compare two strings and check if they are equal or not.

Uploaded by

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

C++ ASSIGNMENT

1. C++ program to find area of square, rectangle, circle area and triangle
by using function overloading.
#include<iostream>
using namespace std;
int area(int);
int area(int,int);
float area(float);
float area(float,float);
int main()
{
int a,l,b;
float r,bs,ht;
cout<<"Enter side of a square:";
cin>>s;
cout<<"Enter length and breadth of rectangle:";
cin>>l>>"\t">>b;
cout<<"Enter radius of circle:";
cin>>r;
cout<<"Enter base and height of triangle:";
cin>>bs>>"\t"<<ht;
cout<<"Area of square is"<<area(a);
cout<<"\nArea of rectangle is "<<area(l,b);
cout<<"\nArea of circle is "<<area(r);
cout<<"\nArea of triangle is "<<area(bs,ht);
}

R.KAVIBHARATH 1901102 CSE-B


int area(int s)
{
return(s*s);
}
int area(int l,int b)
{
return(l*b);
}
float area(float r)
{
return(3.14*r*r);
}
float area(float bs,float ht)
{
return((bs*ht)/2);
}
Output:
Enter side of a square: 2
Enter length and breadth of rectangle: 3 6
Enter radius of circle: 3
Enter base and height of triangle: 4 4
Area of square is4
Area of rectangle is 18
Area of circle is 28.26
Area of triangle is 8

R.KAVIBHARATH 1901102 CSE-B


2. C++ program to find the subtraction and multiplication of two complex
numbers using operator overloading.
#include<iostream>
using namespace std;
class complex
{
int i,r;
public:
void read()
{
cout<<"\nEnter Real Part:";
cin>>r;
cout<<"Enter Imaginary Part:";
cin>>i;
}
void display()
{
cout<<"\n= "<<r<<"+"<<i<<"i";
}
complex operator-(complex a2)
{
complex a;
a.r=r-a2.r;
a.i=i-a2.i;
return a;
}
complex operator*(complex a2)
{

R.KAVIBHARATH 1901102 CSE-B


complex a;
a.r=(r*a2.r)-(i*a2.i);
a.i=(r*a2.i)+(i*a2.r);
return a;
}
};
int main()
{
complex a,b,c;
cout<<"\nEnter The First Complex Number:";
a.read();
a.display();
cout<<"\nEnter The Second Complex Number:";
b.read();
b.display();
c=b-a;
c.display();
c=a*b;
c.display();
break;
return 0;
}

R.KAVIBHARATH 1901102 CSE-B


Output:
Enter the first complex number:
Enter Real part: 3
Enter Imaginary part: 4
n=3+4i
Enter the second complex number:
Enter Real part: 4
Enter Imaginary part: 7
n=4+7i
=1+3i
=-16+37i

R.KAVIBHARATH 1901102 CSE-B


3. C++ program to overload ‘+’ operator to concatenate two strings.
#include <iostream>
using namespace std;
class String
{
public:
char s1[25], s2[25];
String(char str1[], char str2[])
{
strcpy(s1, str1);
strcpy(s2, str2);
}
void operator+()
{
cout << "\nConcatenation: " << strcat(s1, s2);
}
};
int main()
{
char str1[] = "Kavi";
char str2[] = "bharath";
String a1(str1, str2);
return 0;
}
Output:
Concatenation: Kavibharath

R.KAVIBHARATH 1901102 CSE-B


4. C++ program to compare (< or >) two strings using operator
overloading.
#include<iostream>
using namespace std;
class String
{
char str[20];
public:
void getdata()
{
gets(str);
}
int operator =(String s)
{
if(strcmp(str,s.str))
return 1;
else
return 0;
}
};
int main()
{
String s1,s2;
cout<<"Enter first string : ";
s1.getdata();
cout<<"\nEnter second string :";
s2.getdata();
if(s1=s2)

R.KAVIBHARATH 1901102 CSE-B


{
cout<<"\nStrigs are Equal\n";
}
else
{
cout<<"\nStrings are Not Equal\n";
}
return 0;
}
Output:
Enter first string: INDIA
Enter second string:india
Strings are Not Equal

R.KAVIBHARATH 1901102 CSE-B

You might also like