Download presentation
Presentation is loading. Please wait.
2
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem
3
2 Namespaces b In Windows, all items of interest to the user are collected under a common namespace and its root is the desktop b b In C++, a namespace is a unit for grouping classes and instances and controlling their scope and visibility b Namespaces are not physical locations, these are logical names grouping together related items
4
3 Namespaces b b The using-directive allows the names in a namespace to be used without the namespace-name as an explicit qualifier b b namespace M bb{bb{ b b int i; bb}bb}
5
4 Namespaces b b using namespace M; b b namespace N bb{bb{ b b int j; b b double f(){ return M::d;} b b // error: M::d does not yet b b exist b b int k() {return i) bb}bb}
6
5 Namespaces b b namespace M // namespace extension bb{bb{ b b double d; bb}bb} bb bb b b // now M::d can be used All our standard header files are in namespace std
7
6 First Class Travel b Let us define our first class as the class for accepting and displaying time b We will learn to declare variables (objects) belonging to this class and use the same in our programs b Class definition goes into a header file and it is surrounded by pre-processor directives to prevent re-defining class members in programs that include this file
8
7 Pre-Processor Directives b #ifndef TIME1_H b #define TIME1_H b Class Time { b.. b Class definition goes here b.. b }; b #endif
9
8 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
10
9 Class Definition b class Time { b public: b Time(); b void SetTime(int,int,int); b void PrintMilitary(); b void PrintStandard();
11
10 Class Definition b private: b int hours; b int minutes; b int seconds; b char ampm; b };
12
11 Lab Exercise 14 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. We show the default constructor that accepts no arguments and executes by default when an object of this class is declared
13
12 Lab Exercise 14 b Time::Time() b{b{b{b{ b hours=0; b minutes=0; b seconds=0; b ampm=‘a’; b}b}b}b}
14
13 Lab Exercise 14 (Demo) b After completing the class definition (in a header file) and member functions (in an implementation file), develop a driver function i.e. main function (in another file) b In the main function, define an object of this class and use it to set time to 2 hours, 34 minutes and 33 seconds. Print time values in military and standard formats
15
14 How Many Files? b Class definition in a header file (e.g. Time.h) b Class implementation in a C++ source code file (e.g. Time.cpp) b Class driver code in a C++ source code file (e.g. driver.cpp) b All these files are part of your project
16
15 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
17
16 Tips for Good Programming b Always plan for your program on paper before starting on computer b Divide the program into a number of functions, each one dedicated to a small task
18
17 Tips for Good Programming b Before writing a function, write comments about it, including pre and post-conditions and what is it supposed to accomplish. Use meaningful variable names b For example, buy() function in a vending machine allows the user to buy an item. It subtracts one item from the machine’s contents and adds sale amount to total cash in the machine
19
18 Tips for Good Programming b To represent apple juice cans in a vending machine, declare a variable called b int apple_juice_cans; b instead of declaring b int var1; b Also constants are to be declared by name b For example, const int price_per_can=50
20
19 Tips for Good Programming b Use diagnostic messages if your program hangs and you see a blank screen b For example, use b cout<<“OK until this line”<<endl; b and keep moving this statement forward if you see it displayed in the output window
21
20 Handling Classes b You have to plan for implementing a program with classes b Decide about the structure of the class, its private and public members b Then define the class in a header file and implement its functions in an implementation file b Develop the client code later to use the objects that belong to this class
22
21 Practice Problem for Handling Classes b A vending machine contains fruit juices and soft drinks. It can have a maximum of 10 apple juice cans, 10 orange juice cans, 10 mixed juice cans, 10 pepsi cans and 10 sprite cans. Users can buy one can at a time and more items can be added to the machine
23
22 Practice Problem b Develop a program that models this vending machine as an object. It should have all items as private data items and buy/add functions as public functions. There should be another public function show() that shows (prints) all items and their current quantity available.
24
23 Additional Practice Problem b Declare a class calculator that has member functions add,subtract, multiply and divide. It has one private member named result. Only the member functions can access the result. Show implementation of at least two member functions. How will the client program use the result of one function for the other function. For example b (12+3)-5
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.