0% found this document useful (0 votes)
57 views

Marryam Azhar Bs Cs 10 B CMS ID: 346584

The document describes a lab assignment on object-oriented programming concepts in C++. The tasks involve implementing a Sandwich class with overloaded constructors, and separating a class interface from its implementation across multiple files. Students are asked to create a Sandwich class with default and parameterized constructors, setter and getter methods, and print the sandwich details. They also need to develop a simple unit conversion application by defining classes in a header file and implementing methods in a separate cpp file, and write client code to test the conversion. The lab aims to teach students how to overload constructors, separate interface from implementation, and apply OOP concepts in C++.

Uploaded by

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

Marryam Azhar Bs Cs 10 B CMS ID: 346584

The document describes a lab assignment on object-oriented programming concepts in C++. The tasks involve implementing a Sandwich class with overloaded constructors, and separating a class interface from its implementation across multiple files. Students are asked to create a Sandwich class with default and parameterized constructors, setter and getter methods, and print the sandwich details. They also need to develop a simple unit conversion application by defining classes in a header file and implementing methods in a separate cpp file, and write client code to test the conversion. The lab aims to teach students how to overload constructors, separate interface from implementation, and apply OOP concepts in C++.

Uploaded by

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

MARRYAM AZHAR

BS CS 10 B
CMS ID: 346584
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


CODE OF TASK 1

// MARRYAM AZHAR CMS ID:346584


#include<iostream>
using namespace std;
//Class
class Sandwich
{
//Data members
string filling;
double price;
string size;
bool is_ready;
//Member functions
public:
//Default Constructor
Sandwich()
{
filling = "chicken";
price = 100.0;
size = "small";
is_ready = true;
}
//overloaded constructor
Sandwich(string f,double p)
{
filling = f;
price = p;
}
// setter memeber function to set values
void setFilling(string is_filling)
{
filling = is_filling;
is_ready = true;

CS212: Object Oriented Programming Page 8


}
// getter member function to get the values
string getFilling()
{
return filling;
}
// setter memeber function to set values
void setPrice(double is_price)
{
price = is_price;
}
// getter member function to get the values
double getPrice()
{
return price;
}
// setter memeber function to set values
void setSize(string is_size)
{
size = is_size;
}
// getter member function to get the values
string getSize()
{
return size;
}
// getter member function to get the values
bool checkStatus()
{
return is_ready;
}
void printData()
{
cout << "~~~~~~~~SANDWHICH INFO~~~~~~~~\n Sandwich
filling is : " << getFilling()
<< " \n Sandwich size is : " << getSize()
<< " \n Sandwich price is : Rs " << getPrice();

CS212: Object Oriented Programming Page 9


if (checkStatus())
cout << " \n Sandwhich is ready!!\n"<<endl;
}
};
int main()
{
int order;
cout << " Please enter 1 to customize your order or enter 2 to have our
default sandwich : ";
cin >> order;
if (order == 2)
{
//Using and calling default constructor
Sandwich s1; //Object s1 of class Sandwich
cout << " Sandwich is getting ready.\n";
s1.printData();
}
else if (order==1)
{
string size,flavour;
double price;
cout << "~~~~~~FLAVOURS~~~~~~\n Tuna\n Chicken\n
Ham\n~~~~~~PRICES~~~~~~\n Small Rs.100\n Medium Rs.150\n Large
Rs.200\n";
cout << " The sandwich flavour you want : ";
cin >> flavour;
cout << " The size of the sandwich you want : ";
cin >> size;
cout << " Please pay the respective amount according to the size of
the sandwich you want : ";
cin >> price;
// Using parameterized constructor
/* at first the parameterized constructor has default values
after getting user input we will rest the default values
by using getter and setter functions.*/
Sandwich s2("chicken", 100); // Object s2 of class Sandwich
//setting values again according to user input

CS212: Object Oriented Programming Page 10


s2.setFilling(flavour);
s2.setSize(size);
s2.setPrice(price);
cout << " Sandwich is getting ready.\n";
s2.printData();
}
return 0;
}

CS212: Object Oriented Programming Page 11


OUTPUT OF TASK 1

CS212: Object Oriented Programming Page 12


CODE OF TASK 2

UnitConversion.h (The Header)


// MARRYAM AZHAR CMS ID: 346584
#include <iostream>
using namespace std;
class UnitConversion
{
// Data Members
double feet, meters, centimeters, pounds, kilograms, fahrenheit, celsius;
//Member functions
public:
//Default constructor prototype
UnitConversion();

//Setter function prototypes of length


void setFeetToMeters(double feet);
void setMetersToFeet(double meters);
void setMetersToCms(double meters);
void setCmsToMeters(double cms);
void setCmsToFeet(double cm);
void setFeetToCms(double feet);

//Setter function prototypes of weight


void setKilograms(double pounds);
void setPounds(double kg);

//Setter function prototypes of temperature


void setCelsius(double fahrenheit);
void setFahrenheit(double celsius);

//Getter function prototypes of length


double getFeetToMeters();
CS212: Object Oriented Programming Page 13
double getMetersToFeet();
double getMetersToCms();
double getCmsToMeters();
double getCmsToFeet();
double getFeetToCms();

//Getter function prototypes of weight


double getKilograms();
double getPounds();

//Getter function prototypes of temperature


double getCelsius();
double getFahrenheit();
};

UnitConversion.cpp (Source Code File)

//MARRYAM AZHAR CMS ID:346584


