CSIS 123A Lecture 7 Static variables, destructors, & namespaces.

Slides:



Advertisements
Similar presentations
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Advertisements

Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
Review of C++ Programming Part II Sheng-Fang Huang.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
Object Oriented Programming in C++ Dr. Hammadi Nait-Charif Media School Bournemouth University
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Basic Concepts of OOP in C++ Darvay Zsolt. C++ 2 Outline  The namespace and its members  The using declaration and directive  The address operator.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Overview of Previous Lesson(s) Over View .NET Framework is a software framework developed by Microsoft that runs primarily on Microsoft Windows.  It.
C++ Memory Overview 4 major memory segments Key differences from Java
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C++ Programming Part 2 Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part.
#include guards Practical session #2 Software Engineering
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
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?
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Variables and memory addresses
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
C++ Functions A bit of review (things we’ve covered so far)
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
C++ Programming Michael Griffiths Corporate Information and Computing Services The University of Sheffield
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Lecture 3: Getting Started & Input / Output (I/O)
Chapter 15 - C++ As A "Better C"
Chapter 6 CS 3370 – C++ Functions.
Overview 4 major memory segments Key differences from Java stack
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?
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Chapter 5 Classes.
User-Defined Functions
Lecture 4-7 Classes and Objects
Overview 4 major memory segments Key differences from Java stack
Classes and Objects.
Namespaces How Shall I Name Thee?.
The Function Prototype
Java Programming Language
Submitted By : Veenu Saini Lecturer (IT)
CS410 – Software Engineering Lecture #5: C++ Basics III
Pointers and dynamic objects
Functions Reasons Concepts Passing arguments to a function
Classes Member Qualifiers
Functions Chapter No. 5.
Presentation transcript:

CSIS 123A Lecture 7 Static variables, destructors, & namespaces

Static variables Maintains its value among different function calls –Declared on heap Declared using static key word –static int myInt;

Example int main() { for(int i = 0; i < 10; i++) updateStatic(); } void updateStatic() { static in myInt = 0; cout << ++myInt << endl; } myInt retains its value through each function call. The above code will print 0 - 9

Static class variables Allows each instance of the class to share a variable. –Actually doesn’t require an instance of the class to exist. –Functions can also be static Should access through class name not instance –myClass::someFunction(); –Not myClass mc; –mc.someFunctions

More static classes you cannot initialize the static class member inside of the class. –In fact, if you decide to put your code in a header file, you cannot even initialize the static variable inside of the header file; do it in a.cpp file instead. You are required to initialize the static class member or it will not be in scope. (The syntax is a bit different: "type class_name::static_variable = value".)

Example class CSquare { public: static double Side; CSquare() {}; void setSide(double S) { Side = S; } double getSide() { return Side; } double Area() { return Side * Side; } }; double CSquare::Side = 30.65; int main() { CSquare Sqr; cout << "Square Characteristics"; cout << "\nSide: " << CSquare::Side; cout << "\nArea: " << Sqr.Area() << endl; Sqr.setSide(44.28); cout << "\nSquare Characteristics"; cout << "\nSide: " << CSquare::Side; cout << "\nArea: " << Sqr.Area(); cout << "\n"; return 0; }

Singleton Patterns Ensures that a class has only one instance –Provide a global point of access to the class Many situations in which a singleton object is necessary –a GUI application must have a single mouse –an active modem needs one and only one telephone line – an operating system can only have one window manager –A PC is connected to a single keyboard

Design Considerations Using a global object ensures that the instance is easily accessible but it doesn't keep you from instantiating multiple objects. –Our addressBook for example you can still create a local instance of the same class in addition to the global one. –Singleton patterns provides an elegant solution to this problem by making the class itself responsible for managing its sole instance. The sole instance is an ordinary object of its class, but that class is written so that only one instance can ever be created. –you provide a global point of access to that instance. hides the operation that creates the instance behind a static member function. –This member function, traditionally called Instance(), returns a pointer to the sole instance.

Example class Singleton { public: static Singleton* Instance(); Singleton(); Singleton(const Singleton&); Singleton& operator= (const Singleton&); private: static Singleton* pinstance; }; The class declares a private static pointer to its instance, pinstance When the static function Instance() is called for the first time, it creates the sole instance, assigns its address to pinstance, and returns that address. Subsequent calls, Instance() will merely return that address.

