Department of Computing: CS250: Data Structures and Algorithms
Department of Computing: CS250: Data Structures and Algorithms
Introduction
This purpose of this lab is to introduce students with the core concepts of object oriented
programming in c++. Student will solve given problems with the help of classes and objects
which will help them to get hands-on experience of critical thinking and problem solving using
object oriented programming languages. .
Objectives
This lab will revise the old concepts of the students, taught in the previous semesters.
Tools/Software Requirement
Visual Studio c++
Helping Material:
Lecture slides. Text book.
Lab Tasks
Task 1
Task 2
Define a class batsman with the following specifications:
Private members:
bcode 4 digits code number
bname 20 characters
innings, notout, runs integer type
batavg it is calculated according to the formula –
batavg =runs/(innings-notout)
calcavg() Function to compute batavg
Public members:
readdata() Function to accept value from bcode, name,
innings, notout and invoke the function calcavg()
displaydata() Function to display the data members on the
screen a code to find the memory in bytes occupied by int, long, double,
float and char.
Task 3
A common place to buy candy is from a machine. The machine sells candies, chips, gum, and
cookies. You have been asked to write a program for this candy machine.
The program should do the following:
1. Show the customer the different products sold by the candy machine.
The machine has two main components: a built-in cash register and several dispensers to hold
and release the products.
Define class cashRegister in C++ with the following descriptions :
Private Members:
cashOnHand of type integer
Public Members:
A default constructor cashRegister() sets the cash in the register to 500.
A constructor cashRegister(int) sets the cash in the register to a specific amount.
A function getCurrentBalance() which returns value of cashOnHand
A function acceptAmount(int) to receive the amount deposited by the customer
and update the amount in the register
int main(){
cashRegister counter;
dispenserType candy(100, 50);
dispenserType chips(100, 65);
dispenserType gum(75, 45);
dispenserType cookies(50, 85);
int choice;
showSelection();
cin >> choice;
while (choice != 5)
{
switch (choice)
{
case 1:
sellProduct(candy, counter);
break;
case 2:
sellProduct(chips, counter);
break;
case 3:
sellProduct(gum, counter);
break;
case 4:
sellProduct(cookies, counter);
break;
default:
cout << "Invalid selection." << endl;
}
showSelection();
cin >> choice;
}
return 0;
}
Answer:
Solution
Task 1 Code:
#include<iostream>
#include <string>
class Test{
private:
int flight_no;
string destination;
float distance;
float fuel;
float CALFUEL(float dis);
public:
void FEEDINFO();
void SHOWINFO();
};
float Test::CALFUEL(float dis){
if(dis<=1000){
fuel=500;
}
else if(dis>1000 && dis<=2000){
fuel=1100;
}
else{
fuel=2200;
}
return fuel;
}
void Test::FEEDINFO(){
cout<<"Enter flight number: ";
cin>>flight_no;
cout<<"Enter your destination: ";
cin>>destination;
cout<<"Enter the distance travelled: ";
cin>>distance;
fuel=CALFUEL(distance);
}
int main(){
Test obj;
obj.FEEDINFO();
cout<<endl;
obj.SHOWINFO();
return 0;
}
Task 2 Code:
#include<iostream>
#include<string>
using namespace std;
class Batsman{
private:
int bcode;
string bname;
public:
void readData();
void DisplayData();
};
float Batsman::calc_avg(int runs, int innings, int notout){
bat_avg =runs/(innings-notout);
return bat_avg;
}
void Batsman::readData(){
cout<<"Enter bcode: ";
cin>>bcode;
cout<<"Enter batsman name: ";
cin>>bname;
cout<<"Enter innings: ";
cin>>innings;
cout<<"Enter notout: ";
cin>>notout;
cout<<"Enter runs: ";
cin>>runs;
cout<<endl;
bat_avg=calc_avg(runs,innings,notout);
}
void Batsman::DisplayData(){
cout<<"Bcode is "<<bcode<<" and its size is "<<sizeof(bcode)
<<" bytes"<<endl;
cout<<"Batsman name is "<<bname<<" and its size is "<<sizeof
(bname)<<" bytes"<<endl;
cout<<"Innings is "<<innings<<" and its size is "<<sizeof(in
nings)<<" bytes"<<endl;
}
int main(){
Batsman b1;
b1.readData();
b1.DisplayData();
return 0;
}
Task 3 Code:
#include<iostream>
using namespace std;
class CashRegister{
private:
int cashOnHand=0;
int cashInRegister;
};
CashRegister::CashRegister(void){
cashInRegister=500;
}
CashRegister::CashRegister(int a){
cashInRegister=a;
}
int CashRegister::getCurrentBalance(){
return cashOnHand;
}
void CashRegister::AcceptAmount(int b){
cashOnHand=b;
cashInRegister+=cashOnHand;
}
class DispenserType{
private:
int numberOfItems;
int cost;
public:
DispenserType(void);
DispenserType(int num, int costt);
int getNumberOfItems();
int getCost();
void makeSale();
};
DispenserType::DispenserType(void){
cost=50;
}
void showSelection(){ // A method which displays the available tim
es
cout<<"Choose the item from the following list: "<<endl;
cout << "Select \n 1 for Candy \n 2 for chips \n 3 for gum \n 4 fo
r cookies\n 5 to exit .\n";
}
void sellProduct(DispenserType sweet, CashRegister cashcount)
{
int cashgiven;
cout << "Please Enter the amount you are paying:";
cin >> cashgiven;
cout << "Thank you for paying! (" << (cashgiven - sweet.getCos
t()) << " change has been given to you)";
cashcount.AcceptAmount(cashgiven - sweet.getCost());
sweet.makeSale();
cout << endl;
cout << "Here is your item\n";
}
int main(){