Download presentation
Presentation is loading. Please wait.
1
1 Lab Session-XII CSIT121 Fall 2000 b Namespaces b Will This Program Compile ? b Master of Deceit b Lab Exercise 12-A b First Taste of Classes b Lab Exercise 12-B
2
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
3
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}
4
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}
5
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
6
6 Will This Program Compile? b #include b #include b using namespace std; b reject main() b{b{b{b{ b chocolate plenty;
7
7 Will This Program Compile? b shout "How many chocolates do you have?"; b eat plenty; b if (plenty<mine) b shout "You have less chocolates than I do!!Ha Ha!!!"; b damn_it
8
8 Master of Deceit (#define) b The #define pre-processor directive allows you to modify the C++syntax rules if you do not wish to remember all these rules b You can invent your own rules no matter how outrageous these rules look. b Let us take a look at the source code shown above b Will it work or not?
9
9 Lab Exercise 12-A b The source code given completely violates the syntax rules that you know for reading and writing values from keyboard and the display b The main player here is the myown.h file that has revolutionized the syntax and you no longer need to remember >> and > and << redirectors b Let us look at contents of this file
10
10 Myown.h File b #define shout cout<< b #define chocolate int b #define eat cin>> b #define reject void b #define mine 23 b #define damn_it }
11
11 First Taste of Classes 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
12
12 Pre-Processor Directives b #ifndef TIME1_H b #define TIME1_H b Class Time { b.. b Class definition goes here b.. b }; b #endif
13
13 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 put in the definition of a class. However, data items cannot be initialized here. Initialization takes place in a constructor
14
14 Class Definition b class Time { b public: b Time(); b void SetTime(int,int,int); b void PrintMilitary(); b void PrintStandard();
15
15 Class Definition b private: b int hours; b int minutes; b int seconds; b char ampm; b };
16
16 Lab Exercise 12-B 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
17
17 Lab Exercise 12-B b Time::Time() b{b{b{b{ b hours=0; b minutes=0; b seconds=0; b ampm=‘a’; b}b}b}b}
18
18 Lab Exercise 12-B 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 print time values in military and standard formats
19
19 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
20
20 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
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.