SlideShare a Scribd company logo
UNIT – IV POINTERS
Pointer is a variable in C++ that holds the address of another variable. They have data type just like variables,
for example an integer type pointer can hold the address of an integer variable and an character type pointer
can hold the address of char variable.
Syntax of pointer - data_type *pointer_name;
How to declare a pointer?
/* This pointer p can hold the address of an integer variable, here p is a pointer and var is just a * simple
integer variable */ int *p, var
Assignment
As I mentioned above, an integer type pointer can hold the address of another int variable. Here we have an
integer variable var and pointer p holds the address of var. To assign the address of variable to pointer we use
ampersand symbol (&).
/* This is how you assign the address of another variable to the pointer*/ p = &var;
Defining a Pointer of Class type
We can define pointer of class type, which can be used to point to class objects.
class Simple
{
public:
int a;
};
int main()
{
Simple obj;
Simple* ptr; // Pointer of class type
ptr = &obj;
cout << obj.a;
cout << ptr->a; // Accessing member with pointer
}
Here you can see that we have declared a pointer of class type which points to class's object. We can access data members
and member functions using pointer name with arrow -> symbol.
Pointer to Data Members of Class
We can use pointer to point to class's data members (Member variables).
Syntax for Declaration - datatype class_name :: *pointer_name;
Syntax for Assignment - pointer_name = &class_name :: datamember_name;
Both declaration and assignment can be done in a single statement too.
datatype class_name::*pointer_name = &class_name::datamember_name ;
Using Pointers with Objects
For accessing normal data members we use the dot . operator with object and -> qith pointer to object. But when we have a
pointer to data member, we have to dereference that pointer to get what its pointing to, hence it becomes,
Object.*pointerToMember
and with pointer to object, it can be accessed by writing,
ObjectPointer->*pointerToMember
Lets take an example, to understand the complete concept.
class Data
{
public:
int a;
void print()
{
cout << "a is "<< a;
}
};
int main()
{
Data d, *dp;
dp = &d; // pointer to object
int Data::*ptr=&Data::a; // pointer to data member 'a‘
d.*ptr=10;
d.print();
dp->*ptr=20;
dp->print();
}
OUTPUT:
a is 10
a is 20
This Pointer:
C++, this pointer is used to represent the address of an object inside a member
function.
For example, consider an object obj calling one of its member function say method() as
obj.method(). Then, this pointer will hold the address of object obj inside the member function
method(). The this pointer acts as an implicit argument to all the member functions.
class ClassName {
private:
int dataMember;
public:
method(int a) {
// this pointer stores the address of object obj and access dataMember
this->dataMember = a;
... .. ...
}
}
int main() {
ClassName obj;
obj.method(5);
... .. ...
}
C++ ARRAYS:
•C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type.
An array is used to store a collection of data, but it is often more useful to think of an array as a collection of
variables of the same type.
•Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array
variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A
specific element in an array is accessed by an index.
•All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the
highest address to the last element.
Declaring Arrays
•To declare an array in C++, the programmer specifies the type of the elements and the number of elements required
by an array as follows − type arrayName [ arraySize ];
•This is called a single-dimension array. The arraySize must be an integer constant greater than zero and type can be
any valid C++ data type. For example, to declare a 10-element array called balance of type double, use this statement
− double balance[10];
Initializing Arrays
•double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
•The number of values between braces { } can not be larger than the number of elements that we declare for the array
between square brackets [ ]. Following is an example to assign a single element of the array −
•If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write
− double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};
•You will create exactly the same array as you did in the previous example. balance[4] = 50.0;
•The above statement assigns element number 5th in the array a value of 50.0. Array with 4th index will be 5th, i.e.,
last element because all arrays have 0 as the index of their first element which is also called base index. Following is
the pictorial representaion of the same array we discussed above −Array Presentation, Accessing Array Elements
•An element is accessed by indexing the array name. This is done by placing the index of the element within square
brackets after the name of the array. For example −double salary = balance[9];
•The above statement will take 10th element from the array and assign the value to salary variable. Following is an
example, which will use all the above-mentioned three concepts viz. declaration, assignment and accessing arrays −
#include <iostream>
using namespace std;
#include <iomanip>
using std::setw;
int main () {
int n[ 10 ]; // n is an array of 10 integers
// initialize elements of array n to 0
for ( int i = 0; i < 10; i++ ) {
n[ i ] = i + 100; // set element at location i to i + 100
}
cout << "Element" << setw( 13 ) << "Value" << endl;
// output each array element's value
for ( int j = 0; j < 10; j++ ) {
cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl;
}
return 0;
}
•This program makes use of setw() function to format the output. When the above code is compiled and executed, it
produces the following result −
•Arrays in C++
•Arrays are important to C++ and should need lots of more detail. There are following few important concepts, which
should be clear to a C++ programmer −
•1 Multi-dimensional arrays -C++ supports multidimensional arrays. The simplest form of the multidimensional
array is the two-dimensional array.
•2Pointer to an array -You can generate a pointer to the first element of an array by simply specifying the array
name, without any index.
•3Passing arrays to functions - You can pass to the function a pointer to an array by specifying the array's name
without an index.
Array class in C++
Array classes are generally more efficient, light-
weight and reliable than C-style arrays.
Operations on array :-
1. at() :- This function is used to access the elements
of array.
2. get() :- This function is also used to access the
elements of array. This function is not the member of
array class but overloaded function from class tuple.
3. operator[] :- This is similar to C-style arrays. This
method is also used to access array elements.
Memory model
Defines the semantics of computer memory storage for the purpose of the C++ abstract
machine.
The memory available to a C++ program is one or more contiguous sequences of bytes.
Each byte in memory has a unique address.
Byte
A byte is the smallest addressable unit of memory. It is defined as a contiguous sequence
of bits, large enough to hold the value of any UTF-8 code unit (256 distinct values) and of (since C++14)any
member of the basic execution character set (the 96 characters that are required to be
single-byte). Similar to C, C++ supports bytes of sizes 8 bits and greater.
The types char, unsigned char, and signed char use one byte for both storage and value
representation. The number of bits in a byte is accessible as CHAR_BIT or
std::numeric_limits<unsigned char>::digits.
Memory location
A memory location is
•an object of scalar type (arithmetic type, pointer type, enumeration type, or
std::nullptr_t)
•or the largest contiguous sequence of bit fields of non-zero length
Note: Various features of the language, such as references and virtual functions, might
involve additional memory locations that are not accessible to programs but are
managed by the implementation.
NEW & DELETE OPERATOR
C++ supports these functions new and delete to perform allocating and freeing the memory.
New operator - The new operator denotes a request for memory allocation on the Heap. If
sufficient memory is available, new operator initializes the memory and returns the address of
the newly allocated and initialized memory to the pointer variable.
Syntax to use new operator: To allocate memory of any data type, the syntax is:
pointer-variable = new data-type;
Here, pointer-variable is the pointer of type data-type. Data-type could be any built-in data type
including array or any user defined data types including structure and class
Initialize memory: We can also initialize the memory using new operator:
pointer-variable = new data-type(value);
Example: int *p = new int(25); float *q = new float(75.25);
Allocate block of memory: new operator is also used to allocate a block(an array) of memory of
type data-type.pointer-variable = new data-type[size]; where size(a variable) specifies the
number of elements in an array.
Example: int *p = new int[10] Dynamically allocates memory for 10 integers continuously of
type int and returns pointer to the first element of the sequence, which is assigned to p(a
pointer). p[0] refers to first element, p[1] refers to second element and so on.
Delete operator - Since it is programmer’s responsibility to deallocate dynamically allocated
memory, programmers are provided delete operator by C++ language. Syntax:
// Release memory pointed by pointer-variable delete pointer-variable; Here, pointer-variable is
the pointer that points to the data object created by new.
Examples:
delete p; delete q;
DYNAMIC OBJECT
When a C++ object is created, two events occur:
Storage is allocated for the object.
The constructor is called to initialize that storage.
By now you should believe that step two always happens. C++ enforces it because uninitialized
objects are a major source of program bugs. It doesn’t matter where or how the object is
created – the constructor is always called.
Storage can be allocated before the program begins, in the static storage area. This storage
exists for the life of the program.
Storage can be created on the stack whenever a particular execution point is reached (an
opening brace). That storage is released automatically at the complementary execution point
(the closing brace). These stack-allocation operations are built into the instruction set of the
processor and are very efficient. However, you have to know exactly how many variables you
need when you’re writing the program so the compiler can generate the right code.
Storage can be allocated from a pool of memory called the heap (also known as the free store).
This is called dynamic memory allocation. To allocate this memory, a function is called at
runtime; this means you can decide at any time that you want some memory and how much
you need. You are also responsible for determining when to release the memory, which means
the lifetime of that memory can be as long as you choose – it isn’t determined by scope.
Often these three regions are placed in a single contiguous piece of physical memory: the static
area, the stack, and the heap (in an order determined by the compiler writer). However, there
are no rules. The stack may be in a special place, and the heap may be implemented by making
calls for chunks of memory from the operating system. As a programmer, these things are
normally shielded from you, so all you need to think about is that the memory is there when
BINDING
Binding refers to the process of converting identifiers (such as variable and
performance names) into addresses. Binding is done for each variable and
functions. For functions, it means that matching the call with the right function
definition by the compiler. It takes place either at compile time or at runtime.
Early Binding (compile-time time polymorphism) As the name indicates,
compiler (or linker) directly associate an address to the function call. It replaces
the call with a machine language instruction that tells the mainframe to leap to
the address of the function.
Late Binding : (Run time polymorphism) In this, the compiler adds code that
identifies the kind of object at runtime then matches the call with the right
function definition (Refer this for details). This can be achieved by declaring
a virtual function.
POLYMORPHISM
Polymorphism is a feature of OOPs that allows the object to behave differently in
different conditions. In C++ we have two types of polymorphism:
1) Compile time Polymorphism – This is also known as static (or early) binding.
2) Runtime Polymorphism – This is also known as dynamic (or late) binding.
1) Compile time Polymorphism
Function overloading and Operator overloading are perfect example of Compile time
polymorphism.
Compile time Polymorphism Example
In this example, we have two functions with same name but different number of
arguments. Based on how many parameters we pass during function call determines
which function is to be called, this is why it is considered as an example of
polymorphism because in different conditions the output is different. Since, the call is
determined during compile time thats why it is called compile time polymorphism.
2) Runtime Polymorphism
Function overriding is an example of Runtime polymorphism.
Function Overriding: When child class declares a method, which is already present in
the parent class then this is called function overriding, here child class overrides the
parent class.In case of function overriding we have two definitions of the same
function, one is parent class and one in child class. The call to the function is
determined at runtime to decide which definition of the function is to be called, thats
the reason it is called runtime polymorphism.
VIRTUAL FUNCTION
C++ virtual function is a member function in the base class that you redefine in a derived class. It
is declared using the virtual keyword.
It is used to tell the compiler to perform dynamic linkage or late binding on the function.
There is a necessity to use the single pointer to refer to all the objects of the different classes.
So, we create the pointer to the base class that refers to all the derived objects. But, when base
class pointer contains the address of the derived class object, always executes the base class
function. This issue can only be resolved by using the 'virtual' function.
A 'virtual' is a keyword preceding the normal declaration of a function.
When the function is made virtual, C++ determines which function is to be invoked at the
runtime based on the type of the object pointed by the base class pointer.
Late binding or Dynamic linkage
In late binding function call is resolved during runtime. Therefore compiler determines the type
of object at runtime, and then binds the function call.
Rules of Virtual Function
Virtual functions must be members of some class.
Virtual functions cannot be static members.
They are accessed through object pointers. & They can be a friend of another class.
A virtual function must be defined in the base class, even though it is not used.
The prototypes of a virtual function of the base class and all the derived classes must be
identical. If the two functions with the same name but different prototypes, C++ will consider
them as the overloaded functions.
We cannot have a virtual constructor, but we can have a virtual destructor
Consider the situation when we don't use the virtual keyword.
THANK YOU!....

