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.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Copyright © 2002 Pearson Education, Inc. Slide 1.
1 Lab Session-1 CSIT221 Fall 2002 b Refresher slides b Practice Problem b Lab Exercise (Demo Required)
Classes and Objects Systems Programming.
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12A Separate Compilation and Namespaces For classes this time.
1 Lab Session-2 CSIT221 Spring 2003 b Intro to Object Oriented Programming b Practice Problem b Lab Exercise (Demo Required)
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Specification and Implementation Separating the specification from implementation makes it easier to modify programs. Changes in the class’s implementation.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
1 Lab Session-4 CSIT121 Fall 2004 Scope of Variables Top Down Design Problem The Solution Lab Exercise for Demo.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
1 Session-9 CSIT 121 Spring 2006 Lab Demo (first 20 minutes) The correct way to do the previous lab sheet Function calls Calling void functions Calling.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
Chapter 6: Functions.
IT PUTS THE ++ IN C++ Object Oriented Programming.
Abstract Data Types Using Classes Lecture-5. Abstract Data Types Using Classes Representing abstract data types using C++ We need the following C++ keywords.
Separate Compilation. A key concept in programming  Two kinds of languages, compilation (C, Pascal, …) and interpretation (Lisp, …, Matlab, Phython,
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
C++ Classes and Data Structures Jeffrey S. Childs
Chapter 11 Separate Compilation and Namespaces. Learning Objectives Separate Compilation –Encapsulation reviewed –Header and implementation files Namespaces.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
Chapter 9 Classes: A Deeper Look, Part I Part II.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Class 3 (L33) u Client of a Class u Purpose of Public Members u Private Class Members u Controlling Access u Access Functions u Predicate Functions u Utility.
Class 4 (L34) u Constructors u Default Constructor u Example of Default Constructors u Destructors u Constructors Are Called in Global Scope u Constructors.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
CS 132 Spring 2008 Chapter 2 Object-Oriented Design (OOD) and C++ Ideas: * Inheritance (and protected members of a class) ** Operator overloading Pointer.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Separate Compilation and Namespaces.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Friend Functions. Problem u Assuming two Complex objects u How would one add two numbers? W + X Complex operator+(const Complex& w, const Complex& x);
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
Classes C++ representation of an object
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Andy Wang Object Oriented Programming in C++ COP 3330
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter Structured Types, Data Abstraction and Classes
More about OOP and ADTs Classes
Lecture 4-7 Classes and Objects
Separate Compilation and Namespaces
Classes: A Deeper Look Outline
More about OOP and ADTs Classes
Learning Objectives Classes Constructors Principles of OOP
C++ Compilation Model C++ is a compiled language
Classes C++ representation of an object
What Is? function predefined, programmer-defined
Chapter 1 c++ structure C++ Input / Output
Classes and Objects Systems Programming.
Presentation transcript:

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 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 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 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 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 Will This Program Compile? b #include b #include b using namespace std; b reject main() b{b{b{b{ b chocolate plenty;

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 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 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 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 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 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 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 Class Definition b class Time { b public: b Time(); b void SetTime(int,int,int); b void PrintMilitary(); b void PrintStandard();

15 Class Definition b private: b int hours; b int minutes; b int seconds; b char ampm; b };

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