Download presentation
Presentation is loading. Please wait.
Published byChad Hancock Modified over 9 years ago
1
CS212: Object Oriented Analysis and Design Lecture 30: Namespace and Other Advanced Topics
2
Recap of Lecture 29 Function Objects Binders
3
Outline of Lecture 30 Namespace Conversion function RTTI Design by contract
4
Introduction Purpose is to localize the names of identifiers to avoid name collisions Global namespace Name defined by one library would conflict with the same name defined by the other library Namespace allows the same name to be used in different contexts without conflicts arising e.g. std
5
Namespace Fundamentals Allows you to partition the global namespace Creating a declarative region i.e. a namespace defines a scope Inside a namespace, identifiers declared within that namespace can be referred to directly Scope resolution operator to be used to refer to objects declared within a namespace from outside that namespace Demonstration: namespace1.cpp
6
The ‘using’ keyword Use of :: always is a tedious task The using statement was invented to alleviate this problem The using statement has these two general forms using namespace name; using name::member; using CounterNameSpace::lowerbound; Demonstration: namespace2.cpp
7
Unnamed Namespaces A special type of namespace, called an unnamed namespace Allows to create identifiers that are unique within a file to establish unique identifiers that are known only within the scope of a single file Outside the file, the identifiers are unknown Demonstration (file1 & file2.cpp)
8
The std namespace Standard C++ defines its entire library in its own namespace called std using namespace std; This causes the std namespace to be brought into the current namespace Direct access to the names of the functions and classes defined within the library without using std::
9
In a nutshell Standard namespace Variables Functions Classes User defined namespace Variables Functions Classes Variables Functions Classes
10
Conversion function Use an object of a class in an expression involving other types of data Overloaded operator functions can provide the means of doing this Custom conversion functions A conversion function converts your class into a type compatible with that of the rest of the expression operator type( ) { return value;} Demonstration: conv_func.cpp
11
Explicit Constructors Automatic type conversion in case of classes with single object myclass ob = 4; The explicit specifier applies only to constructors A constructor specified as explicit will only be used when an initialization uses the normal constructor syntax Demonstration (explCtor.cpp)
12
RTTI Run time type identification In non-polymorphic languages there is no need for run-time type information Since base-class pointers may be used to point to objects of the base class or any object derived from that base To obtain an object's type, use typeid Demonstrion (rtti1.cpp)
13
Design by contract A contract is a binding document that describes the responsibilities and expectations of the parties entering into the contract Interfaces between classes can be modeled in the same way This contract consists of the following conditions: Pre-conditions (calling function) Post-conditions (called function) Consistency checks (objects involve in the transaction)
14
Design by Contract: Example #include “Contract.h” Stack::Stack(int size) : _size(size) { Contract::Require(valid_size, size > 0); _representation = new int [size]; _topOfStack = 0; Contract::Invariant(ClassInvariant()); Contract::Ensure(empty_stack, IsEmpty()); }
15
Thank you Next Lecture: UML
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.