More Related Content

Similar to C++ - UNIT_-_IV.pptx which contains details about Pointers

ch06.ppt
ch06.pptch06.ppt
ch06.ppt
AqeelAbbas94
 
array Details
array Detailsarray Details
array Details
shivas379526
 
Array
ArrayArray
Array
hjasjhd
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
TaseerRao
 
Array assignment
Array assignmentArray assignment
Array assignment
Ahmad Kamal
 
Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
Shri Shankaracharya College, Bhilai,Junwani
 
Lecture 8
Lecture 8Lecture 8
Lecture 8
Mohammed Saleh
 
Array and Pointers
Array and PointersArray and Pointers
Array and Pointers
Prof Ansari
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
Pointers
PointersPointers
Pointers
Frijo Francis
 
Module7
Module7Module7
Module7
Seid Hussein
 
Chapter-Five.pptx
Chapter-Five.pptxChapter-Five.pptx
Chapter-Five.pptx
berekethailu2
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
Rakesh Roshan
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
MrMaster11
 
C++ lecture 04
C++ lecture 04C++ lecture 04
C++ lecture 04
HNDE Labuduwa Galle
 
Learn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & ArraysLearn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & Arrays
Eng Teong Cheah
 
Arrays
ArraysArrays
Arrays
ViniVini48
 
