Latest Activity In Study Groups

Join Your Study Groups

VU Past Papers, MCQs and More

We non-commercial site working hard since 2009 to facilitate learning Read More. We can't keep up without your support. Donate.

Assignment No. 02
Semester: Spring 2015

CS304: Object Oriented Programming

Total Marks: 20

 

Due Date: 08/06/2015

 

Instructions:

 

Please read the following instructions carefully before submitting assignment. It should be clear that your assignment will not get any credit if:

 

  • The assignment is submitted after due date.
  • The submitted assignment does not open or file is corrupt.
  • Assignment is copied(partial or full) from any source (websites, forums, students, etc)

 

Note: You have to upload only .cpp file. Assignment in any other format (extension) will not be accepted and will be awarded with zero marks. For example, if you submit code in .doc (Microsoft Word Document) or .txt files or .exe file, no reward will be given in any case.

 

 

Objective:

 

The objective of this assignment is to provide hands on experience of:

 

  • Classes and objects
  • Constructors and destructors
  • Setter and Getter functions
  • Static data member
  • Member initializer list
  • Copy constructor and deep copy

 

 

Guidelines:

 

  • Code should be properly indented and well commented.
  • Follow C/C++ guide lines while writing variable names, function names etc
  • Use only dev-C++ for this assignment.

 

 

 

For any query about the assignment, contact at cs304@vu.edu.pk

 

 

 

 ASSIGNMENT

Problem Statement:  

  

In this assignment you will be creating a class named Employee.

 

Employee class has the following data members:

  • Employee ID.
  • Employee Name
  • Department
  • Employee Salary
  • Static data member which will hold the value of total no. of employees

 

 

Employee class must have the following member functions:

 

Function

Description

Employee()

Default constructor for Employee class, which will set employee id  to 0, employee name to NULL, employee department to NULL and salary to 0.0

Employee(int, char *, char *, float);

It will take employee id, employee name, department and salary as arguments and initialize their values using member initializer list

Employee(Employee &); 

Copy constructor which will initialize one object with another object using deep copy

Setter functions

You have to define setter function for each data member which will take a value as an argument and set their values

Getter functions

You have to define getter function for each data member which will return the values of their data members

calcNetPay()

This function will calculate the net pay of an employee based on his salary

display()

This function will display the values of all data members of class

~Employee()

Destructor of Employee class which will decrement the value of static data member of class

 

 

calcNetPay():  This function will calculate net pay of an employee based on his salary as:

If salary is less than and equal to 10,000 then tax is 0.

If salary is greater than 10,000 and less than equal to 20,000 then tax is 5%.

If salary is greater than 20,000 and less than equal to 30,000 then tax is 7%.

The tax on the salary greater than 30,000 is 10%.

Note: On creation of each object, the value of static data member should be incremented.

 

 

 

 

 

Within main() function, create an array of 5 objects of class Employee. Second object should be initialized with the first object.

 

Now, use for loop to call calcNetPay() and display() functions for each object.

 

You can create objects as follow:

Employee emp1(5, "Ahmad", "Accounts", 20000 );

Employee emp2(emp1) ;

Employee emp3(10, "Ayesha", "Accounts", 25000);

Employee emp4(3, "Hassan", "Administration", 10000);

Employee emp5(6,  "Arsalan", "Administration", 35000);

 

Sample output of the program:

 

 

 

 

 

 

 

 

 

 

Views: 7566

Replies to This Discussion

