Marryam Azhar Bs Cs 10 B CMS ID: 346584
Marryam Azhar Bs Cs 10 B CMS ID: 346584
BS CS 10 B
CMS ID: 346584
Department of Electrical Engineering
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
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();
void setPrice(double);
bool check_status();
void printData();
};
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.
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
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 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
};
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
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
Deliverables
Compile a single Word document by filling in the solution/answer part and submit this Word file
on LMS.
pounds = 0.0;
kilograms = 0.0;
fahrenheit = 0.0;
TestUnitConversion.cpp
int main()
{
int calculation;
//to store the number the user, want to convert
double number;
//to store the converted result
double result;
cout << " Enter the number you want to convert: ";
cin >> 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: