Experiment 10

AIM: Programming using object-oriented programming (using data members, member functions, constructors, overloading & inheritance)

Task 1: Class “Employee” has data members: Emp_id, Emp_name and Emp_sal and this class uses a parameterized constructor to accept the details of 2 employees and display the results using the display () function.

#include <iostream>
using namespace std;
#include <cstring>

class Employee
{
int Emp_id;
char Emp_name[20];
float Emp_salary;
public:
Employee(int x, char y[], float z)
{
Emp_id = x;
Emp_salary = z;
strcpy(Emp_name, y);
}
void display()
{
cout << endl << "Employee ID: " << Emp_id << endl << "Employee Name: " << Emp_name << endl << "Emp Salary: " << Emp_salary << endl;
}
};

int main()
{
Employee e1(1, "Lorem", 50000);
Employee e2(2, "Ipsum", 75000);
e1.display();
e2.display();
return 0;
}

Task 2: Define class Complex with real and imaginary as data members, define default and parameterized constructor to initialize two complex numbers. Define add (Complex, Complex) member function to add two complex numbers and show( ) function to display both the complex numbers with their addition.

#include <iostream>
using namespace std;

class Complex
{
float real;
float imag;
public:
Complex()
{
real = 0.0;
imag = 0.0;
}
void read()
{
cout << "Enter the Real And Imaginary parts:" << endl;
cin >> real >> imag;
}
void add(Complex v1, Complex v2)
{
real = v1.real + v2.real;
imag = v1.imag + v2.imag;
}
void display()
{
cout << real << "+" << imag << "i";
}
};

int main()
{
Complex v1, v2, v3;
v1.read();
v2.read();
v3.add(v1, v2);
v3.display();
return 0;
}

Task 3: Rewrite above question to add two complex numbers using overloaded + operator.

#include <iostream>
using namespace std;

class Complex
{
float real;
float imag;
public:
Complex()
{
real = 0.0;
imag = 0.0;
}
void input()
{
cout << "Enter the complex number:";
cin >> real >> imag;
}
Complex operator+(const Complex obj)
{
Complex temp;
temp.real = real + obj.real;
temp.imag = imag + obj.imag;
return temp;
}
void output()
{
if (imag < 0)
cout << "Output Complex number: " << real << imag << "i";
else
cout << "Output Complex number: " << real << "+" << imag << "i";
}
};

int main()
{
Complex complex1, complex2, result;
cout << "Enter first number:" << endl;
complex1.input();
cout << "Enter second number:" << endl;
complex2.input();
result = complex1 + complex2;
result.output();
return 0;
}

Task 4: Create a class rectangle with (length, width), derive a class box with additional member (depth). In both the classes write parameterized constructor to initialize data member and area ( ) function to find area. Define main ( ) and create appropriate objects to call area ( ) function.

#include <iostream>
using namespace std;

class rectangle
{
public:
int length;
int width;
rectangle()
{
length = 1;
width = 1;
}
rectangle(int m, int n)
{
length = m;
width = n;
}
void area()
{
cout << "Area of rectangle will be: " << length * width << endl;
}
};

class box : public rectangle
{
public:
int depth;
box(int m, int n, int o)
{
length = m;
width = n;
depth = o;
}
void area()
{
cout << "Area for box will be: " << length * width * depth << endl;
}
};

int main()
{
rectangle r1(3, 2);
box r2(3, 2, 4);
r1.area();
r2.area();
return 0;
}

Task 5: Declare a class employee having data members as id, name and member function as getData( ) & display( ). Derive class manager from employee class. Manager class has data members as salary and member functions as getdata( ) & display( ). Again, derive class project manager from manager. Project manager class have data members as total experience, number of projects handled and member function as getdata() & display(). Write a program using multilevel inheritance to display all details of project manager.

#include <iostream>
using namespace std;

class employee
{
public:
int id;
char name[20];
void getdata()
{
cout << "Enter id:";
cin >> id;
cout << "Enter name:";
cin >> name;
}
void display()
{
cout << "For ID:" << id << endl;
cout << "Name is: " << name << endl;
}
};

class manager : public employee
{
public:
employee e1;
int salary;
void getdata()
{
e1.getdata();
cout << "Enter the salary:";
cin >> salary;
}
void display()
{
e1.display();
cout << "Salary is: " << salary << endl;
}
};

class projectmanager : public manager
{
public:
manager m1;
int total_exp;
int no_of_pro;
void getdata()
{
m1.getdata();
cout << "Enter Total Experience:";
cin >> total_exp;
cout << "Enter Number of Project(s):";
cin >> no_of_pro;
}
void display()
{
m1.display();
cout << "Total Experience is: " << total_exp << endl;
cout << "Number of Project(s): " << no_of_pro << endl;
}
};

int main()
{
projectmanager p1;
p1.getdata();
p1.display();
return 0;
}