You are on page 1of 8

Department of Computing

CS 212: Object Oriented Programming

Class: BEE-13D
Fall 2022

Lab05: Getters and Setters, Separating


implementation from interface
Name:abdurra
him
Reg NO :369247

Introduction
In this lab, we are going to explore how we can separate class interface from the implementation
and the usage of getters and setters.

Objective
Students would be able to
 Split class code into two separate files: (i) the header file that stores class definition and
source code file.
 Assign values to attributes using setter function and access the values using getter function

Description
It is important to separate Class definition from the client code. This practice is considered as a
CS 212: Object Oriented Page 1
Programming
fundamental software engineering principle.
The interface is separated from the implementation by splitting the class code into two files
 The Header (GradeBook.h)
 Source-Code File (GradeBook.cpp)

The class definition is stored in GradeBook.h and member functions are defined in GradeBook.cpp
GradeBook.h
#include <string>
using namespace std;
class GradeBook
{
public:
GradeBook( string );
void setCourseName( string );
string getCourseName();
void displayMessage();
private:
string courseName; // course name for this GradeBook
};

Source-code file GradeBook.cpp defines class GradeBook’s member functions. Each member-
function name in the function headers is preceded by the class name and :: which is known as the
binary scope resolution operator. This “ties” each member function to the (now separate)
GradeBook class definition, which declares the class’s member functions and data members.
Without “GradeBook:: preceding each function name, these functions would not be
recognized by the compiler as member functions of class GradeBook.

GradeBook.cpp
#include <iostream>
#include "GradeBook.h"
using namespace std;
GradeBook::GradeBook(string name)
{
courseName = name;
}
void GradeBook::setCourseName( string name){
courseName = name;
}
string GradeBook::getCourseName(){
return courseName;
}
void GradeBook::displayMessage(){

CS 212: Object Oriented Page 2


Programming
cout << "Welcome to the grade book for\n" << getCourseName() << "!" <<
endl;
}

Finally, we can write the client code in another file TestGradeBook.cpp. Separating GradeBook’s
interface from the implementation of its member functions does not affect the way that this client
code uses the class.

#include <iostream>
#include "GradeBook.h"
using namespace std;
int main(){
GradeBook gradeBook1( "CS101 Introduction to C++ Programming" );
GradeBook gradeBook2( "CS102 Data Structures in C++" );
cout << "gradeBook1 created for course: "
<< gradeBook1.getCourseName() << "\ngradeBook2 created for course:"
<< gradeBook2.getCourseName() << endl;
}

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: Implement GradeBook example in Visual Studio by creating three files


a) GradeBook.h
b) GradeBook.cpp
c) TestGradeBook

Extend the class implementation by providing data validation for the private data member
“courseName”. The length of course name should not exceed 25 characters. In case of an invalid
argument the set function should return a code so that the client code can check whether the
assignment was successful or not.
Altered Code:
Header file:
#include <string> // iostream is not neede as it is used
using namespace std;
class GradeBook
{
public:// can be acessed outside the class
GradeBook(string);

bool setCourseName(string); // this is to check forthat string less than 25

CS 212: Object Oriented Page 3


Programming
string getCourseName();
void displayMessage();

private:
string courseName; //can be acessed inside the class
};
Cpp file:
Part 1 which is of class
#include <iostream>
#include<string.h>
#include "GradeBook.h" // it is used to caal out header file that we defined
using namespace std;
GradeBook::GradeBook(string name) // constructor
{
courseName = name;
}

bool GradeBook::setCourseName(string name) { // set name


courseName = name;

if (courseName.length() <= 25) {


return 1;
}
else
{
cout << "invalid statement" << endl;
return 0;
}
}

string GradeBook::getCourseName() { // it gets the coursename


return courseName;
}
void GradeBook::displayMessage(){
cout << "Welcome to the grade book for\n" << getCourseName() << "!" << endl;
}