Example Continued Singleton* Singleton::pinstance = 0;// initialize pointer Singleton* Singleton::Instance () { if (pinstance == 0) // is it the first call? { pinstance = new Singleton; // create sole instance } return pinstance; // address of sole instance } Singleton::Singleton() { //... perform necessary instance initializations }

Inline Functions Treated like a macro –Code substitution happens (Function not called) Compiler will replace function call with actual code for the function Many compilers will decide –If inline code is too big, compiler will not make it inline Functions are declared with the keyword inline –inline int add(int x,int y)

More Inline Inline functions should be used for small pieces of code Advantage is code executes faster Disadvantage: makes your code larger inline int add(int x,int y) { return x+y; }

Destructors The opposite of a constructor –Are less complicated than constructors –You don't call them explicitly (they are called automatically for you) There's only one destructor for each object. –The name of the destructor is the name of the class, preceeded by a tilde (~). –called when the object of a class goes out of scope

Example class Example { private: int x; //Data member int y; // Data member public: Example() //Constructor for the C++ tutorial { x = 0; y = 0; } ~Example() //destructor { } int Add() { return x+y; } };

Namespaces Allow you to group a set of global classes, objects and/or functions under a name. –They serve to split the global scope in sub- scopes known as namespaces –namespaces use the following form: namespace identifier { namespace-body }

Namespaces II Where identifier is any valid identifier and namespace-body is the set of classes, objects and functions that are included within the namespace. namespace general { int a, b; }

Namespaces III a and b are normal variables integrated within the general namespace. – In order to access these variables from outside the namespace you have to use the scope operator ::. –general::a general::b

Namespaces IV Useful in case there is a possibility that a global object or function has the same name as another one, – causing a redefinition error. For example:

Example // namespaces #include using namespace std; namespace first { int var = 5; } namespace second { double var = ; } int main () { cout << first::var << endl; cout << second::var << endl; return 0; }

Using Namespaces The using directive followed by namespace serves to associate the present nesting level with a certain namespace so that the objects and functions of that namespace can be accesible directly as if they were defined in the global scope. Its utilization follows this prototype: using namespace identifier ;

Example using // using namespace example #include using namespace std; namespace first { int var = 5; } namespace second { double var = ; } int main () { using namespace second; cout << var << endl; cout << (var*2) << endl; return 0; }

More Namespace You have to consider that the sentence using namespace has validity only in the block in which it is declared (block scope) Used in the global scope. For example, if we had intention to first use the objects of a namespace and then those of another one we could do something similar to the next example

#include using namespace std; namespace first { int var = 5; } namespace second { double var = ; } int main () { { using namespace first; cout << var << endl; } { using namespace second; cout << var << endl; } return 0; }

Alias Definition You can also define alternative names for namespaces that already exist. It is done as follows namespace new_name = current_name ;

Namespace std The use of std is a recent addition to the language. –It is almost as recent as the ANSI standard itself (1997) and many older compilers do not comply with this rule. –Almost all compilers allow the use of the traditional header files (like iostream.h, stdlib.h, etc), Can be used just as you would namespaces. The ANSI standard has completely redesigned these libraries taking advantage of the templates feature and following the rule to declare all the functions and variables under the namespace std.

std II The standard has specified new names for these "header" files. –Basically using the same name for C++ specific files, but without the ending.h. iostream.h becomes iostream If we use the ANSI-C++ compliant include files we have to bear in mind that all the functions, classes and objects will be declared under the std namespace.

Example std // ANSI-C++ compliant hello world #include int main () { std::cout << "Hello world in ANSI-C++\n"; return 0; } // ANSI-C++ compliant hello world (II) #include using namespace std; int main () { cout << "Hello world in ANSI-C++\n"; return 0; }

Command Line Arguments Allows you to pass information into program through main –Two arguments to main Int main(int argc, char argv[][]) or Int main(int argc, char **argv) –argc is the argument count It starts at one. argc == 0 would mean no arguments –argv is an array of cstrings (2D array of chars) Contains information passed argv[0] is name of program

Parsing Command Line Arguments Passing data is simple –From command line cmdArgs arg1 arg2 argv[0] == “cmdArgs.exe” argv[1] == “arg1” argv[2] == “arg2” // cmdArgs.exe #include using namespace std; int main(int argc, char **argv) { cout << "argc = " << argc << endl; for(int i = 0; i < argc; i++) cout << "argv[" << i << "] = " << argv[i] << endl; return 0; }