0% found this document useful (0 votes)
0 views4 pages

Jai C++ Record 4th EXERCISE

The document contains three C++ programming exercises focusing on function overloading, unary operator overloading, and binary operator overloading. Each exercise includes an aim, algorithm, program code, and a result confirming successful execution. The programs demonstrate string and number concatenation, unary operator functionality, and addition of complex numbers using operator overloading.

Uploaded by

mithula.2305062
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)
0 views4 pages

Jai C++ Record 4th EXERCISE

The document contains three C++ programming exercises focusing on function overloading, unary operator overloading, and binary operator overloading. Each exercise includes an aim, algorithm, program code, and a result confirming successful execution. The programs demonstrate string and number concatenation, unary operator functionality, and addition of complex numbers using operator overloading.

Uploaded by

mithula.2305062
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/ 4

Ex.

No: 4a)
CONCATINATING OF A STRING AND NUMBER USING FUNCTION
OVERLOADING

AIM:

To write a C++ program for Function overloading.

ALGORITHM:
Step 1: Start the program.
Step 2: Define two functions add( ) and assign different number of parameters to
each.
Step 3: Overload the functions by calling both the functions in the main( ) method by
passing required number of parameters.
Step 4: Display the output.
Step 5: End the program.
PROGRAM:
#include <iostream>
#include <string>
using namespace std;
string add(string str1, string str2) {
return str1 + str2;
}
int add(int x, int y) {
return x + y;
}
int main() {
string str1 = "Adhnan ";
string str2 = "Jeff!";
int num1 = 03, num2 = 26;
string resultStr = add(str1, str2);
int resultNum = add(num1, num2);
cout << "Concatenated String: " << resultStr << endl;
cout << "Sum of Numbers: " << resultNum << endl;
return 0;
}

OUTPUT:

RESULT:
Thus the above program has been successfully executed and the output was verified.
13
Ex. No: 4b)

UNARY OPERATOR OVERLOADING FOR NOT OPERATOR

AIM:

To write a C++ program for Unary operator overloading.

ALGORITHM:
Step 1: Start the program.
Step 2: Define a class Num with n as a private data member.
Step 3: Define method dispNum( ) and exp( ) to overload the ‘n’ operator.
Step 4: Create an object for the class Num to access the class members.
Step 5: Display the output.
Step 6: End the program.
PROGRAM:
#include <iostream>
using namespace std;
class Num{
private:
int n;
public:
void getNum(int x) {
n=x;
}
void dispNum() {
cout<<"The value of n is: 4";
}
void exp(int n) {
cout<<"-"<<n;
}
};
int main(){
Num num;
num.getNum(2);
num.dispNum();
cout<<endl;
return 0;
}
OUTPUT:

RESULT:
Thus the above program has been successfully executed and the output was verified.
14
Ex. No: 4c)
ADDITION OF COMPLEX NUMBERS USING BINARY OPERATOR
OVERLOADING

AIM:

To write a C++ program for Binary operator overloading.

ALGORITHM:
Step 1: Start the program.
Step 2: Create a class Complex.
Step 3: Create constructor to initialize the variables.
Step 4: Define a member function display( ) to display the complex number result.
Step 5: Use operator overloading to perform addition and subtraction between
complex numbers.
Step 6: In the main method, create object for the class.
Step 7: Call the overloading function using the operator to be overloaded.
Step 8: Display the output.
Step 9: Stop the program.
PROGRAM:
#include <iostream>
using namespace std;
class Complex{
private:
double real;
double img;
public:
Complex(double r=0, double i=0){
real =r;
img =i;
}
Complex operator+(Complex &other){
return Complex(real+other.real, img+other.img);
}
Complex operator-(Complex &other){
return Complex(real-other.real, img-other.img);
}
void display(){
cout<<real<<"+"<<img<<"i"<<endl;
}
};
int main(){
Complex num1(8.0, 9.0);
Complex num2(26.0, 41.0);
Complex sum = num1+num2;
Complex diff = num1-num2;
cout<<"Num1 : "<<endl;
num1.display();

15
cout<<"Num 2: "<<endl;
num2.display();
cout<<"Sum: "<<endl;
sum.display();
cout<<"Diff: "<<endl;
diff.display();
return 0;}
OUTPUT:

RESULT:
Thus the above program has been successfully executed and the output was verified.

16

You might also like