#include <iostream>
#include "UnitConversion.h"
using namespace std;

//Function Definitions using scope resolution operator ::


//Default constructor defination
UnitConversion::UnitConversion()
{
//All values are initialized to 0
meters = 0.0;
feet = 0.0;
centimeters = 0.0;

pounds = 0.0;
kilograms = 0.0;

fahrenheit = 0.0;

CS212: Object Oriented Programming Page 14


celsius = 0.0;
}

//Setter and getter function definations of length


void UnitConversion::setFeetToMeters(double feet)
{
meters = feet * 0.3048;
}
double UnitConversion::getFeetToMeters()
{
return meters;
}

void UnitConversion::setMetersToFeet(double meters)


{
feet = meters * 3.28084;
}
double UnitConversion::getMetersToFeet()
{
return feet;
}
void UnitConversion::setMetersToCms(double meters)
{
centimeters = meters * 100;
}
double UnitConversion::getMetersToCms()
{
return centimeters;
}
void UnitConversion::setCmsToMeters(double cms)
{
meters = cms / 100;
}
double UnitConversion::getCmsToMeters()
{
return meters;
}

CS212: Object Oriented Programming Page 15


void UnitConversion::setCmsToFeet(double cms)
{
feet = cms * 0.0328084;
}
double UnitConversion::getCmsToFeet()
{
return feet;
}
void UnitConversion::setFeetToCms(double feet)
{
centimeters = feet * 30.48;
}
double UnitConversion::getFeetToCms()
{
return centimeters;
}

//Setter and getter function definations of weight


void UnitConversion::setKilograms(double pounds)
{
kilograms = pounds * 0.453592;;
}
double UnitConversion::getKilograms()
{
return kilograms;
}
void UnitConversion::setPounds(double kgs)
{
pounds = kgs * 2.20462;
}
double UnitConversion::getPounds()
{
return pounds;
}

//Setter and getter function definations of temperature

CS212: Object Oriented Programming Page 16


void UnitConversion::setCelsius(double fahrenheit)
{
celsius = (fahrenheit - 32) * (5 / 9);
}
double UnitConversion::getCelsius()
{
return celsius;
}
void UnitConversion::setFahrenheit(double celsius)
{
fahrenheit = (celsius * (9 / 5)) + 32;
}
double UnitConversion::getFahrenheit()
{
return fahrenheit;
}

TestUnitConversion.cpp

// MARRYAM AZHAR CMS ID: 346584


#include <iostream>
#include "UnitConversion.h"
using namespace std;

int main()
{
int calculation;
//to store the number the user, want to convert
double number;
//to store the converted result
double result;

cout << " Which conversion do you want to perform?"


//LENGTH CONVERSIONS

CS212: Object Oriented Programming Page 17


<< "\n\t\t1. Feet to Meters"
<< "\n\t\t2. Meters to Feet"
<< "\n\t\t3. Feet to Centimeters"
<< "\n\t\t4. Centimeters to Feet"
<< "\n\t\t5. Meters to Centimeters"
<< "\n\t\t6. Centimeters to Meters"
//WEIGHT CONVERSIONS
<< "\n\t\t7. Kilogrammes to Pounds"
<< "\n\t\t8. Pounds to Kilogrammes"
//TEMPERATURE CONVERSIONS
<< "\n\t\t9. Celsius to Fahrenheit"
<< "\n\t\t10. Fahrenheit to Celsius"
<< "\n\n\tEnter your choice : ";
cin >> calculation;

cout << " Enter the number you want to convert: ";
cin >> number;

// value is the Object of class UnitConversion


UnitConversion value;

cout << "\n\t ~~~~~~~~RESULT~~~~~~~~\n\t\t" << number;

//calls and pass the value to the conversion function the user has chosen
//prints the converted result accordingly
switch (calculation) {
case 1:
value.setFeetToMeters(number);
result = value.getFeetToMeters();
cout << "ft" << " = " << result << "m\n";
break;
case 2:
value.setMetersToFeet(number);
result = value.getMetersToFeet();
cout << "m" << " = " << result << "ft\n";
break;
case 3:

CS212: Object Oriented Programming Page 18


value.setFeetToCms(number);
result = value.getFeetToCms();
cout << "ft" << " = " << result << "cm\n";
break;
case 4:
value.setCmsToFeet(number);
result = value.getCmsToFeet();
cout << "cm" << " = " << result << "ft\n";
break;
case 5:
value.setMetersToCms(number);
result = value.getMetersToCms();
cout << "m" << " = " << result << "cm\n";
break;
case 6:
value.setCmsToMeters(number);
result = value.getCmsToMeters();
cout << "cm" << " = " << result << "m\n";
break;
case 7:
value.setPounds(number);
result = value.getPounds();
cout << "kg" << " = " << result << "lb\n";
break;
case 8:
value.setKilograms(number);
result = value.getKilograms();
cout << "lb" << " = " << result << " kg\n";
break;
case 9:
value.setFahrenheit(number);
result = value.getFahrenheit();
cout << "C" << " = " << result << "F\n";
break;
case 10:
value.setCelsius(number);
result = value.getCelsius();

CS212: Object Oriented Programming Page 19


cout << "F" << " = " << result << "C\n";
break;
}
}

CS212: Object Oriented Programming Page 20


OUTPUT OF TASK 2

SOME OUTPUTS OF TASK 2 ARE AS FOLLOWS:

CS212: Object Oriented Programming Page 21


CS212: Object Oriented Programming Page 22

You might also like