Arrry structure Stacks in data structure
Arrry structure Stacks  in data structureArrry structure Stacks  in data structure
Arrry structure Stacks in data structure
lodhran-hayat
 
c++ arrays and pointers grade 9 STEP curriculum.pptx
c++ arrays and pointers grade 9 STEP curriculum.pptxc++ arrays and pointers grade 9 STEP curriculum.pptx
c++ arrays and pointers grade 9 STEP curriculum.pptx
JanineCallangan
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
Mohammed Khan
 

Similar to C++ - UNIT_-_IV.pptx which contains details about Pointers (20)

ch06.ppt
ch06.pptch06.ppt
ch06.ppt
 
array Details
array Detailsarray Details
array Details
 
Array
ArrayArray
Array
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
 
Array assignment
Array assignmentArray assignment
Array assignment
 
Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
 
Lecture 8
Lecture 8Lecture 8
Lecture 8
 
Array and Pointers
Array and PointersArray and Pointers
Array and Pointers
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Pointers
PointersPointers
Pointers
 
Module7
Module7Module7
Module7
 
Chapter-Five.pptx
Chapter-Five.pptxChapter-Five.pptx
Chapter-Five.pptx
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
 
C++ lecture 04
C++ lecture 04C++ lecture 04
C++ lecture 04
 
Learn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & ArraysLearn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & Arrays
 