Part 2:
#include <iostream>
#include <string>
#include "GradeBook.h"
using namespace std;
int main() {

bool check;
GradeBook gradeBook1("CS101 Introduction to C++ Programming");
GradeBook gradeBook2("CS102 Data Structures in C++");
cout << "gradeBook1 created for course: "
<< gradeBook1.getCourseName() << "\ngradeBook2 created for course:" <<
gradeBook2.getCourseName() << endl;
string book;
cout << "enter the string:";
getline(cin, book); // input for string

CS 212: Object Oriented Page 4


Programming
GradeBook gb3(book);

check = gb3.setCourseName(book);
if(!check){
book = book.substr(0, 25); // here 0 is for positon and 25 is for length
gb3.setCourseName(book);
}
cout<< gb3.getCourseName();

Output:

Hint: Following program trims Geeks to eek using inbuilt function substr()
// CPP program to illustrate substr()
#include <string.h>
#include <iostream>
using namespace std;

CS 212: Object Oriented Page 5


Programming
int main()
{
    // Take any string
    string s1 = "Geeks";
    string r;

    // Copy the string using substr


    r = s1.substr(1, 3);

    // prints the result


    cout << "String is: " << r;

    return 0;
}
Output:

String is: eek

TASK 2: Create a Simple Unit Conversion App that allows converting the following
1. Length (Foot to Meter, Centimeter etc)
2. Weight (Pounds to Kg, Kg to Pounds etc.)
3. Temperature ( Fahrenheit to Celsius, Celsius to Fahrenheit)

Apply Object Oriented Programming concepts to ensure that data is private and accessed properly
through public interface comprising of set and get functions.
Split implementation across multiple files (Header Files and Source Code Files) to ensure that
application design is modular.

Write Client Code to create objects and convert units.


Code
Header file:
#pragma once
#include<iostream>
using namespace std;
class converter
{
private:
double l;
double p;
double k;
double f;
double c;
public:

void set_length();// set length in foot


void set_pound();// set weight in pound
void set_kilo();// set weight in kilogram

CS 212: Object Oriented Page 6


Programming
void set_farenheit();// set temp in fareheit
void set_celcius(); // set temp in celcius
double getlength_meter(); // convert to meter
double getlength_centimeter();// convert to centi meter
double getweight_kilogram();// convert to kilogram
double getweight_pound();// convert to pound
double gettemp_farenheit();// convert to fareheit
double gettemp_celcius();// convert to celcius
};
Cpp program:
#include<iostream>
#include"Header.h" // unstandandard file
using namespace std;
//:: is a scope resolution which calls the class
void converter::set_length() {
cout << "enter the length in foot:";
cin >> l;

}
void converter::set_pound() {
cout << "enter the weight in pound:";
cin >> p;
}
void converter :: set_kilo() {
cout << "enter the weight in kilogram:";
cin >> k;
}
void converter::set_farenheit() {
cout << "enter the temp in farenheit:";
cin >> f;
}
void converter::set_celcius() {
cout << "enter the temp in celcius:";
cin >> c;
}
double converter::getlength_meter() {
return 0.3048 * l;
}
double converter::getlength_centimeter() {
return 30.48 * l;
}
double converter::getweight_kilogram() {
return 0.45359237 * p;
}
double converter::getweight_pound() {
return 2.2 * k;
}
double converter::gettemp_farenheit() {
return (1.8 * f) + 32;
}
double converter::gettemp_celcius() {
return (5/9)*(c-32);
}

CS 212: Object Oriented Page 7


Programming
Cpp program:
#include<iostream>
#include"Header.h" // calls unstandard file
using namespace std;
int main() {
converter fun;
fun.set_length();
fun.set_pound();
fun.set_kilo();
fun.set_farenheit();
fun.set_celcius();
cout<<"the length in meter is "<<fun.getlength_meter()<<" m"<<endl;
cout << "the length in centimeter is " << fun.getlength_centimeter() << " cm" << endl;
cout << "the weight in kilogram is " << fun.getweight_kilogram() << " kg" << endl;

cout << "the weight in kilogram is " << fun.getweight_pound() << " lb" << endl;
cout << "the temperature in ceclcius is " << fun.gettemp_celcius() << " C" << endl;
cout << "the temperature in farenheit is " << fun.gettemp_farenheit() << " F" << endl;

}
Output:

Deliverables: Complete lab manual by performing all tasks. Copy paste your code and screen
shot of console window as a solution of each task. You are required to upload the lab tasks on
LMS and the name of the task must be in this format YourFullName_reg#.

CS 212: Object Oriented Page 8


Programming

You might also like