#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
class Employee
{
private:
int employeeID;
char *employeeName;
char *department;
float employeeSalary;
static int NoOfEmployees;
public:
Employee() {
employeeID = 0;
employeeName = NULL;
department = NULL;
employeeSalary = 0.0;
}
Employee(int empId, char *empName, char *depat, float empSalary) {
employeeID = empId;
employeeName = new char[strlen(empName)+1];
strcpy(employeeName,empName);
department = new char[strlen(depat)+1];
strcpy(department, depat);
employeeSalary = empSalary;
NoOfEmployees++; }
void setEmpID(int empId) {
employeeID = empId; }
int getEmpID() {
return employeeID; }
void setEmpName(int empId) {
employeeID = empId; }
char *getEmpName() {
return employeeName; }
void setDepat(char *depat) {
department = depat; }
char *getDepat() {
return department; }
void setEmpSalary(int empSalary) {
employeeSalary = empSalary; }
float getEmpSalary() {
return employeeSalary; }
float calcNetPay() {
if ( employeeSalary >= 0 && employeeSalary <= 10000 ) {
employeeSalary = employeeSalary;
} else if ( employeeSalary > 10000 && employeeSalary <= 20000) {
employeeSalary = employeeSalary - (0.05 * employeeSalary);
}
else if ( employeeSalary > 20000 && employeeSalary <= 30000) {
employeeSalary = employeeSalary - (0.07 * employeeSalary);
} else {
employeeSalary = employeeSalary - (0.1 * employeeSalary);
}
}
void display() {
cout getEmpID() "\t\t" getEmpName() "\t\t" getDepat() "\t\t" getEmpSalary() endl; }
~Employee() {
delete[] employeeName;
delete[] department;
NoOfEmployees--;
}
};
int Employee::NoOfEmployees = 0;
int main() {
Employee EmployeeSet[4];
Employee emp1(5, "Ahmad", "Accounts", 20000 );
Employee emp2(emp1);
Employee emp3(10, "Ayesha", "Accounts", 25000);
Employee emp4(3, "Hassan", "Administration", 10000);
Employee emp5(6, "Arsalan", "Administration", 35000);
cout "ID" "\t\tName" "\t\tDepartment" "\t\tNet Pay" endl;
EmployeeSet[0] = emp1;
EmployeeSet[1] = emp2;
EmployeeSet[2] = emp3;
EmployeeSet[3] = emp4;
EmployeeSet[4] = emp5;
for(int i = 0; i < 5; ++i){
EmployeeSet[i].calcNetPay();
}
for(int i = 0; i < 5; ++i){
EmployeeSet[i].display();
}
system("pause");
}

main() using for loop.... the tricky part is only this so i only share it .....

#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
class Employee
{
private:
int employeeID;
char *employeeName;
char *department;
float employeeSalary;
static int NoOfEmployees;
public:
Employee() {
employeeID = 0;
employeeName = NULL;
department = NULL;
employeeSalary = 0.0; 
}
Employee(int empId, char *empName, char *depot, float empSalary) {
employeeID = empId;
employeeName = new char[strlen(empName)+1];
strcpy(employeeName,empName);
department = new char[strlen(depot)+1];
strcpy(department, depot);
employeeSalary = empSalary;
NoOfEmployees++; }
void setEmpID(int empId) {
employeeID = empId; }
int getEmpID() {
return employeeID; }
void setEmpName(int empId) {
employeeID = empId; }
char *getEmpName() {
return employeeName; }
void setDepot(char *depot) {
department = depot; }
char *getDepot() {
return department; }
void setEmpSalary(int empSalary) {
employeeSalary = empSalary; }
float getEmpSalary() {
return employeeSalary; }
float calcNetPay() {
if ( employeeSalary >= 0 && employeeSalary <= 10000 ) {
employeeSalary = employeeSalary;
} else if ( employeeSalary > 10000 && employeeSalary <= 20000) {
employeeSalary = employeeSalary - (0.05 * employeeSalary);
}
else if ( employeeSalary > 20000 && employeeSalary <= 30000) {
employeeSalary = employeeSalary - (0.07 * employeeSalary);
} else {
employeeSalary = employeeSalary - (0.1 * employeeSalary);
}
}
void display() {
cout getEmpID() "\t\t" getEmpName() "\t\t" getDepot() "\t\t" getEmpSalary() endl; }
~Employee() {
delete[] employeeName;
delete[] department;
NoOfEmployees--; 
}
};
int Employee::NoOfEmployees = 0;
int main() {
Employee EmployeeSet[4];
Employee emp1(5, "Ahmad", "Accounts", 20000 );
Employee emp2(emp1);
Employee emp3(10, "Ayesha", "Accounts", 25000);
Employee emp4(3, "Hassan", "Administration", 10000);
Employee emp5(6, "Arsalan", "Administration", 35000);
cout "ID" "\t\tName" "\t\tDepartment" "\t\tNet Pay" endl;
EmployeeSet[0] = emp1;
EmployeeSet[1] = emp2;
EmployeeSet[2] = emp3;
EmployeeSet[3] = emp4;
EmployeeSet[4] = emp5;
for(int i = 0; i < 5; ++i){
EmployeeSet[i].calcNetPay();
}
for(int i = 0; i < 5; ++i){
EmployeeSet[i].display();
}
system("pause");
}

#include <iostream.h>
#include <string.h>
using namespace std;

