Department of Computing
CS212: Object Oriented Programming
                       Lab 03: GET and SET Functions
                                Date: July 30, 2021
                           Instructor: Dr Shams Qazi
LAB SUBMITTED BY: UMAIR KHALIL
                    Bee 10 C
                     256884
CS212: Object Oriented Programming     Page 1
                               Lab 04: GET and SET Functions
Introduction
This lab is about getter and setter functions in OOP.
Objective
To understand the difference between get and set functions and their usability in the object
oriented paradigm
Tools/Software Requirement
    Microsoft Visual Studio
Instructions
You are encouraged to use good programming conventions by entering appropriate comments,
using indentations, and using descriptive variable names in your programs. Insert the
solution/answer in this document as directed below. You must also submit this Word document
on the LMS within the time of the lab.
Task 1
Create a class Rectangle. The class has attributes length and width, each of which defaults to 1.
Provide methods that calculate the perimeter and the area of the rectangle. Provide set and get
methods for both length and width. The set methods should verify that length and width are each
floating-point numbers greater than or equal to 0.0 and less than 20.0. Write a program to test
class Rectangle.
Code :
#include <iostream>
using namespace std;
class Rectangle{
    private:
    float length,width,area,perimeter;
CS212: Object Oriented Programming            Page 2
  public:
  Rectangle(){
  length=1;
  width=1;
  };
  void setdimensions(){
       cout<<"Please enter the length of rectangle : ";
       cin>>length;
        while(length<0||length>=20){
         cout<<"Please enter the length between 0.0 and 20"<<endl;
         cout<<"Please enter the length again ";
        cin>>length;
       cout<<"Please enter the width of rectangle : ";
       cin>>width;
         while(width<0||width>=20){
         cout<<"Please enter the width between 0.0 and 20"<<endl;
         cout<<"Please enter the width again : ";
             cin>>width ;
   perimeter=2*(length+width);
CS212: Object Oriented Programming                  Page 3
      area=length*width;
     void getdimensions(){
         cout<<"Perimeter of the rectangle is "<<perimeter<<" units"<<endl;
         cout<<"Area of rectangle is "<<area<<" units"<<endl;
};
int main(){
     Rectangle R1;
     R1.setdimensions();
     R1.getdimensions();
     return 0;
Output(Online compiler):
CS212: Object Oriented Programming                 Page 4
Task 2
Create a class called Employee that includes three pieces of information as instance variables—a
first name, a last name, and a monthly salary. Provide a set and a get method for each instance
variable. If the monthly salary is not positive, set it to 0.0.Write a test application named
EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects
and display the yearly salary for each Employee. Then give each Employee a 10% raise and
display each Employee’s yearly salary again.
Code:
#include <iostream>
#include <string>
using namespace std;
class Employee{
  private:
  string fname,lname,fname1,lname1;
                                                                                          int
salary,monthly_salary,incremented_salary,yearly_salary,salary1,monthly_salary1,incremented_s
alary1,yearly_salary1;
  public:
  void setfunction1(){
     cout<<"Please enter the first name of the 1st employee : ";
     getline(cin,fname);
 if ( fname.length() > 25 )
  fname = fname.substr( 0, 25 );
CS212: Object Oriented Programming           Page 5
   cout << "Name \"" << fname <<"\" exceeds maximum length (25).\n"<< "Limiting first Name
to first 25 characters.\n" << endl;
 };
       cout<<"\nPlease enter the last name of the 1st employee : ";
       getline(cin,lname);
       if ( lname.length() > 25 )
  lname = lname.substr( 0, 25 );
   cout << "Name \"" << lname <<"\" exceeds maximum length (25).\n"<< "Limiting lastName
to first 25 characters.\n" << endl;
 };
       cout<<"\nPlease enter the salary of the 1st employee : ";
       cin>>salary;
       if (salary>0){
            monthly_salary=salary;
       else{
            cout<<"\nInvalid salary entered. Salary set to 0.0";
            monthly_salary=0;
       };
       yearly_salary=monthly_salary*12;
       incremented_salary=yearly_salary+(0.1*yearly_salary);
  };
CS212: Object Oriented Programming                Page 6
  void getfunction1(){
       cout<<"\nThe name of the 1st employee is \"" <<fname<< lname<<"\""<<endl;
       cout<<"\nThe monthly salary of the 1st employee is "<<monthly_salary<<endl;
       cout<<"\nThe yearly salary of the 1st employee is "<<yearly_salary<<endl;
                cout<<"\n\nThe incremented yearly salary of the 1st employee is
"<<incremented_salary<<endl;
  };
  void setfunction2(){
       cout<<"\nPlease enter the first name of the 2nd employee : ";
       cin>>fname1;
 if ( fname1.length() > 25 )
  fname1 = fname1.substr( 0, 25 );
   cout << "Name \"" << fname1 <<"\" exceeds maximum length (25).\n"<< "Limiting first
Name to first 25 characters.\n" << endl;
 };
       cout<<"\nPlease enter the last name of the 2nd employee : ";
       cin>>lname1;
       if ( lname1.length() > 25 )
  lname1 = lname1.substr( 0, 25 );
   cout << "Name \"" << lname1 <<"\" exceeds maximum length (25).\n"<< "Limiting lastName
to first 25 characters.\n" << endl;
 };
CS212: Object Oriented Programming             Page 7
       cout<<"\nPlease enter the salary of the 2nd employee : ";
       cin>>salary1;
       if (salary1>0){
            monthly_salary1=salary1;
       else{
            cout<<"\nInvalid salary entered. Salary set to 0.0";
            monthly_salary1=0;
       };
       yearly_salary1=monthly_salary1*12;
       incremented_salary1=yearly_salary1+(0.1*yearly_salary1);
  };
  void getfunction2(){
       cout<<"\nThe name of the 2nd employee is \"" <<fname1<< lname1<<"\""<<endl;
       cout<<"\nThe monthly salary of the 2nd employee is "<<monthly_salary1<<endl;
       cout<<"\nThe yearly salary of the 2nd employee is "<<yearly_salary1<<endl;
                 cout<<"\nThe incremented yearly salary of the 2nd employee is
"<<incremented_salary1<<endl;
  };
  void EmployeeTest(){
      cout<<"\n\"Capabilities of Class Employee\"\n\nThe class named Employee is a class that
asks the user for employees data and then displays that data.\n\nUser enters first and last name of
the employees and class checks for whether the names are greater than 25 chars and if so, it
limits the name to first 25 chars to help memory management.\n\nUser is then asked to enter the
CS212: Object Oriented Programming                Page 8
salary and if salary is negative it assigns zero value to salary.\n\nIn the end data of the employee
is shown on the screen including name,salary,yearly salary and incremented salary . ";
     };
};
int main(){
     Employee E1,E2,E;
     E1.setfunction1();
     E2.setfunction2();
     E1.getfunction1();
     E2.getfunction2();
     E.EmployeeTest();
     return 0;
CS212: Object Oriented Programming            Page 9
Output(Online compiler)
Deliverables
Compile a single Word document by filling in the solution/answer part and submit this Word file
on LMS.
This lab is graded. Min marks: 0. Max marks: 10.
CS212: Object Oriented Programming         Page 10