background preloader

C/C++

Facebook Twitter

Other Data Types. Type aliases (typedef / using) A type alias is a different name by which a type can be identified.

Other Data Types

In C++, any valid type can be aliased so that it can be referred to with a different identifier. In C++, there are two syntaxes for creating such type aliases: The first, inherited from the C language, uses the typedef keyword: typedef existing_type new_type_name ; where existing_type is any type, either fundamental or compound, and new_type_name is an identifier with the new name given to the type. For example: This defines four type aliases: C, WORD, pChar, and field as char, unsigned int, char* and char[50], respectively. More recently, a second syntax to define type aliases was introduced in the C++ language: For example, the same type aliases as above could be defined as: Both aliases defined with typedef and aliases defined with using are semantically equivalent.

Note that neither typedef nor using create new distinct data types. Unions declares an object (mytypes) with three members: Data Structures. Data structures data structure is a group of data elements grouped together under one name.

Data Structures

These data elements, known as members, can have different types and different lengths. Data structures can be declared in C++ using the following syntax: struct type_name { member_type1 member_name1; member_type2 member_name2; member_type3 member_name3; . . } object_names; Where type_name is a name for the structure type, object_name can be a set of valid identifiers for objects that have the type of this structure. For example: This declares a structure type, called product, and defines it having two members: weight and price, each of a different fundamental type. Right at the end of the struct definition, and before the ending semicolon (;), the optional field object_names can be used to directly declare objects of the structure type. It is important to clearly differentiate between what is the structure type name (product), and what is an object of this type (apple, banana, and melon). List (C++ Std Lib Sample)

Illustrates how to use the list::list Standard Template Library (STL) function in Visual C++. explicit list( const A& Al = A( ) ); explicit list( size_type n, const T& v = T( ), const A& Al = A( ) ); list( const list& x ); list( const_iterator First, const_iterator Last, const A& Al = A( ) ); The first constructor specifies an empty initial controlled sequence.

list (C++ Std Lib Sample)

The second constructor specifies a repetition of n elements of value x. The third constructor specifies a copy of the sequence controlled by x. The last constructor specifies the sequence [First, Last). Test: one two test: one two test: three three three test: three three. Classes (I) Classes are an expanded concept of data structures: like data structures, they can contain data members, but they can also contain functions as members.

Classes (I)

An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable. Classes are defined using either keyword class or keyword struct, with the following syntax: Where class_name is a valid identifier for the class, object_names is an optional list of names for objects of this class. Classes have the same format as plain data structures, except that they can also include functions and have these new things called access specifiers. Private members of a class are accessible only from within other members of the same class (or from their "friends").protected members are accessible from other members of the same class (or from their "friends"), but also from members of their derived classes.Finally, public members are accessible from anywhere where the object is visible.

C++

Cplusplus.com - The C++ Resources Network.