C++ User Defined Data Types
C++ allows you to use one or more data types, group them in one, to create a new data type as you see fit. This technique for grouping various values is called class. C++ proposes three techniques for defining a new kind of data: a structure, a class, and a union. They are also composite data types.
Structures
A structure is one or more variables that are considered to be a (custom) data type. To build a structure, use the struct keyword, followed by a name for the object, at least followed by a semicolon. Therefore, fundamentally, a structure can be created as:
The struct keyword is required. The name of the structure follows the same rules we have reviewed for C++ names.
The area between the braces is called the body of the structure. Within this body, you can state at least one variable that would be used to define the structure. The variable has to be declared complete with a known data type, a name, and the ending semicolon.
Here is an example:
struct students { char name[10]; //name of student int SSN; // social security number char cardtype[10]; float balance; }
Unions
A union is a user-defined data or class type that at some point contains only one object from its members (while this object may be an array or class type).
union [tag] { member-list } [declarators];
[union] tag declarators;
Parameters:-
Tag-The type name given to the union.
member-list
declarators- The Declarator list specifies the names of the union.
Declaring a Union
Begin the Union Declaration with the Union Keyword and attach the membership list in the curly braces:
Using a Union
A C++ union is a limited form in class type. It can contain access specifiers (public, protected, private), member data and member functions, including constructors and destructors. It shall not contain virtual functions or static data members. It cannot be used as a base class and cannot have any base classes. Default access of members of a union is public.
A C union type may only hold data members. In C, the keyword union should be used to declare a variable union. In C++, the union keyword is unnecessary:
A union variable may contain a value of any type declared within the union. Use the Member Selection Operator (.) to gain access to a union member:
You can declare and initialize a union in the same instruction by assigning an expression included in brackets. The expression is assessed and allocated to the first field of the union.
Example Program:
#include <iostream> using namespace std; union NumericType { int iValue; long lValue; double dValue; }; int main() { union NumericType Values = { 10 }; // iValue = 10 cout<<"%d\n"<<Values.iValue; Values.dValue = 3.1416; cout<<"%f\n"<<Values.dValue; }
Output:
Classes
In object-oriented programming language C++, the data and functions (procedures to manipulate the data) are bundled together as a self-contained unit called an object. A class is an extended concept similar to that of structure in the C programming language; this class describes the data properties alone. In the C++ programming language, the class describes both the properties (data) and behaviors (functions) of objects. Classes are not objects, but they are used to instantiate objects.
Features of Class: Classes contain data known as members and member functions. As a unit, the collection of members and member functions is an object. Therefore, this unit of objects makes up a class.
How to write a Class: In the C programming language, a structure is specified with a name. The C++ programming language extends this concept. A class is specified with a name after the keyword class. The starting flower brace symbol, { is placed at the beginning of the code. Following the flower brace symbol, the body of the class is defined with the member functions data. Then the class is closed with a flower brace symbol } and concluded with a semicolon; .
class example
{ data; member functions; …………… };
There are different access specifiers for defining the data and functions present inside a class.
Enum as Data type
Enumerated type declares a new type-name and a sequence of value containing identifiers which have values starting from 0 and incrementing by 1 every time.
Example :
enum day(mon, tues, wed, thurs, frid);
Here an enumeration of days is defined with variable d. mon will hold value 0, tue will have 1, and so on. We can also explicitly assign values, like, enum day(mon, tue=7, wed);.
Here, mon will be 0, tue is assigned 7, so wed will have value 8.