UDTs (User-Defined Types), Namespaces, and string Type

UDTs (User-Defined Types):

A data type is a set of values together with a set of operations on those values. User-defined types are collections of data, which describe an object's attributes and state. In C++, there are many examples of objects, including user-defined variables. Every object has a type, like variables of native types (short, char, float, etc.). For example, a class can be defined called MyClass, then objects can be instantiated of type MyClass, like native variables can be of type short, char, etc. However, since the compiler has no fundamental understanding of user-defined types, programmers have to define these data types.

User-Defined Types


Class: Review Chapter 9 of the text book for a more detailed review of classes.

A class specified by the keyword class, is a user defined type that contains both data members and member functions...

Simple Class Example:
To define and declare a class within one file. The following is an example only and is not a practical use of a class.
(The section beginning with Reusability demonstrates how to separate the header file and source code) Reusability:

Enumeration Data Type

Enumeration Data Type:

An enumeration, specified by the keyword enum, is a set of integer constants associated by identifiers--called enumerators. Enumerations provide a manner to implement names (or identifiers), in place of integer constants. Enumerator values begin at zero (0), if a value for the initial enumerator was not provided. Enumerators may be used wherever an int value is utilized. If no user specified value is assigned, compilers will assign the following integer value after the integer value assigned to the preceding enumerator.

Using Enumeration Data Type:
To define enumeration data type:
  1. Name for data type
  2. Values for data type
  3. Operations on values (C++ does not allow users to create operations for enumeration type)
Creating Enumeration Data Type:
Declaring Variables: Assignment: Operations: Operations and Input/Output: Functions and Enumeration Types: Anonymous Data Types: Anonymous Data Type Issues: Summary of enumeration type:
//This program demos enumeration data types

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

//initialize constants

//initialize global variables

//prototype functions

int main()
  //initialize local variables

  enum bookType {Math, CSC, English, History, Physics, Philosophy};

  bookType book;

  book = English;
  cout<<"book = "<< book <<" (ONLY outputs DEFAULT value DIRECTLY.)" << endl;

  book = static_cast<bookType>(book + 1);
  cout<<"book = "<< book <<" (increment, still ONLY outputs DEFAULT value DIRECTLY.)" << endl;

  //must decrement for next block of code to work properly...
  book = static_cast<bookType>(book - 1);
  cout<<"The enumeration value currently selected is ";

  switch (book)
    case Math:  cout << "Math";
    case CSC:  cout << "CSC";
    case English: cout << "English";
    case History: cout << "History";
    case Physics: cout << "Physics";
    case Philosophy: cout << "Philosophy";

  cout << "\nPress Enter key to exit...";
  cin.get();  // make DOS window stay open, sometimes...

  return 0;
Sample Run:
book = 2 (ONLY outputs DEFAULT value DIRECTLY.)
book = 3 (increment, still ONLY outputs DEFAULT value DIRECTLY.)
The enumeration value currently selected is English
Press Enter key to exit...
Enumeration Program

typedef Statement

Using typedef Statement:

As a shortcut to long, repetitious variable declarations, and as an error preventative measure, C++ allows the creation of an alias or synonym--by using the keyword typedef, which stands for type definition. Type definitions are also useful when data types may change in later revisions of programs. With a type definition, a synonym or alias is created. Realize that typedef does not create a new type--it is important to understand the distinction. The syntax for a type definition is typedef, followed by the existing type, then the alias or synonym, ending with a semicolon. For example:

//typedef defined...
//USHORT synonym for unsigned short int
typedef unsigned short int USHORT;
The typedef definition creates an alias or synonym called USHORT that can be used rather than unsigned short int:
//data type of weight is unsigned short int
USHORT weight = 160;
Summary of typedef Definitions:

ANSI/ISO Standard C++

ANSI/ISO Standard C++:

ANSI/ISO standard C++ was officially approved in July 1998.


Using Namespaces:

ANSI/ISO Standard C++ has some features not available in Standard C++. ANSI/ISO Standard C++ (July, 1998) included the namespace mechanism--Standard C++ did not have it. For example, when a header file, such as iostream, is included in a program, global identifiers in the header file also become 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 a syntax error (such as identifier redefined). The same problem can occur if a program uses libraries from other compiler vendors. To overcome this problem, these third party compiler vendors usually begin their global identifiers with a special symbol--for example, an underscore character (_). To avoid problems associated with identifier conflicts and linking errors, it is a good policy not to begin identifier names with the underscore character (_) even though it is permissable. ANSI/ISO standard C++ uses namespaces to assist with the problem of overlapping global identifier names.

ANSI/ISO Standard C++ resolves the problem of overlapping global identifier names with the namespace mechanism.

The scope of a namespace member is local to the namespace. Though, a namespace member can be accessed outside the namespace in one of three ways:

Each time it is accessed:
1) namespace_name::identifier

Simplified (place using statement after the namespace declaration):
2) using namespace namespace_name (access all namespace members)
3) using namespace namespace_name::identifier (access specific namespace member)
After the using statement, you do not have to put the namespace_name and the scope resolution operator before the namespace member to access a namespace member.
Syntax of namespace Statement:
namespace namespace_name
Accessing namespace Member: Using using Statement:

string Data Type

Using string Data Type:

length() Function:
string firstName;
string name;
string str;

firstName = "John";
name = firstName + " Doe";
str = "Hello World.";

cout << firstName.length() << endl; //prints 4
cout << name.length() << endl; //prints 8
cout << str.length() << endl; //prints 12
size() Function: find() Function:
string str = "It is sunny, isn't it?";

cout << str.find("is") << endl; //prints 3
cout << str.find("s") << endl; //prints 4
cout << str.find("it") << endl; //prints 19
substr() Function:
string str = "It is sunny, isn't it?";

cout << str.substr(0, 5) << endl; //prints It is
cout << str.substr(6, 5) << endl; //prints sunny
cout << str.substr(3, 2) << endl; //prints is
swap() Function: