You are on page 1of 13

Department of Computing

CS-250: Data Structure and Algorithms


Class: BESE 11AB

Lab 2: Classes, objects, dynamic memory allocation

Date: 24 Sep, 2021


Time: 10am-1pm & 2-5pm

Instructors: Dr. Ahsan Saadat & Dr Asad Waqar

CS250: Data Structures and Algorithms Page 1


Lab 2: Classes, objects, dynamic memory allocation

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

Define a class in C++ with following description:


Private Members
A data member Flight number of type integer
A data member Destination of type string
A data member Distance of type float
A data member Fuel of type float
A member function CALFUEL() to calculate the value of Fuel as per the
following criteria
Distance Fuel
<=1000 500
more than 1000 and <=2000 1100
more than 2000 2200
Public Members
A function FEEDINFO() to allow user to enter values for Flight Number,
Destination, Distance & call function CALFUEL() to calculate the quantity of
Fuel
A function SHOWINFO() to allow user to view the content of all the data
members

CS250: Data Structures and Algorithms Page 2


Use the following main() function for testing:
int main (){
TEST obj;
obj.SCHDULE();
obj.DISPTEST();
getch();
return 0;
}

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.

Use the following main() function for testing:


int main(){
batsman obj;
obj.readdata();
obj.displaydata();
getch();
return 0;
}

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.

CS250: Data Structures and Algorithms Page 3


2. Let the customer make the selection.
3. Show the customer the cost of the item selected.
4. Accept money from the customer.
5. Release the item.

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

Define class dispenserType in C++ with the following descriptions :


Private Members:
numberOfItems of type integer
cost of type integer
Public Members:
A default constructor dispenserType () sets the cost and number of items in the
dispenser to 50 each.
A constructor dispenserType (int,int) sets the cost and number of items in the
dispenser to the values specified by the user.
A function getNoOfItems() to return the value of numberOfItems.
A function getCost() to return the value of cost.
A function makeSale() to reduce the number of items by 1.

When the program executes, it must do the following:


1. Show the different products sold by the candy machine.
2. Show how to select a particular product.
Once the user has made the appropriate selection, the candy machine must act accordingly. If the
user has opted to buy a product and that product is available, the candy machine should show the
cost of the product and ask the user to deposit the money. If the amount deposited is at least the
cost of the item, the candy machine should sell the item and display an appropriate message.
Divide this program into three functions: showSelection, sellProduct, and main.

CS250: Data Structures and Algorithms Page 4


The function sellProduct must have access to the dispenser holding the product (to decrement
the number of items in the dispenser by 1 and to show the cost of the item) as well as the cash
register (to update the cash). Therefore, this function has two parameters: one corresponding to
the dispenser and the other corresponding to the cash register

Use the following main() function for testing:

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>

CS250: Data Structures and Algorithms Page 5


using namespace std;

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);
}

CS250: Data Structures and Algorithms Page 6


void Test::SHOWINFO(){
    cout<<"Flight number: "<<flight_no<<endl;
    cout<<"Destination: "<<destination<<endl;
    cout<<"Distance travelled: "<<distance<<endl;
    cout<<"Fuel: "<<fuel<<endl;
}

int main(){
    Test obj;
    obj.FEEDINFO();
    cout<<endl;
    obj.SHOWINFO();

return 0;
}

Task 1 Output Screenshot:

Task 2 Code:

#include<iostream>
#include<string>
using namespace std;

class Batsman{
    private:
    int bcode;
    string bname;

CS250: Data Structures and Algorithms Page 7


    int innings;
    int notout;
    int runs;
    float bat_avg;
    float calc_avg(int runs, int innings, int notout);

    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;

CS250: Data Structures and Algorithms Page 8


       cout<<"Notout is "<<notout<<" and its size is "<<sizeof(not
out)<<" bytes"<<endl;
        cout<<"Runs of batsman is "<<runs<<" and its size is "<<si
zeof(runs)<<" bytes"<<endl;
        cout<<"Batsman average is "<<bat_avg<<" and its size is "<
<sizeof(bat_avg)<<" bytes"<<endl;

}
int main(){
    Batsman b1;
    b1.readData();
    b1.DisplayData();

    return 0;
}

Task 2 Output Screenshot:

Task 3 Code:

#include<iostream>
using namespace std;

class CashRegister{
    private:
    int cashOnHand=0;
    int cashInRegister;

CS250: Data Structures and Algorithms Page 9


    public:
    CashRegister(void);
    CashRegister(int a);
    int getCurrentBalance();
    void AcceptAmount(int b);

};
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;

CS250: Data Structures and Algorithms Page 10


    numberOfItems=50;
}
DispenserType::DispenserType(int num, int costt){
    numberOfItems=num;
    cost=costt;
}
int DispenserType::getNumberOfItems(){
    return numberOfItems;
}
int DispenserType::getCost(){
    return cost;
}
void DispenserType::makeSale(){
    numberOfItems=numberOfItems-1;

}
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(){

CS250: Data Structures and Algorithms Page 11


    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;
}

Task 3 Output Screenshot:

CS250: Data Structures and Algorithms Page 12


Deliverables
Compile a single word document by filling in the solution part and submit this Word file on LMS.
This lab grading policy is as follows: The lab is graded between 0 to 10 marks. The submitted
solution can get a maximum of 5 marks. At the end of each lab or in the next lab, there will be a
viva related to the tasks. The viva has a weightage of 5 marks. Insert the solution/answer in this
document. You must show the implementation of the tasks in the designing tool, along with
your complete Word document to get your work graded. You must also submit this Word
document on the LMS. In case of any problems with submissions on LMS, or any query email to
Mr. Aftab Hussain: aftab.hussain1@seecs.edu.pk.

CS250: Data Structures and Algorithms Page 13

You might also like