0% found this document useful (0 votes)
153 views7 pages

Department of Electrical Engineering CS212: Object Oriented Programming Class: BSCS10 - Section A&B Lab: 06

This document provides instructions for two lab tasks: 1. Implementing overloaded constructors for a Sandwich class with default and parameterized constructors. 2. Separating a class interface from its implementation by splitting the code into a header file for the class definition and a source code file for function definitions. Students are instructed to create a simple unit conversion application demonstrating this concept.

Uploaded by

umair khalid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
153 views7 pages

Department of Electrical Engineering CS212: Object Oriented Programming Class: BSCS10 - Section A&B Lab: 06

This document provides instructions for two lab tasks: 1. Implementing overloaded constructors for a Sandwich class with default and parameterized constructors. 2. Separating a class interface from its implementation by splitting the code into a header file for the class definition and a source code file for function definitions. Students are instructed to create a simple unit conversion application demonstrating this concept.

Uploaded by

umair khalid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Department of Electrical Engineering

CS212: Object Oriented Programming

Class: BSCS10 - Section A&B


Lab: 06
1. Overloaded Constructors
2. Separating Interface from Implementation
Instructor: Dr Shams Qazi

Lab Engineer: Ms Shakeela BiBi

CS212: Object Oriented Programming Page 1


Task 1: Overloaded Constructors
Introduction

In this lab Students will implement tasks using overloaded constructor and will come to know
how they can differentiate in default and overloaded constructors.

Objective

After performing this lab the student should be able to learn how to overload a constructor and
what are the scenarios where overloaded constructors can be used.

Tools/Software Requirement

Microsoft Visual Studio

Task#1

Implement a Sandwich class that includes data members to represent a Sandwich filling,
size, is_ready, and price. The class interface includes methods that provide appropriate
access to the data members (e.g., methods to get/set the Sandwich's data).

class Sandwich{

string filling;

double price;

bool is_ready;

……

public:

Sandwich();

Sandwich(string filling, double price);

CS212: Object Oriented Programming Page 2


void setFilling(string);

void setPrice(double);

bool check_status();

void printData();

};

Provide implementations of default constructor and parameterized constructor.

The setFilling() function sets the filling, and also sets the is_ready variable to true.

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 3


CS212: Object Oriented Programming Page 4
Task 2: Separating Interface from Implementation

Introduction
In this lab, we are going to explore how we can separate class interface from the implementation.

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.

Tools/Software Requirement

 Microsoft Visual Studio

Description
It is important to separate Class definition from the client code. This practice is considered as a
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
};

CS212: Object Oriented Programming Page 5


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 ){
void GradeBook::setCourseName( name ){
courseName = name;
}
string GradeBook::getCourseName(){
return courseName;
}
void GradeBook::displayMessage(){
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;
}

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)

CS212: Object Oriented Programming Page 6


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.
Task

Implement Simple Unit Conversion App example in Visual Studio by creating three files

a) UnitConversion.h
b) UnitConversion.cpp
c) TestUnitConversion

Write Client Code to create objects and convert units.

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 7

You might also like