Lab Report 4 Oop Lab 230609

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Lab task 1:

Q1. Create a class Complex where you define complex numbers with real and imaginary
parts.

 The real and imaginary parts should be private members.


 Write a friend function add() that takes two Complex objects and returns a new
Complex object representing the sum.

 Additionally, implement a friend function multiply() for multiplying two complex


numbers.

 Make sure to include functionalities to display the complex number, which will
require accessing the private members.

Input:

Code:
#include<iostream>
using namespace std;
class complex
{private:
int r,i;
public:
void input()
{cout<<"Enter the real part ";
cin>>r;
cout<<"Enter the imaginary
part ";
cin>>i;
}
void disp()
{cout<<r<<"+"<<i<<"i"<<endl;
}
friend complex add(complex &a,complex &b);
friend complex multiply(complex &a,complex &b);
};
complex add(complex &a,complex &b)
{complex result;
result.r=a.r+b.r;
result.i=a.i+b.i;
return result;
}
complex multiply(complex &a,complex &b)
{complex result;
result.r=a.r*b.r;
result.i=a.i*b.i;
return result;
}
int main()
{complex r1,r2,r3;
cout<<"Enter the first complex number"<<endl;
r1.input();
cout<<"Enter the second complex number"<<endl;
r2.input();
r3=add(r1,r2);
r3.disp();
}

Output:

Lab task 2:
Q2. Define a class Employee with private members like name, position, and
salary.
 Create a friend class HR that has functions to modify and report these
private attributes, for example, changing the salary, promoting the
employee (changing the position), or generating a salary slip.
 Demonstrate the interaction between objects of these two classes.

Input:
Code:
#include<iostream>
using namespace std;
class Employee
{private:
string name;
string position;
double salary;
public:
friend class HR;
};
class HR
{public:
void input(Employee &n)
{cout<<"Enter the name of new Employee";
getline(cin,n.name);
cout<<"Enter the position";
getline(cin,n.position);
cout<<"Enter the salary";
cin>>n.salary;
}
void setPosition(Employee &n)
{cin.ignore(200,'\n');
cout<<"Enter new position";
getline(cin,n.position);
}
void setSalary(Employee &n)
{cout<<"Enter new salary";
cin>>n.salary;
}
void salaryslip(Employee &n)
{cout<<" SALARY SLIP"<<endl;
cout<<"Name: "<<n.name;
cout<<"\nPosition: "<<n.position;
cout<<"\nSalary: "<<n.salary;
}
};
int main()
{Employee E1;
HR h1;
h1.input(E1);
h1.setSalary(E1);
h1.setPosition(E1);
h1.salaryslip(E1);

Output:

You might also like