Lab Session-2 CSIT221 Spring 2003 - PowerPoint PPT Presentation

About This Presentation
Title:

Lab Session-2 CSIT221 Spring 2003

Description:

... soft drinks and 95 cents change and zero dollars in bills. ... operate the machine, they insert one dollar bills and/or coins to buy the soft drinks. ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 10
Provided by: sunyfr
Category:
Tags: bill | csit221 | dollar | lab | session | spring

less

Transcript and Presenter's Notes

Title: Lab Session-2 CSIT221 Spring 2003


1
Lab Session-2 CSIT221 Spring 2003
  • Intro to Object Oriented Programming
  • Practice Problem
  • Lab Exercise (Demo Required)

2
Object Oriented Programming
  • We start our lab today with an introductory
    session for object oriented programming
  • OOP involves following steps
  • Definition of class in a header file including
    prototypes of member functions and private data
  • Implementation of public member functions
  • Testing with a driver program

3
Practice Problem
  • Define a class cat that includes private data
    members weight and color of the cat and some
    public member functions
  • Write a driver program that tests out the class
    by defining two objects that belong to this class

4
Building Class Definition
  • The class definition will consist of public and
    private sections. By default, everything is
    private. However, we should explicitly label the
    sections as such to avoid confusion
  • The function prototypes and the data items are
    included in the definition of a class. However,
    data items cannot be initialized here.
    Initialization takes place in a constructor

5
Constructor
  • Once the class definition is completed, Start
    building up the source code for all the member
    functions in the implementation file
  • The first one to be written is the constructor.
    The default constructor accepts no arguments and
    executes by default when an object of this class
    is declared

6
How Many Files?
  • Class definition in a header file (e.g. cat.h)
  • Class implementation in a C source code file
    (e.g. cat_imp.cpp)
  • Class driver code in a C source code file (e.g.
    cat_test.cpp)
  • All these files are part of your project

7
Be Careful
  • Use the keyword void in the implementation file
    before starting to implement any void member
    function
  • If parameters are being passed to a member
    function, specify their data types and local
    names
  • The order of inserting files may influence the
    number of compiling errors

8
Lab Exercise (Demo required on 2/11 for Sec-1 and
2/13 for Sec-2)
  • Model a vending machine as an object. Develop a
    class definition for a vending machine that has
    an initial quantity of ten bottles each for 5
    different soft drinks and 95 cents change and
    zero dollars in bills. As the users operate the
    machine, they insert one dollar bills and/or
    coins to buy the soft drinks. The machine can
    display and change the number of bottles of each
    type and its total cash with the help of member
    functions. The driver program cannot alter any
    private data. All private data must be handled by
    the member functions only. Show a complete demo
    with the following scenario

9
User Scenario
  • Display total number of all types of soft drinks
    and cash at hand
  • Let the user buy two bottles
  • Display the balance cash and bottles
  • Let the user buy three bottles
  • Display the balance cash and bottles
  • Let the vendor take away the cash and fill up the
    machine
  • Display the balance cash and bottles
Write a Comment
User Comments (0)
About PowerShow.com