210 likes | 400 Views
C++ Programming. Jerry Lebowitz. Chapter 7. Topics. Enums Typedefs String datatype Namespaces . C++ Data Types. floating. address. float double long double. pointer reference. simple. structured. integral enum. array struct union class.
E N D
C++ Programming Jerry Lebowitz
Topics • Enums • Typedefs • String datatype • Namespaces
C++ Data Types floating address float double long double pointer reference simple structured integral enum array struct union class char short int long bool
Enumerated Types • Used to increase readability and maintainability • Enumerated types are used to declare a set of integer constants • Syntax: enum [tag] {comma separated list of identifiers} [variable-list]; enum trees { oak, maple, cherry };// no variables declared yet enum trees myTree; // declares a variable of type enum trees • See example enum1.cpp
Typedefs • Used to associate an identifier with a type (no storage allocated) • Enhances readability and maintainability • Allows programmers to use types that are appropriate to the application • Syntax: • typedef oldType newType • Example: typedef int color; // color is now a type color red, white, blue • See example enum1.cpp
String Class • String class • A class supplied by many compiler vendors • Not part of the language • Need to #include <string> • Cannot always be used (open of files requires a C string) • Using the String class can eliminate many of the problems associated with Cstrings • Has over 100 members • Memory is dynamically allocated when needed • Many operators are overloaded • + << >> [ ] • Has a default constructor that initializes a string object to A NULL string • Has another constructor that takes a parameter, creates a string object, and sets it to the parameter • Boundary checking member function available
String Class • Can create an array of string variables • String myStrings[30]; • myStrings[2] = “Hello World” ; // accesses third string • (See examples: string1.cpp through string3.cpp)
Scope Resolution Operator • The scope operator :: also allows access to global variables even though there are local variable of the same name • It is not a good idea to use global variables, but one should understand this construct to understand someone else’s code • Note: local variables take precedence over global variables • The scope operator :: is also used with namespaces and C++ classes • (See examples: namespace1.cpp)
Namespaces • When a header file, such as iostream, is included in a program, the global identifiers in the header file also become the global identifiers in the program • If a global identifier in a program has the same name as one of the global identifiers in the header file, the compiler will generate syntax error (such as identifier redefined) • The same problem can occur if a program uses third party libraries • ANSI/ISO standard C++ attempts to solve this problem of overlapping global identifier names with the namespace mechanism
More on Namespaces • A namespaceis a space (that is a section of code) which is enclosed by braces • Includes only definitions or other namespaces • Scoping mechanism that is used to identify variables clearly within different blocks of code • The general idea of namespaces is to declare identifiers with namespace scope • The construct #include <iostream.h> is not compliant with the ISO/ANSI standard • For pre ISO/ANSI programs, compilers allow one to include <iostream.h> and have the directive “using namespace std” for compatibility • The header files (such as <iostream>) declare all their identifiers to be in a namespace called std
Syntax of a Namespace namespace nameSpaceName { [declarations of members] } • where a member is usually a variable declaration, a named constant, a function, or another namespace
Create a namespace namespace globalType { const int n = 10; const double rate = 7.50; int count = 0; void printResult(); } defines globalType to be a namespace with four members
Scope of a namespace member • The scope of a namespace member is local to the namespace • There are two ways a namespace member can be accessed outside the namespace
First Way to Access Namespace Identifiers • Use a qualified name consisting of the namespace, the scope resolution operator :: and the desired the identifier • Syntax: Accessing a namespace Member • namespace_name::identifier • To access the member rate of the namespace globalType, the following statement is required: • globalType::rate • To access the member printResult (which is a function), the following statement is required: • globalType::printResult( ); • See example (namespace2.cpp)
First Way to Access Namespace Identifiers • To access members within iostream std::cout << std :: endl ; ios::scientific • See example (namespace3.cpp)
Utilizing Two namespaces namespace firstNameSpace { int nameSpaceVariable=5; } namespace secondNameSpace { int nameSpaceVariable=10; } // utilizing namespace variables firstNameSpace::nameSpaceVariable secondNameSpace::nameSpaceVariable • See example (namespace4.cpp)
Second Way to Access Namespace Identifiers • Using a declaration • Syntax: • (a) using namespace namespaceMember; • or • (b) To simplify the accessing of a specific namespace member: • using namespaceName:: namespaceMember; • For globalType namespace: • using namespace globalType; • or • using globalType::rate; • After the using statement • To access a namespace member it is not necessary to precede the namespace_name and the scope resolution operator before the namespace member
“using” example namespace globalType { const int n = 10; const double rate = 7.50; int count = 0; void printResult(); } using namespace globalType; See examples namespace5.cpp and namespace6.cpp
“using” examples for iostream • For iostream • #include <iostream> • using std::cout ; • using std::endl ; • using namespace std; • A function can refer to the global identifiers of the header file iostream without using the prefix std:: before the identifier name • If a namespace member and a global identifier in a program have the same name • To access this namespace member in the program, the namespace_name and the scope resolution operator must precede the namespace member • See examples namespace7.cpp through namespace10.cpp
Another namespace Example #include <iostream> using namespace std; int myVariable=7; namespace myNameSpace { char myVariable='t'; } using namespace myNameSpace; int main() { double myVariable=12.0; cout << "myVariable defined in main " << myVariable << endl; cout << "global myVariable " << ::myVariable << endl; cout << "myVariable defined in myNameSpace " << myNameSpace::myVariable • See example namespace11.cpp