Sei sulla pagina 1di 4

COMP1011 Programming Fundamentals

COMP1011 Programming Fundamentals


Laboratory Exercise 10
Date: 30 Mar-03Apr 2015

Name: Click here to enter text.

Student ID:Click here to enter text.

Objectives:
Learn how to define class

Learn how to define member functions i.e. private or public

Learn how to define pointers to object

Part I. Review Questions


A. class
Define a class Rectangle, to store the following information: height (int),
width (int), Rectangle constructor to create a new rectangle with some set
height and width, getArea( ), getHeight( ), getWidth( ), setHeight( ),
setWidth( ), dimensionsRectangle( ).
class Rectangle
{
public: //everything that follows is public
//Default constructor
Rectangle(); //the default constructor has no parameters
// Constructor to create a new rectangle with the given values
Rectangle(int w, int h);
// Setters that change the values of the attributes
void setWidth(int w);
void setHeight(int h);
// Getters that return information about the rectangle, note the const at the end of the
method
// this guarantees that the method cannot alter the member variables
int getWidth() const;
int getHeight() const;
int getArea() const;
// Display method that prints the rectangle's height and width
void displayRectangle() const;
private: //everything that follows is private and cannot be "seen" from out-side the class
// Some classes have private methods, this one doesn't!
int width;
int height;
}; //Rectangle - note the ";" - don't forget it!

Complete a program that creates an object named rect1 of the class


COMP1011 Lab 10

Page 1

COMP1011 Programming Fundamentals


Rectangle , and prompts the user to input data in order to set the height and
width of Rectangle and then display its information by different means;
#include<iostream>
usingnamespace std;
Rectangle::Rectangle(){
width = 1;
height = 1;
}
void Rectangle::setWidth(int w){
width = w;
}
void Rectangle::setHeight(int h){
height = h;
}
int Rectangle::getWidth() const{
return width;
}
int Rectangle::getHeight() const{
return height;
}
int Rectangle::getArea() const{
return width*height;
}
void Rectangle::displayRectangle() const{
cout<<"Rectangle's width :"<< width<<endl;
cout<<"Rectangle's Height :"<<height<<endl;
}
int main( ){
Rectangle rect1;
rect1.displayRectangle();
return 0;
}

Part II. Exercise


Write a C++ program that reads employeesID followed by their name, year
of birth, the year in which they joined the companyand their salary. The
program should build a class named Employee which should be referred by
pointer array.
Employee* emp_record = new Employee(100);
The program must contain the following features:
- Asks for the number of employees, then enter their record
-

Must be able to output the record of oldest employee

Can trace based upon the experience and find the employees with least
and most experience

COMP1011 Lab 10

Page 2

COMP1011 Programming Fundamentals


-

Can trace based upon the salary and find the employees with minimum
and maximum salaries

Place your source code here.


#include <iostream>
#include<string>
using namespace std;
class Employee
{
public:
Employee(double n); //default constructor
void get_ID(string x,int n) ;
void get_birth(double x,int n) ;
void get_yr(double x,int n) ;
void get_salary(double x,int n) ;
void get_name(string x,int n) ;
void get(int z);
void exp(int n);
void ss(int n);
private:
string ID[100];
string name[100];
double birth[100];
double yr[100];
double salary[100];
};
Employee::Employee(double n){
}
void Employee::get_ID(string x,int n){
ID[n]=x;
}
void Employee::get_name(string x,int n){
name[n]=x;
}
void Employee::get_birth(double x,int n) {
birth[n]=x;
}
void Employee::get_yr(double x,int n) {
yr[n]=x;
}
void Employee::get_salary(double x,int n) {
salary[n]=x;}
void Employee::get(int n){cout<<"Employee"<<n+1<<":"<<endl;
cout<<"ID:"<<ID[n]<<endl;
cout<<"name:"<<name[n]<<endl;
cout<<"year of birth:"<< birth[n]<<endl;
cout<<"year joined company:"<<yr[n]<<endl;
cout<<"salary:"<<salary[n]<<endl;};
void Employee::exp(int n){int z=0;int u=0;int y=z;int h=u; double least=yr[z]; double
most=yr[u];
while(z<n){ if(least>yr[z]){least=yr[z];y=z;}z++;} cout<<"least experience:"<< name[y]<<endl;
while(u<n){ if(most<yr[u]){most=yr[u];h=u;}u++;} cout<<"most experience:"<< name[h]<<endl;}

COMP1011 Lab 10

Page 3

COMP1011 Programming Fundamentals


void Employee::ss(int n){int z=0;int u=0;int y=z;int h=u; double least=salary[z]; double
most=salary[u];
while(z<n){ if(least>salary[z]){least=salary[z];y=z;}z++;} cout<<"minimum salaries:"<<
name[y]<<endl;
while(u<n){ if(most<salary[u]){most=salary[u];h=u;}u++;} cout<<"maximum salaries:"<<
name[h]<<endl;}
int main(){
int m;int y;m=0;
cout<<"number of employees:";cin>>y;
Employee* x =new Employee(m);
while(m<y){cout<<"Employee"<<m+1<<":"<<endl;
string ID; string name; double birth;double yr; double salary;
cout<<"ID:";
cin>>ID; (*x).get_ID(ID,m) ;
cout<<"name:"; cin>>name; (*x).get_name(name,m) ;
cout<<"year of birth:";
cin>>birth; (*x).get_birth(birth,m) ;
cout<<"year joined company:"; cin>>yr; (*x).get_yr(yr,m) ;
cout<<"salary:";
cin>>salary; (*x).get_salary(salary,m) ;
m++;}
int z=0;
while(z<y){(*x).get(z);cout<<endl;z++;}
(*x).exp(y);
(*x).ss(y);
return 0;
}

~End of Lab 10~

COMP1011 Lab 10

Page 4

Potrebbero piacerti anche