3. Primitive Data Types in C++:
• Without data type, there is no use in writing
the programs. So let us learn what are the
data types. Here, we will also learn, how to
use them by declaring the variables. So let us
overview all the data types available in C++.
4. • The primitive data types are of three types in
categorization.
• Integral type, which means there is no decimal
point.
• Boolean means true or false next,
• Floating-point with the decimal point.
• So, this is the broader categorization, under
integral we have int and char.
5.
6. User-Defined Data Types
The data types that are defined by the user are
called the derived data type or user-defined
derived data type. These types include:
•Class
•Structure
•Union
•Enumeration
•Typedef
7. 1. Class
• A Class is the building block of C++ that leads to
Object-Oriented programming is a Class. It is a
user-defined data type, which holds its own data
members and member functions, which can be
accessed and used by creating an instance of that
class. A class is like a blueprint for an object.
9. 2. Structure
• A Structure is a user-defined data type in C/C++. A
structure creates a data type that can be used to group
items of possibly different types into a single type.
• Syntax
struct structName{
char varName[size];
int varName;
};
10. 3. Union
• Like Structures , Union a user-defined data type. In union,
all members share the same memory location. For example
in the following C program, both x and y share the same
location. If we change x, we can see the changes being
reflected in y.
• Syntax
Union_Name
{
// Declaration of data members
}; union_variables;
11. 4. Enumeration
• Enumeration (or enum) is a user-defined data type in
C. It is mainly used to assign names to integral
constants, the names make a program easy to read and
maintain.
Syntax
• enum nameOfEnum {
varName1 = 1, varName2 = 0
};
12. 5. Typedef
• C++ allows you to define explicitly new data type names by using
the keyword typedef. Using typedef does not create a new data
class, rather it defines a name for an existing type. This can increase
the portability(the ability of a program to be used across different
types of machines; i.e., mini, mainframe, micro, etc; without many
changes to the code)of a program as only the typedef statements
would have to be changed. Using typedef one can also aid in self-
documenting code by allowing descriptive names for the standard
data types.
• Syntax
typedef typeName;
13. Derived Data Types in C++
• The data types that are derived from the
primitive or built-in data types are referred to as
Derived Data Types. These can be of four types
namely:
• Function
• Array
• Pointers
• References
14. 1. Function
• A Function is a block of code or program segment that is
defined to perform a specific well-defined task. A function
is generally defined to save the user from writing the same
lines of code again and again for the same input. All the
lines of code are put together inside a single function and
this can be called anywhere required. main() is a default
function that is defined in every program of C++.
• Syntax
FunctionType FunctionName(parameters)
15. 2. Array
• An Array is a collection of items stored at
continuous memory locations. The idea of
array is to represent many instances in one
variable.
• Syntax
DataType ArrayName[size_of_array];
16. 3. Pointers
• Pointers are symbolic representation of
addresses. They enable programs to simulate call-
by-reference as well as to create and manipulate
dynamic data structures. It’s general declaration
in C/C++ has the format:
• Syntax
datatype *var_name;
17. 4. Reference
• When a variable is declared as reference, it
becomes an alternative name for an existing
variable. A variable can be declared as
reference by putting ‘&’ in the declaration.
• Syntax
data_type &ref = variable;