Arrays
ArraysArrays
Arrays
 
Arrry structure Stacks in data structure
Arrry structure Stacks  in data structureArrry structure Stacks  in data structure
Arrry structure Stacks in data structure
 
c++ arrays and pointers grade 9 STEP curriculum.pptx
c++ arrays and pointers grade 9 STEP curriculum.pptxc++ arrays and pointers grade 9 STEP curriculum.pptx
c++ arrays and pointers grade 9 STEP curriculum.pptx
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 

More from ANUSUYA S

Digital Old Question Paper for Reference (Bharathiyar University)
Digital Old Question Paper for Reference (Bharathiyar University)Digital Old Question Paper for Reference (Bharathiyar University)
Digital Old Question Paper for Reference (Bharathiyar University)
ANUSUYA S
 
C Old Question Paper for Reference (Bharathiyar University)
C Old Question Paper for Reference (Bharathiyar University)C Old Question Paper for Reference (Bharathiyar University)
C Old Question Paper for Reference (Bharathiyar University)
ANUSUYA S
 
C++ Old Question Paper for Reference (Bharathiyar University)
C++ Old Question Paper for Reference (Bharathiyar University)C++ Old Question Paper for Reference (Bharathiyar University)
C++ Old Question Paper for Reference (Bharathiyar University)
ANUSUYA S
 
C++ 260 MCQ Question with Answer for all Units
C++ 260 MCQ Question with Answer for all UnitsC++ 260 MCQ Question with Answer for all Units
C++ 260 MCQ Question with Answer for all Units
ANUSUYA S
 
C++ - UNIT_-_V.pptx which contains details about File Concepts
C++  - UNIT_-_V.pptx which contains details about File ConceptsC++  - UNIT_-_V.pptx which contains details about File Concepts
C++ - UNIT_-_V.pptx which contains details about File Concepts
ANUSUYA S
 
C++ - UNIT_-_II.pptx which used to contain breif description about classes & ...
C++ - UNIT_-_II.pptx which used to contain breif description about classes & ...C++ - UNIT_-_II.pptx which used to contain breif description about classes & ...
C++ - UNIT_-_II.pptx which used to contain breif description about classes & ...
ANUSUYA S
 
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
ANUSUYA S
 
C++ - UNIT_-_III.pptx which contains detail description of Operator & Overloa...
C++ - UNIT_-_III.pptx which contains detail description of Operator & Overloa...C++ - UNIT_-_III.pptx which contains detail description of Operator & Overloa...
C++ - UNIT_-_III.pptx which contains detail description of Operator & Overloa...
ANUSUYA S
 

More from ANUSUYA S (8)

Digital Old Question Paper for Reference (Bharathiyar University)
Digital Old Question Paper for Reference (Bharathiyar University)Digital Old Question Paper for Reference (Bharathiyar University)
Digital Old Question Paper for Reference (Bharathiyar University)
 
C Old Question Paper for Reference (Bharathiyar University)
C Old Question Paper for Reference (Bharathiyar University)C Old Question Paper for Reference (Bharathiyar University)
C Old Question Paper for Reference (Bharathiyar University)
 
C++ Old Question Paper for Reference (Bharathiyar University)
C++ Old Question Paper for Reference (Bharathiyar University)C++ Old Question Paper for Reference (Bharathiyar University)
C++ Old Question Paper for Reference (Bharathiyar University)
 
C++ 260 MCQ Question with Answer for all Units
C++ 260 MCQ Question with Answer for all UnitsC++ 260 MCQ Question with Answer for all Units
C++ 260 MCQ Question with Answer for all Units
 
C++ - UNIT_-_V.pptx which contains details about File Concepts
C++  - UNIT_-_V.pptx which contains details about File ConceptsC++  - UNIT_-_V.pptx which contains details about File Concepts
C++ - UNIT_-_V.pptx which contains details about File Concepts
 
C++ - UNIT_-_II.pptx which used to contain breif description about classes & ...
C++ - UNIT_-_II.pptx which used to contain breif description about classes & ...C++ - UNIT_-_II.pptx which used to contain breif description about classes & ...
C++ - UNIT_-_II.pptx which used to contain breif description about classes & ...
 
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
 
C++ - UNIT_-_III.pptx which contains detail description of Operator & Overloa...
C++ - UNIT_-_III.pptx which contains detail description of Operator & Overloa...C++ - UNIT_-_III.pptx which contains detail description of Operator & Overloa...
C++ - UNIT_-_III.pptx which contains detail description of Operator & Overloa...
 

Recently uploaded

Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Indian Matka
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Indian MatkaDpboss Matka Guessing Satta Matta Matka Kalyan Chart Indian Matka
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Indian Matka
➒➌➎➏➑➐➋➑➐➐Dpboss Matka Guessing Satta Matka Kalyan Chart Indian Matka
 
Subjectivity Subjectivity Subjectivity Subjectivity
Subjectivity Subjectivity Subjectivity SubjectivitySubjectivity Subjectivity Subjectivity Subjectivity
Subjectivity Subjectivity Subjectivity Subjectivity
hidetoshi4
 