class Employee{ // start from the class definition 
private: // to be initiate the private data members this mean nobody see the members
int employeeID;
char* employeeName;
char* department;
float employeeSalary;
static int totalNoOfEmployees;

public: // anyone can accessor function
Employee(){
employeeID = 0;
employeeName = NULL;
department = NULL;
employeeSalary = 0.0;
} // default constructor end


//start the parameter
Employee(int _employeeID, char const * _employeeName, char const * _department, float _employeeSalary): employeeID(0), employeeName(NULL), department(NULL), employeeSalary(0.0){
employeeID =_employeeID;
employeeName = new char[strlen(_employeeName)+1];
strcpy( employeeName,_employeeName);
department = new char[strlen(_department)+1];
strcpy(department,_department);
employeeSalary = _employeeSalary;
++totalNoOfEmployees;
} // end of parameterized constructor



//Start for Copy Constructor
Employee(Employee& emp){
employeeID = emp.employeeID;
employeeName = new char[strlen(emp.employeeName)+1];
strcpy(employeeName,emp.employeeName);
department = new char[strlen(emp.department)+1];
strcpy(department,emp.department);
employeeSalary = emp.employeeSalary;
++totalNoOfEmployees;
} // end of copy constructor


//start for void setEmployeeID function
void setEmployeeID(int _employeeID){
if(_employeeID > 0){
employeeID = _employeeID;
}
} // employee ID setter function

int getEmployeeID(){
return employeeID;
} // Employee ID getter

void setEmployeeName(char* _employeeName){
if (strlen(_employeeName) > 0){
employeeName = new char[strlen(_employeeName)];
strcpy(employeeName, _employeeName);
}
} // Employee name setter funciton

char* getEmployeeName(){
return employeeName;
} // Getter EmployeeName function

void setDepartment(char* _department){
if (strlen(_department) > 0){
department = new char[strlen(_department)];
strcpy(department, _department);
}
} // to set the department function

char* getDepartment(){
return department;
} // to get the department function

void setEmployeeSalary(int _employeeSalary){
if(_employeeSalary > 0){
employeeSalary = _employeeSalary;
}
} // to set the salary function 

float getEmployeeSalary(){
return employeeSalary;
} // to get the salary function

float calcNetPay(){
if (employeeSalary>=0 && employeeSalary<=10000)
employeeSalary = employeeSalary;
else if(employeeSalary>10000 && employeeSalary<=20000)
employeeSalary -= employeeSalary * (0.05);
else if(employeeSalary>20000 && employeeSalary<=30000)
employeeSalary -= employeeSalary * (0.07);
else if(employeeSalary>30000)
employeeSalary -= employeeSalary * (0.10);
} // to calculate the net salary function

void display(){
coutgetEmployeeID()"\t"getEmployeeName()"\t"getDepartment()"\t"getEmployeeSalary()endl;
} // end of display function

~Employee(){
delete []employeeName;
delete []department;
--totalNoOfEmployees;
} // end of destructor

}; // end of class definition
int Employee::totalNoOfEmployees=0; // this is static variable

main(){
Employee employee[4]; // for employee we creating array 
// initilization of all object 
Employee emp1(5, "Ahmad", "Accounts", 20000.0 ); 
Employee emp2(emp1) ;
Employee emp3(10, "Ayesha", "Accounts", 25000.0);
Employee emp4(3, "Hassan", "Administration", 10000.0);
Employee emp5(6, "Arsalan", "Administration", 35000.0);
// for adding objects to array
employee[0] = emp1;
employee[1] = emp2;
employee[2] = emp3;
employee[3] = emp4;
employee[4] = emp5;

for (int i = 0; i < 5; i++){
employee[i].calcNetPay();
employee[i].display();
}

system("pause");
}
// program has been end.

RSS

Looking For Something? Search Below

Latest Activity

waseem akram left a comment for Huma Ahmad
4 hours ago
waseem akram left a comment for Huma Ahmad
4 hours ago
Hania Khalid Shariff is now friends with Mr Siren Head and Huma Ahmad
6 hours ago
saba zulfqar posted blog posts
9 hours ago
+ ¢αяєℓєѕѕ gιяℓ updated their profile
10 hours ago
saba zulfqar and Hania Khalid Shariff are now friends
12 hours ago
Mr Siren Head replied to Hania Khalid Shariff's discussion zero semester
18 hours ago
Profile Iconshahzad Hussain and Umar Farooq joined Virtual University of Pakistan
18 hours ago

VIP Member Badge & Others

How to Get This Badge at Your Profile DP

------------------------------------

Management: Admins ::: Moderators

Other Awards Badges List Moderators Group

© 2021   Created by + M.Tariq Malik.   Powered by

Promote Us  |  Report an Issue  |  Privacy Policy  |  Terms of Service