Professional Documents
Culture Documents
CP 111 Lecture 4 - DATA TYPES
CP 111 Lecture 4 - DATA TYPES
CP 111
Recap
What is a variable?
Every variable should get some space in the memory; otherwise how can you use
it for computation?
All variables use data-type during declaration to restrict the type of data to be
stored
Observation: compiler must know the data type for size mapping
What is a Data Type
Data types are means to identify the type of data and associated operation of
handling it
● Integer
● Character
● Boolean
● Floating Point
● Double Floating Point
● Valueless or Void
● Wide Character
Derived Data Type
The data-types that are derived from the primitive or built-in datatypes are referred
to as Derived Data Types. These can be of four types namely:
● Function
● Array
● Pointer
● Reference
User Defined Data Type
These data types are defined by user itself. Like, defining a class in C++ or a
structure. C++ provides the following user-defined datatypes:
● Class
● Structure
● Union
● Enumeration
● Typedef defined DataType
primitive data types available in C++
Integer: Keyword used for integer data types is int.
Write a simple code in c++ that assign value to a integer data type and display it.
primitive data types available in C++
Character: Character data type is used for storing characters.
Characters typically requires 1 byte of memory space and ranges from -128 to 127
or 0 to 255.
Write a simple code in c++ that assign value to a character data type and display
it.
primitive data types available in C++
Boolean: Boolean data type is used for storing boolean or logical values.
Write a simple code in c++ that assign value to a boolean data type and display it.
primitive data types available in C++
Floating Point: Floating Point data type is used for storing single precision floating
point values or decimal values.
Write a simple code in c++ that assign value to a float data type and display it.
primitive data types available in C++
Double Floating Point: Double Floating Point data type is used for storing double
precision floating point values or decimal values.
Write a simple code in c++ that assign value to a double data type and display it.
primitive data types available in C++
Void: Void means without any value. void data type represents a valueless entity.
Void data type is used for those function which does not returns a value.
Wide Character: Wide character data type is also a character data type but this
data type has size greater than the normal 8-bit data type.
The basic data type has modifiers preceding them .we use modifier to alter the
meaning of the base type to fit various situation more precisely.
● Arrays.
● Functions.
● Pointers.
● References.
● Constant.
Pointers
Pointers are symbolic representation of addresses
Syntax:
datatype *var_name;
int *ptr; //ptr can point to an address which holds int data
The reason we associate data type to a pointer is that it knows how many bytes
the data is stored in.
Pointers
● Define a pointer variable
● Assigning the address of a variable to a pointer using unary operator (&)
which returns the address of that variable.
● Accessing the value stored in the address using unary operator (*) which
returns the value of the variable located at the address specified by its
operand.
}
Pointers
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)
Why do we need functions?
Functions help us in reducing code redundancy. If functionality is performed at
multiple places in software, then rather than writing the same code, again and
again, we create a function and call it everywhere. This also helps in maintenance
as we have to change at one place if we make future changes to the functionality.
Functions make code modular. Consider a big file having many lines of codes. It
becomes really simple to read and use the code if the code is divided into
functions.
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20;
int m = max(a, b); // Calling above function to find max of 'a' and 'b'
cout << "m is " << m;
return 0;
}
How to use a function
There are two main approaches of using a function in your program.
Approach I
Approach II
Below are an example of function declarations. (parameter names are not there in
below declarations)
Exercise;
Write a function declaration where by the function is called display, has one
parameter is a string and returns no value. (3 minutes).
Write A function that takes a int pointer and an int variable as parameters and
returns an pointer of type int (discuss with your neighbour)
How to define a function
How to call a function
A function can be called or used in the main function or in another function.
You use the same format as the declaration but this time you pass actual values.
The parameters passed to function are called actual parameters. For example, in
the above program 10 and 20 are actual parameters.
The parameters received by function are called formal parameters. For example,
in the above program x and y are formal parameters.
There are two most popular ways to pass parameters.
Pass by Value: In this parameter passing
method, values of actual parameters are
copied to function’s formal parameters and
the two types of parameters are stored in
different memory locations.
So any changes made
inside functions are not reflected in
actual parameters of caller.
hey are used to store similar type of elements as in the data type must be the
same for all elements
Why do we need arrays?
We can use normal variables (v1, v2, v3, ..) when we have a small number of
objects, but if we want to store a large number of instances, it becomes difficult to
manage them with normal variables. The idea of an array is to represent many
instances in one variable.
Array declarations
There are various ways in which we can declare an array. It can be done
int arr1[10];
int n = 10;
int arr2[n];
Array declaration by initializing elements
// elements