OUROBOROS_COPERNICUS_SAMPLE_CAT_ZYGOCKI.pdf
OUROBOROS_COPERNICUS_SAMPLE_CAT_ZYGOCKI.pdfOUROBOROS_COPERNICUS_SAMPLE_CAT_ZYGOCKI.pdf
OUROBOROS_COPERNICUS_SAMPLE_CAT_ZYGOCKI.pdf
zygocki
 
VTV FULL SCRIPT ------------------------
VTV FULL SCRIPT ------------------------VTV FULL SCRIPT ------------------------
VTV FULL SCRIPT ------------------------
ssathiyanishanth8
 
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka Kalyan Matka
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka Kalyan Matka➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka Kalyan Matka
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka Kalyan Matka
➒➌➎➏➑➐➋➑➐➐Dpboss Matka Guessing Satta Matka Kalyan Chart Indian Matka
 
chemistry projectdssssssssssssssssssssssssssssssssssss
chemistry projectdsssssssssssssssssssssssssssssssssssschemistry projectdssssssssssssssssssssssssssssssssssss
chemistry projectdssssssssssssssssssssssssssssssssssss
STUDIOLUCKYDIGITAL
 
Portfolio Update 18.06.2024 Vienna, Austria.pdf
Portfolio Update 18.06.2024 Vienna, Austria.pdfPortfolio Update 18.06.2024 Vienna, Austria.pdf
Portfolio Update 18.06.2024 Vienna, Austria.pdf
hosseiniliya666
 
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka
➒➌➎➏➑➐➋➑➐➐Dpboss Matka Guessing Satta Matka Kalyan Chart Indian Matka
 
A Brief Introduction About Hanying Chen_
A Brief Introduction About Hanying Chen_A Brief Introduction About Hanying Chen_
A Brief Introduction About Hanying Chen_
Hanying Chen
 
The Evolution and Impact of Hip Hop a cultural and artistic
The Evolution and Impact of Hip Hop a cultural and artisticThe Evolution and Impact of Hip Hop a cultural and artistic
The Evolution and Impact of Hip Hop a cultural and artistic
applemusic056
 
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing
➒➌➎➏➑➐➋➑➐➐  Satta Matka Dpboss Matka Guessing➒➌➎➏➑➐➋➑➐➐  Satta Matka Dpboss Matka Guessing
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing
➒➌➎➏➑➐➋➑➐➐Dpboss Matka Guessing Satta Matka Kalyan Chart Indian Matka
 
"Snow" by Ann Beattie illustrated by Aimie Bourgeois
"Snow" by Ann Beattie illustrated by Aimie Bourgeois"Snow" by Ann Beattie illustrated by Aimie Bourgeois
"Snow" by Ann Beattie illustrated by Aimie Bourgeois
aimiebougie
 
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka
➒➌➎➏➑➐➋➑➐➐Dpboss Matka Guessing Satta Matka Kalyan Chart Indian Matka
 
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka
➒➌➎➏➑➐➋➑➐➐Dpboss Matka Guessing Satta Matka Kalyan Chart Indian Matka
 
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing
➒➌➎➏➑➐➋➑➐➐Dpboss Matka Guessing Satta Matka Kalyan Chart Indian Matka
 
Class 12 Geography Practical file PDF.pdf
Class 12 Geography Practical file PDF.pdfClass 12 Geography Practical file PDF.pdf
Class 12 Geography Practical file PDF.pdf
ravisekhwat866
 
CloneHigh_episode207_SC17_Storyboard_examples
CloneHigh_episode207_SC17_Storyboard_examplesCloneHigh_episode207_SC17_Storyboard_examples
CloneHigh_episode207_SC17_Storyboard_examples
AnnaPalindrome1
 
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka KALYAN MATKA |
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka KALYAN MATKA |➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka KALYAN MATKA |
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka KALYAN MATKA |
➒➌➎➏➑➐➋➑➐➐Dpboss Matka Guessing Satta Matka Kalyan Chart Indian Matka
 
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Indian Matka
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Indian MatkaDpboss Matka Guessing Satta Matta Matka Kalyan Chart Indian Matka
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Indian Matka
➑➌➋➑➒➎➑➑➊➍
 
Clouded storyboard why does the title need to be so long
Clouded storyboard why does the title need to be so longClouded storyboard why does the title need to be so long
Clouded storyboard why does the title need to be so long
Karina Young
 

Recently uploaded (20)

Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Indian Matka
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Indian MatkaDpboss Matka Guessing Satta Matta Matka Kalyan Chart Indian Matka
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Indian Matka
 
Subjectivity Subjectivity Subjectivity Subjectivity
Subjectivity Subjectivity Subjectivity SubjectivitySubjectivity Subjectivity Subjectivity Subjectivity
Subjectivity Subjectivity Subjectivity Subjectivity
 
