Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Lab Session-2 CSIT221 Spring 2003 b Intro to Object Oriented Programming b Practice Problem b Lab Exercise (Demo Required)"— Presentation transcript:

1

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

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

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

5 4 Building Class Definition b 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 b 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

6 5 Constructor b Once the class definition is completed, Start building up the source code for all the member functions in the implementation file b 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

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

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

9 8 Lab Exercise (Demo required on 2/11 for Sec-1 and 2/13 for Sec-2) b 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

10 9 User Scenario b Display total number of all types of soft drinks and cash at hand b Let the user buy two bottles b Display the balance cash and bottles b Let the user buy three bottles b Display the balance cash and bottles b Let the vendor take away the cash and fill up the machine b Display the balance cash and bottles


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

Similar presentations


Ads by Google