You are on page 1of 12

The University Of Lahore

Department Of Computer Science


Assignment #01
Object Oriented Programming
Rashed Ashraf
Sap Id :70109140
Section: E
Task 1:
Write a program (using user define functions) which asks user to
select the one of the following operations while calling void Menu ()
function in main.
Welcome to the calculator program
Press p for Power
Press a for Addition
Press s for Subtraction
Press m for Multiplication
Press d for Division
Press e for Exit
After selecting one of the options programs ask two numbers from user and
do perform the task accordingly.
Code:
#include <iostream>
#include<cmath>
using namespace std;
char menu()
{
char chinput;
cout << "Enter the opration you want to execute:\nPress p for power: \nPress
a for addition: \n Press s for subtraction: \nPress m for multiplication: \nPress d for
division: \nPress e to exit:\n ";
cin >> chinput;
return chinput;
}
double power()
{
double base;
int n;
double Power;
cout << "Enter the base\n";
cin >> base;
cout << "Enter the power\n";
cin >> n;
Power = pow(base, n);
return Power;
}
double addition()
{
double num_1 = 0, num_2 = 0;
double add;
cout << "Enter the value\n";
cin >> num_2;
cout << "Enter the value\n";
cin >> num_1;
add = num_1 + num_2;
return add;
}
double subtraction()
{
double num_1 = 0, num_2 = 0;
double sub;
cout << "Enter the value\n";
cin >> num_2;
cout << "Enter the value\n";
cin >> num_1;
sub = num_1 - num_2;
return sub;
}
double multiplication()
{
double num_1 = 0, num_2 = 0;
double mul;
cout << "Enter the value\n";
cin >> num_2;
cout << "Enter the value\n";
cin >> num_1;
mul = num_1 * num_2;
return mul;
}
double division()
{
double num_1 = 0, num_2 = 0;
double div;
cout << "Enter the value\n";
cin >> num_2;
cout << "Enter the value\n";
cin >> num_1;
div = num_2 / num_1;
return div;
}

int main()
{

switch (menu())
{
case 'p':
{
cout << power();
break;
}
case 'a':
{
cout << addition();
break;
}
case 's':
{
cout << subtraction();
break;
}
case 'm':
{
cout << multiplication();
break;
}
case 'd':
{
cout << division();
break;
}
case 'e':
{
cout << "End";
break;
}
default:
cout << "invalid input";
}

Task#02:
A class named “Employee” holds information like employee code, name, gender,
year of joining. Write a program to create three objects of employee and enter
some data into it through setters. Make getters and setters for all employee
information. Then ask the user to enter current year. Display the names of those
employees whose tenure is 2 or more than 2 years according to the given current
year only using getters.
Code:
#include <iostream>
using namespace std;
class Employee
{
private:
static int employeecode;
string name;
char gender;
int joiningyear;
public:

void setname(string n)
{
name = n;
}
void setgender(char n)
{
gender = n;
}void setJoiningyear(int n)
{
joiningyear= n;
}

string getname()
{
return name ;
}
char getgender()
{
return gender;
}
int getJoiningyear()
{
return joiningyear;
}
void tenure(int currentyear )
{
int tenure;
tenure = currentyear - joiningyear;
if (tenure > 2)
{
cout<<getname()<<endl;

}
}

};

int main()
{
string name1, name2, name3;
char gender1, gender2, gender3;
int joiningyear1, joiningyear2, joiningyear3;
int currentYear;
cout << "Enter the current year: ";
cin >> currentYear;
cout << "Enter the name: ";
cin >> name1;
cout << "Enter the gender: ";
cin >> gender1;
cout << "Enter the joining year: ";
cin >> joiningyear1;
cout << "Enter the name: ";
cin >> name2;
cout << "Enter the gender: ";
cin >> gender2;
cout << "Enter the joining year: ";
cin >> joiningyear2;
cout << "Enter the name: ";
cin >> name3;
cout << "Enter the gender: ";
cin >> gender3;
cout << "Enter the joining year: ";
cin >> joiningyear3;

Employee E1,E2,E3;
E1.setname(name1);
E1.setgender(gender1);
E1.setJoiningyear(joiningyear1);
E1.tenure(currentYear);
E2.setname(name2);
E2.setgender(gender2);
E2.setJoiningyear(joiningyear2);
E2.tenure(currentYear);

E3.setname(name3);
E3.setgender(gender3);
E3.setJoiningyear(joiningyear3);
E3.tenure(currentYear);
}
Output:
Task 3:
A class named “Employee” holds information like employee code, name,gender,
year of joining. Write a program to create five hundred objects (Array of employee
objects )of employee and enter some data into it through setters. Make getters and
setters for all employee information. Then ask the user to enter current year.
Display the names of those employees whose tenure is 5 or more than 5 years
according to the given current year only using getters.
Note: employee code is automatically assigned to newly created object by calling
default constructor
Code:

#include <iostream>
using namespace std;
class Employee
{
private:
static int employeecode;
string name;
char gender;
int joiningyear;
public:
void setname()
{
cout << "Enter the name: ";
cin >> name;
}
void setgender()
{
cout << "Enter the gender: ";
cin >> gender;
}
void setJoiningyear()
{
cout << "Enter the joining year: ";
cin >> joiningyear;
}

string getname()
{
return name ;
}
int getEmployeecode()
{
return employeecode++;
}
char getgender()
{
return gender;
}
int getJoiningyear()
{
return joiningyear;
}
void tenure(int currentyear )
{
int tenure;
tenure = currentyear - joiningyear;
if (tenure >= 5)
{
cout<<getname()<<"Employee code: ";
cout << getEmployeecode() << endl;
}
}
};
int Employee:: employeecode =100;

int main()
{
string name, name2, name3;
char gender, gender2, gender3;
int joiningyear, joiningyear2, joiningyear3;
int currentYear;
cout << "Enter the current year: ";
cin >> currentYear;

Employee E1[500];
for (int i=0;i<500;i++)
{
E1[i].setname();
E1[i].setgender();
E1[i].setJoiningyear();
}
for (int i = 0; i < 500; i++)
{
E1[i].tenure(currentYear);
}
}
Output:

You might also like