OUROBOROS_COPERNICUS_SAMPLE_CAT_ZYGOCKI.pdf
OUROBOROS_COPERNICUS_SAMPLE_CAT_ZYGOCKI.pdfOUROBOROS_COPERNICUS_SAMPLE_CAT_ZYGOCKI.pdf
OUROBOROS_COPERNICUS_SAMPLE_CAT_ZYGOCKI.pdf
 
VTV FULL SCRIPT ------------------------
VTV FULL SCRIPT ------------------------VTV FULL SCRIPT ------------------------
VTV FULL SCRIPT ------------------------
 
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka Kalyan Matka
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka Kalyan Matka➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka Kalyan Matka
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka Kalyan Matka
 
chemistry projectdssssssssssssssssssssssssssssssssssss
chemistry projectdsssssssssssssssssssssssssssssssssssschemistry projectdssssssssssssssssssssssssssssssssssss
chemistry projectdssssssssssssssssssssssssssssssssssss
 
Portfolio Update 18.06.2024 Vienna, Austria.pdf
Portfolio Update 18.06.2024 Vienna, Austria.pdfPortfolio Update 18.06.2024 Vienna, Austria.pdf
Portfolio Update 18.06.2024 Vienna, Austria.pdf
 
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka
 
A Brief Introduction About Hanying Chen_
A Brief Introduction About Hanying Chen_A Brief Introduction About Hanying Chen_
A Brief Introduction About Hanying Chen_
 
The Evolution and Impact of Hip Hop a cultural and artistic
The Evolution and Impact of Hip Hop a cultural and artisticThe Evolution and Impact of Hip Hop a cultural and artistic
The Evolution and Impact of Hip Hop a cultural and artistic
 
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing
➒➌➎➏➑➐➋➑➐➐  Satta Matka Dpboss Matka Guessing➒➌➎➏➑➐➋➑➐➐  Satta Matka Dpboss Matka Guessing
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing
 
"Snow" by Ann Beattie illustrated by Aimie Bourgeois
"Snow" by Ann Beattie illustrated by Aimie Bourgeois"Snow" by Ann Beattie illustrated by Aimie Bourgeois
"Snow" by Ann Beattie illustrated by Aimie Bourgeois
 
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka
 
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka
 
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing
 
Class 12 Geography Practical file PDF.pdf
Class 12 Geography Practical file PDF.pdfClass 12 Geography Practical file PDF.pdf
Class 12 Geography Practical file PDF.pdf
 
CloneHigh_episode207_SC17_Storyboard_examples
CloneHigh_episode207_SC17_Storyboard_examplesCloneHigh_episode207_SC17_Storyboard_examples
CloneHigh_episode207_SC17_Storyboard_examples
 
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka KALYAN MATKA |
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka KALYAN MATKA |➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka KALYAN MATKA |
➒➌➎➏➑➐➋➑➐➐ Satta Matka Dpboss Matka Guessing Indian Matka KALYAN MATKA |
 
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Indian Matka
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Indian MatkaDpboss Matka Guessing Satta Matta Matka Kalyan Chart Indian Matka
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Indian Matka
 
Clouded storyboard why does the title need to be so long
Clouded storyboard why does the title need to be so longClouded storyboard why does the title need to be so long
Clouded storyboard why does the title need to be so long
 

