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

Lab C++

The document contains code for two C++ programs. The first program creates a Complex class with real and imaginary parts. It overloads the ADD function to add either an integer and complex number, or two complex numbers together. The main function tests the overloaded ADD functions and outputs the results. The second program overloads the == operator to compare two strings. It reads in two strings, calls the overloaded == operator to compare them, and prints if they are equal or not equal.

Uploaded by

Jagadish Narayan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Lab C++

The document contains code for two C++ programs. The first program creates a Complex class with real and imaginary parts. It overloads the ADD function to add either an integer and complex number, or two complex numbers together. The main function tests the overloaded ADD functions and outputs the results. The second program overloads the == operator to compare two strings. It reads in two strings, calls the overloaded == operator to compare them, and prints if they are equal or not equal.

Uploaded by

Jagadish Narayan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PART A

4.Write a C++ program to create a class called COMPLEX and implement the following
overloading functions ADD that return a COMPLEX number.
i. ADD(a , s2) - where a is an integer (real part) and s2 is a complex number.
ii. ADD(s1, s2) - where s1 and s2 are complex numbers.

#include <iostream.h>
#include<conio.h>
#include<math.h>
class complex
{
private:
int real,imag;
public:
void input()
{
cout<<"Enter real and imaginary part"<<'\n';
cin>>real>>imag;
}
void output()
{
cout<<real<<"+"<<"i"<<imag<<'\n';
}
friend complex add(int ,complex);
friend complex add(complex,complex);
};
complex add(int a,complex s2)
{
complex temp;
temp.real=s2.real+a;
temp.imag=s2.imag;
return temp;
}
complex add(complex s1,complex s2)
{
complex s3;
s3.real=s1.real+s2.real;
s3.imag=s1.imag+s2.imag;
return s3;
}
void main()
{
complex s1,s2,sum1,sum2;
int a;
clrscr();
s1.input();
s2.input();
cout<<"First Complex number is : "<<'\n';
s1.output();
cout<<"Second Complex number is :"<<'\n';
s2.output();
cout<<"Enter the value of a ";
cin>>a;
sum1=add(a,s2);
sum2=add(s1,s2);
cout<<"Output of integer and complex number is : "<<'\n';
sum1.output();
cout<<"Output of complex and complex number is : "<<'\n';
sum2.output();
getch();
}

5.Write a C++ Program to compare two strings by overloading = = operator.

#include <iostream.h>
#include<conio.h>
#include<string.h>
class string
{
char str[10];
public:
void read()
{
cout<<"Enter a string"<<endl;
cin>>str;
}
int operator==(string x)
{
if(strcmp(str,x.str)==0)
return 1;
else
return 0;
}
};
void main()
{
string s1,s2;
clrscr();
s1.read();
s2.read();
if(s1==s2)
cout<<"Strings are equal"<<endl;
else
cout<<"Strings are not equal"<<endl;
getch();
}

You might also like