Data Types in C++-Primary or Built-in or Fundamental data type Derived data types User-defined data types
ssuser5610081
386 views
17 slides
Feb 29, 2024
Slide 1 of 17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
About This Presentation
Data Types in C++
Size: 142.45 KB
Language: en
Added: Feb 29, 2024
Slides: 17 pages
Slide Content
Data Types in C++ Dr. A.J.Chinchawade Sharad Institute of Technology College of Engineering Yadrav .
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++.
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 .
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
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.
Syntax
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 ; };
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 ;
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 };
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 ;
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
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)
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 ];
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 ;
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;