C++ - UNIT_-_IV.pptx which contains details about Pointers

  • 1. UNIT – IV POINTERS Pointer is a variable in C++ that holds the address of another variable. They have data type just like variables, for example an integer type pointer can hold the address of an integer variable and an character type pointer can hold the address of char variable. Syntax of pointer - data_type *pointer_name; How to declare a pointer? /* This pointer p can hold the address of an integer variable, here p is a pointer and var is just a * simple integer variable */ int *p, var Assignment As I mentioned above, an integer type pointer can hold the address of another int variable. Here we have an integer variable var and pointer p holds the address of var. To assign the address of variable to pointer we use ampersand symbol (&). /* This is how you assign the address of another variable to the pointer*/ p = &var;
  • 2. Defining a Pointer of Class type We can define pointer of class type, which can be used to point to class objects. class Simple { public: int a; }; int main() { Simple obj; Simple* ptr; // Pointer of class type ptr = &obj; cout << obj.a; cout << ptr->a; // Accessing member with pointer } Here you can see that we have declared a pointer of class type which points to class's object. We can access data members and member functions using pointer name with arrow -> symbol. Pointer to Data Members of Class We can use pointer to point to class's data members (Member variables). Syntax for Declaration - datatype class_name :: *pointer_name; Syntax for Assignment - pointer_name = &class_name :: datamember_name; Both declaration and assignment can be done in a single statement too. datatype class_name::*pointer_name = &class_name::datamember_name ;
  • 3. Using Pointers with Objects For accessing normal data members we use the dot . operator with object and -> qith pointer to object. But when we have a pointer to data member, we have to dereference that pointer to get what its pointing to, hence it becomes, Object.*pointerToMember and with pointer to object, it can be accessed by writing, ObjectPointer->*pointerToMember Lets take an example, to understand the complete concept. class Data { public: int a; void print() { cout << "a is "<< a; } }; int main() { Data d, *dp; dp = &d; // pointer to object int Data::*ptr=&Data::a; // pointer to data member 'a‘ d.*ptr=10; d.print(); dp->*ptr=20; dp->print(); } OUTPUT: a is 10 a is 20
  • 4. This Pointer: C++, this pointer is used to represent the address of an object inside a member function. For example, consider an object obj calling one of its member function say method() as obj.method(). Then, this pointer will hold the address of object obj inside the member function method(). The this pointer acts as an implicit argument to all the member functions. class ClassName { private: int dataMember; public: method(int a) { // this pointer stores the address of object obj and access dataMember this->dataMember = a; ... .. ... } } int main() { ClassName obj; obj.method(5); ... .. ... }
  • 5.
  • 6. C++ ARRAYS: •C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. •Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index. •All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. Declaring Arrays •To declare an array in C++, the programmer specifies the type of the elements and the number of elements required by an array as follows − type arrayName [ arraySize ]; •This is called a single-dimension array. The arraySize must be an integer constant greater than zero and type can be any valid C++ data type. For example, to declare a 10-element array called balance of type double, use this statement − double balance[10]; Initializing Arrays •double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0}; •The number of values between braces { } can not be larger than the number of elements that we declare for the array between square brackets [ ]. Following is an example to assign a single element of the array − •If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write − double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0}; •You will create exactly the same array as you did in the previous example. balance[4] = 50.0; •The above statement assigns element number 5th in the array a value of 50.0. Array with 4th index will be 5th, i.e., last element because all arrays have 0 as the index of their first element which is also called base index. Following is the pictorial representaion of the same array we discussed above −Array Presentation, Accessing Array Elements •An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. For example −double salary = balance[9]; •The above statement will take 10th element from the array and assign the value to salary variable. Following is an example, which will use all the above-mentioned three concepts viz. declaration, assignment and accessing arrays −
  • 7. #include <iostream> using namespace std; #include <iomanip> using std::setw; int main () { int n[ 10 ]; // n is an array of 10 integers // initialize elements of array n to 0 for ( int i = 0; i < 10; i++ ) { n[ i ] = i + 100; // set element at location i to i + 100 } cout << "Element" << setw( 13 ) << "Value" << endl; // output each array element's value for ( int j = 0; j < 10; j++ ) { cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl; } return 0; } •This program makes use of setw() function to format the output. When the above code is compiled and executed, it produces the following result − •Arrays in C++ •Arrays are important to C++ and should need lots of more detail. There are following few important concepts, which should be clear to a C++ programmer − •1 Multi-dimensional arrays -C++ supports multidimensional arrays. The simplest form of the multidimensional array is the two-dimensional array. •2Pointer to an array -You can generate a pointer to the first element of an array by simply specifying the array name, without any index. •3Passing arrays to functions - You can pass to the function a pointer to an array by specifying the array's name without an index.
  • 8. Array class in C++ Array classes are generally more efficient, light- weight and reliable than C-style arrays. Operations on array :- 1. at() :- This function is used to access the elements of array. 2. get() :- This function is also used to access the elements of array. This function is not the member of array class but overloaded function from class tuple. 3. operator[] :- This is similar to C-style arrays. This method is also used to access array elements.
  • 9. Memory model Defines the semantics of computer memory storage for the purpose of the C++ abstract machine. The memory available to a C++ program is one or more contiguous sequences of bytes. Each byte in memory has a unique address. Byte A byte is the smallest addressable unit of memory. It is defined as a contiguous sequence of bits, large enough to hold the value of any UTF-8 code unit (256 distinct values) and of (since C++14)any member of the basic execution character set (the 96 characters that are required to be single-byte). Similar to C, C++ supports bytes of sizes 8 bits and greater. The types char, unsigned char, and signed char use one byte for both storage and value representation. The number of bits in a byte is accessible as CHAR_BIT or std::numeric_limits<unsigned char>::digits. Memory location A memory location is •an object of scalar type (arithmetic type, pointer type, enumeration type, or std::nullptr_t) •or the largest contiguous sequence of bit fields of non-zero length Note: Various features of the language, such as references and virtual functions, might involve additional memory locations that are not accessible to programs but are managed by the implementation.
  • 10. NEW & DELETE OPERATOR C++ supports these functions new and delete to perform allocating and freeing the memory. New operator - The new operator denotes a request for memory allocation on the Heap. If sufficient memory is available, new operator initializes the memory and returns the address of the newly allocated and initialized memory to the pointer variable. Syntax to use new operator: To allocate memory of any data type, the syntax is: pointer-variable = new data-type; Here, pointer-variable is the pointer of type data-type. Data-type could be any built-in data type including array or any user defined data types including structure and class Initialize memory: We can also initialize the memory using new operator: pointer-variable = new data-type(value); Example: int *p = new int(25); float *q = new float(75.25); Allocate block of memory: new operator is also used to allocate a block(an array) of memory of type data-type.pointer-variable = new data-type[size]; where size(a variable) specifies the number of elements in an array. Example: int *p = new int[10] Dynamically allocates memory for 10 integers continuously of type int and returns pointer to the first element of the sequence, which is assigned to p(a pointer). p[0] refers to first element, p[1] refers to second element and so on. Delete operator - Since it is programmer’s responsibility to deallocate dynamically allocated memory, programmers are provided delete operator by C++ language. Syntax: // Release memory pointed by pointer-variable delete pointer-variable; Here, pointer-variable is the pointer that points to the data object created by new. Examples: delete p; delete q;
  • 11. DYNAMIC OBJECT When a C++ object is created, two events occur: Storage is allocated for the object. The constructor is called to initialize that storage. By now you should believe that step two always happens. C++ enforces it because uninitialized objects are a major source of program bugs. It doesn’t matter where or how the object is created – the constructor is always called. Storage can be allocated before the program begins, in the static storage area. This storage exists for the life of the program. Storage can be created on the stack whenever a particular execution point is reached (an opening brace). That storage is released automatically at the complementary execution point (the closing brace). These stack-allocation operations are built into the instruction set of the processor and are very efficient. However, you have to know exactly how many variables you need when you’re writing the program so the compiler can generate the right code. Storage can be allocated from a pool of memory called the heap (also known as the free store). This is called dynamic memory allocation. To allocate this memory, a function is called at runtime; this means you can decide at any time that you want some memory and how much you need. You are also responsible for determining when to release the memory, which means the lifetime of that memory can be as long as you choose – it isn’t determined by scope. Often these three regions are placed in a single contiguous piece of physical memory: the static area, the stack, and the heap (in an order determined by the compiler writer). However, there are no rules. The stack may be in a special place, and the heap may be implemented by making calls for chunks of memory from the operating system. As a programmer, these things are normally shielded from you, so all you need to think about is that the memory is there when
  • 12. BINDING Binding refers to the process of converting identifiers (such as variable and performance names) into addresses. Binding is done for each variable and functions. For functions, it means that matching the call with the right function definition by the compiler. It takes place either at compile time or at runtime. Early Binding (compile-time time polymorphism) As the name indicates, compiler (or linker) directly associate an address to the function call. It replaces the call with a machine language instruction that tells the mainframe to leap to the address of the function. Late Binding : (Run time polymorphism) In this, the compiler adds code that identifies the kind of object at runtime then matches the call with the right function definition (Refer this for details). This can be achieved by declaring a virtual function.
  • 13. POLYMORPHISM Polymorphism is a feature of OOPs that allows the object to behave differently in different conditions. In C++ we have two types of polymorphism: 1) Compile time Polymorphism – This is also known as static (or early) binding. 2) Runtime Polymorphism – This is also known as dynamic (or late) binding. 1) Compile time Polymorphism Function overloading and Operator overloading are perfect example of Compile time polymorphism. Compile time Polymorphism Example In this example, we have two functions with same name but different number of arguments. Based on how many parameters we pass during function call determines which function is to be called, this is why it is considered as an example of polymorphism because in different conditions the output is different. Since, the call is determined during compile time thats why it is called compile time polymorphism. 2) Runtime Polymorphism Function overriding is an example of Runtime polymorphism. Function Overriding: When child class declares a method, which is already present in the parent class then this is called function overriding, here child class overrides the parent class.In case of function overriding we have two definitions of the same function, one is parent class and one in child class. The call to the function is determined at runtime to decide which definition of the function is to be called, thats the reason it is called runtime polymorphism.
  • 14. VIRTUAL FUNCTION C++ virtual function is a member function in the base class that you redefine in a derived class. It is declared using the virtual keyword. It is used to tell the compiler to perform dynamic linkage or late binding on the function. There is a necessity to use the single pointer to refer to all the objects of the different classes. So, we create the pointer to the base class that refers to all the derived objects. But, when base class pointer contains the address of the derived class object, always executes the base class function. This issue can only be resolved by using the 'virtual' function. A 'virtual' is a keyword preceding the normal declaration of a function. When the function is made virtual, C++ determines which function is to be invoked at the runtime based on the type of the object pointed by the base class pointer. Late binding or Dynamic linkage In late binding function call is resolved during runtime. Therefore compiler determines the type of object at runtime, and then binds the function call. Rules of Virtual Function Virtual functions must be members of some class. Virtual functions cannot be static members. They are accessed through object pointers. & They can be a friend of another class. A virtual function must be defined in the base class, even though it is not used. The prototypes of a virtual function of the base class and all the derived classes must be identical. If the two functions with the same name but different prototypes, C++ will consider them as the overloaded functions. We cannot have a virtual constructor, but we can have a virtual destructor Consider the situation when we don't use the virtual keyword.