0% found this document useful (0 votes)
1 views27 pages

Functionoverloadingfor Thirdsemester Students

functionoverloadingfor thirdsemester students

Uploaded by

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

Functionoverloadingfor Thirdsemester Students

functionoverloadingfor thirdsemester students

Uploaded by

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

Write a c++ program to

input an 2D array and


display it
#include <iostream> void display() {
using namespace std; cout << "\nMatrix is:\n";
for (int i = 0; i < rows; i++) {
class Matrix { for (int j = 0; j < cols; j++) {
private: cout << arr[i][j] << " ";
int arr[10][10]; // 2D array }
int rows, cols; cout << endl;
}
public: }
void input() { };
cout << "Enter number of rows:
"; int main() {
cin >> rows; Matrix m1; // Object of class Matrix
cout << "Enter number of
columns: "; m1.input();
cin >> cols; m1.display();

cout << "Enter elements of the return 0;


matrix:\n"; }
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> arr[i][j];
}
Without using operator overloading
#include <iostream> // Function to display complex number
using namespace std; void display() {
cout << real << " + " << imag
class Complex { << "i" << endl;
private: }
float real, imag;

public: // Function to add two complex


// Function to input complex numbers
number Complex add(Complex c2) {
void input() { Complex result;
cout << "Enter real part: "; result.real = real + c2.real;
cin >> real; result.imag = imag + c2.imag;
cout << "Enter imaginary return result;
part: "; }
cin >> imag; };
int main() {
Complex c1, c2, sum;

cout << "Enter first complex number:\n";


c1.input();

cout << "Enter second complex number:\n";


c2.input();

// Use the add() function instead of '+' operator


sum = c1.add(c2);

cout << "\nSum of two complex numbers: ";


sum.display();

return 0;
}
#include <iostream> int main() {
using namespace std; Complex c1, c2, sum;

class Complex { cout << "Enter first complex


private: number:\n";
float real, imag; c1.input();

public: cout << "Enter second complex


void input() { number:\n";
cout << "Enter real and c2.input();
imaginary parts: ";
cin >> real >> imag; sum = c1 + c2;
}
void display() { cout << "\nSum of two complex
cout << real << " + " << numbers: ";
imag << "i" << endl; sum.display();
}
}; return 0;
OPERATOR OVERLOADING

Operator overloading allows you to redefine


how operators (+, -, *, ==, etc.) work for user-
defined types (classes/objects).
Example 1: Overload + Operator for Adding Two
Objects
#include <iostream> // Overload '+' operator
using namespace std; Complex operator + (Complex c2)
{
class Complex { Complex temp;
private: temp.real = real + c2.real;
float real, imag; temp.imag = imag + c2.imag;
return temp;
public: }
void input() {
cout << "Enter real and imaginary void display() {
parts: "; cout << real << " + " << imag
cin >> real >> imag; << "i" << endl;
} }
};
int main() {
Complex c1, c2, sum;

cout << "Enter first complex number:\n";


c1.input();

cout << "Enter second complex number:\


n";
c2.input();

sum = c1 + c2; // Uses overloaded '+'


operator

cout << "\nSum of two complex numbers:


";
sum.display();
Overloading - operator
#include <iostream>
using namespace std; void display() {
cout << "Value = " << value << endl;
class Number { }
private:
int value; // Overload '-' operator
Number operator - (Number obj) {
public: Number result;
void setValue(int v) result.value = value - obj.value; //
{ subtract values
value = v; return result;
} }
};
int main() {
Number n1, n2, diff;

n1.setValue(50);
n2.setValue(20);

diff = n1 - n2; // Calls n1.operator-


(n2)
diff.display(); // Output: Value = 30

return 0;
}
== operator overloading with multiple parameters
#include <iostream>
#include <string>
using namespace std;
void display() {
cout << "Name: " << name
class Student {
<< ", Age: " << age
private:
<< ", Marks: " << marks <<
string name;
endl;
int age;
}
int marks;
// Overload '==' operator
public:
bool operator == (Student s) {
void setData(string n, int a,
return (name == s.name && age
int m)
== s.age && marks == s.marks);
{
}
name = n;
};
age = a;
int main() {
Student s1, s2;
s1.setData("Alice", 20, 85);
s2.setData("Alice", 20, 85);
s1.display();
s2.display();

if (s1 == s2) { // Uses overloaded '=='


cout << "Both students are equal." << endl;
}

else {
cout << "Students are not equal." << endl;
}
return 0;
}
Program No 4
Write a C++ program to create a class called
MATRIX using a two dimensional array of
integers.Implement the following operations by
overloading the operator == which checks the
compatibility of the two matrices to be added and
subtracted. Perform the addition and subtraction
by overloading the operators + and - respectively.
Display the results by overloading operator <<.
• ALGORITHM:
• Start
• Define the MATRIX class:
• Include a private two-dimensional array to store matrix elements.
• Include public methods for input, output, and operator overloading.
• Overload the == operator:
• Check if two matrices have the same dimensions.
• Get the data for the no. of rows, no. of columns and
matrix values from the user
• Overload the + operator:
• Add corresponding elements of two matrices.
• Returns the resulting matrix.
• Overload the - operator:
• Subtract corresponding elements of two matrices.
• Returns the resulting matrix.
• Overload the << operator:
• Display the matrix elements in a formatted manner.
• Main Function
• Create two matrix M1 and M2.
• Check if the matrices are compatible for addition and
subtraction using operator ==
• If compatible, perform addition and subtraction using operator
+ and operator – respectively
• Display the results using <<.
• End
Program No 9
Design, develop and execute a program in C++ based
on the following requirements: An EMPLOYEE class
containing data members & members functions: i) Data
members: employee number (an integer), Employee_
Name (a string of characters), Basic_ Salary (in
integer), All_ Allowances (an integer), Net_Salary (an
integer). (ii) Member functions: To read the data of an
employee, to calculate Net_Salary & to print the values
of all the data members. (All_Allowances = 123% of
Basic, Income Tax (IT) =30% of gross salary (=basic_
Salary_All_Allowances_IT).
How the Salary Calculation
Works:
All Allowances: 123% of basic salary
For Basic Salary = 50000, All Allowances = 50000 * 1.23 = 61500.

Gross Salary: Basic Salary + All Allowances


Gross Salary = 50000 + 61500 = 111500.

Income Tax: 30% of Gross Salary


Income Tax = 111500 * 0.30 = 33450.

Net Salary: Gross Salary - Income Tax


Net Salary = 111500 - 33450 = 78050.
• ALGORITHM:
• Start the program.
• Create the Employee class:
• Attributes:
• employeeNumber: Read an integer for the employee number.
• employeeName: Read a string for the employee's name.
• basicSalary: Read an integer for the basic salary of the
employee.
• allAllowances: To store the calculated allowances (123% of the
basic salary).
• netSalary: To store the final net salary (after deduction of
income tax).
• Member Functions:
• readData():
• Input employee number.
• Input employee name.
• Input basic salary.

• calculateNetSalary():
• Compute allAllowances as 123% of the basicSalary.
• Compute grossSalary as the sum of basicSalary and allAllowances.
• Compute incomeTax as 30% of grossSalary.
• Compute netSalary as grossSalary - incomeTax.
• printData():
• Display the employee number.
• Display the employee name.
• Display the basic salary.
• Display the calculated all allowances.
• Display the calculated net salary.
• In main():
• Create an object emp of the Employee class.
• Call emp.readData() to read the employee's input.
• Call emp.calculateNetSalary() to compute the net
salary.
• Call emp.printData() to display the employee details.
• End the program.
#include <iostream> public:
using namespace std; // Function to read data of an employee
void readData()
class Employee
{ {
private: cout << "Enter Employee Number: ";
cin >> employeeNumber;
int employeeNumber;
cout << "Enter Employee Name: ";
char employeeName[20]; cin>>employeeName;
int basicSalary; cout << "Enter Basic Salary: ";
int allAllowances; cin >> basicSalary;
int netSalary; }
//Funtion to calculate net salary

void calculateNetSalary()
{
allAllowances = static_cast<int>(basicSalary * 1.23);
int grossSalary = basicSalary + allAllowances;
int incomeTax = static_cast<int>(grossSalary * 0.30);
netSalary = grossSalary - incomeTax;
}
// Function to print the employee details

void printData() const


{
cout << "\nEmployee Details:\n";
cout << "Employee Number: " << employeeNumber << endl;
cout << "Employee Name: " << employeeName << endl;
cout << "Basic Salary: " << basicSalary << endl;
cout << "All Allowances: " << allAllowances << endl;
cout << "Net Salary: " << netSalary << endl;
}
};
// Main Program
int main()
{
Employee emp;
emp.readData();
emp.calculateNetSalary();
emp.printData();
return 0;
}

You might also like