Lab 3 OOp
Lab 3 OOp
Bee 10 C
256884
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
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>
class Rectangle{
private:
float length,width,area,perimeter;
Rectangle(){
length=1;
width=1;
};
void setdimensions(){
cin>>length;
while(length<0||length>=20){
cin>>length;
cin>>width;
while(width<0||width>=20){
cin>>width ;
perimeter=2*(length+width);
void getdimensions(){
};
int main(){
Rectangle R1;
R1.setdimensions();
R1.getdimensions();
return 0;
Output(Online compiler):
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>
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(){
getline(cin,fname);
if ( fname.length() > 25 )
fname = fname.substr( 0, 25 );
};
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;
};
cin>>salary;
if (salary>0){
monthly_salary=salary;
else{
monthly_salary=0;
};
yearly_salary=monthly_salary*12;
incremented_salary=yearly_salary+(0.1*yearly_salary);
};
};
void setfunction2(){
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;
};
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;
};
cin>>salary1;
if (salary1>0){
monthly_salary1=salary1;
else{
monthly_salary1=0;
};
yearly_salary1=monthly_salary1*12;
incremented_salary1=yearly_salary1+(0.1*yearly_salary1);
};
void getfunction2(){
};
void EmployeeTest(){
};
};
int main(){
Employee E1,E2,E;
E1.setfunction1();
E2.setfunction2();
E1.getfunction1();
E2.getfunction2();
E.EmployeeTest();
return 0;
Deliverables
Compile a single Word document by filling in the solution/answer part and submit this Word file
on LMS.