0% found this document useful (0 votes)
54 views

Unit-2 Pointers

The document discusses pointers in C++. It defines pointers as variables that hold memory addresses and can point to other data types like objects and arrays. It covers declaring and initializing pointers, using the indirection and address operators, dynamic memory allocation with new and delete, pointers to objects and classes, and this pointer which refers to the current class instance. Examples are provided to demonstrate concepts like declaring different pointer types, storing and accessing memory addresses, allocating and deleting memory for objects, and using pointers to call class methods.

Uploaded by

ajitkolpuke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Unit-2 Pointers

The document discusses pointers in C++. It defines pointers as variables that hold memory addresses and can point to other data types like objects and arrays. It covers declaring and initializing pointers, using the indirection and address operators, dynamic memory allocation with new and delete, pointers to objects and classes, and this pointer which refers to the current class instance. Examples are provided to demonstrate concepts like declaring different pointer types, storing and accessing memory addresses, allocating and deleting memory for objects, and using pointers to call class methods.

Uploaded by

ajitkolpuke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Sinhgad College of Engineering

Vadgaon Bk. Pune


Approved by AICTE New Delhi Recognized by Govt. of Maharashtra
Affiliated to Savitribai Phule Pune University
Accredited by NAAC with A+ Grade

Subject:- 210243: Object Oriented Programming.


Unit-2 Inheritance & Pointers.
Mr. Hitesh E. Chaudhari
Assistant. Professor.
Department of Computer Engineering,
Sinhgad College Of Engineering Vadgaon Bk, Pune-041
Email:- [email protected]

24-11-2022 1
UNIT-2
INHERITANCE & POINTERS

24-11-2022 UNIT-I Object Oriented Programming 2


Department Of Computer Engineering
Contents:
❑ Inheritance.
Base class & Derived class, Protected members, relationship between
base class & derived class , constructor & destructor in derived class,
overriding member functions, class hierarchies, public and private
inheritance, Ambiguity in multiple inheritance, virtual base class,
Abstract class, Nested class.

UNIT-I Problem Solving ,Programming and Python Programming


24-11-2022 3
Department Of Computer Engineering
Contents:
❑ Pointer.
Declaring and initializing pointers, indirection operators, Memory
management, new and delete, pointers to object, this pointer, pointer
vs arrays, accessing arrays using pointers, arrays of pointers, function
pointer, pointer to pointer, pointer to derived class, passing pointer to
functions, return pointer to from functions, null pointer , void pointer.

UNIT-I Problem Solving ,Programming and Python Programming


24-11-2022 4
Department Of Computer Engineering
3.Pointer

UNIT-I Problem Solving ,Programming and Python Programming


24-11-2022 5
Department Of Computer Engineering
3 Pointer.
3.1 Declaring Pointer & Initializing Pointer.
The general form of a pointer declaration is as follows:
type ∗var_name ;

where type is any valid C++ data type and var_name is the name of the pointer variable. Following
declarations declares pointers of different types :

int ∗iptr ; //creates an integer pointer iptr


char ∗cptr ; //creates a character pointer cptr
float ∗fptr ; //creates a float pointer fptr

UNIT-II Inheritance & Pointer


24-11-2022 6
Department Of Computer Engineering
3 Pointer.
3.1.1 Declaring Pointer.
Two special operators ∗ and & are used with pointers. The & is a unary operator that returns the
memory address of its operand. For example,

int i = 25 ; // declares an int variable i


int ∗iptr ; // declares an int pointer iptr
iptr = &i ; // stores the memory address of i into iptr

UNIT-II Inheritance & Pointer


24-11-2022 7
Department Of Computer Engineering
3 Pointer.
3.1.2 Initializing Pointer.
• The pointing of assigning the address of a variable to a pointer variables is known as initialization

•Once a pointer variable has been declared we can use the assignment operator to initialize the
variable.

• Example:

int quantity;
int *p; // declaration
p=&quantity; //initialization.

UNIT-II Inheritance & Pointer


24-11-2022 8
Department Of Computer Engineering
3 Pointer.
3.2 Pointer Indirection Operator:
• The unary indirection operator (*) dereferences a pointer; that is, it converts a pointer value to an l-value.

• The operand of the indirection operator must be a pointer to a type.

• Every Variable stored in memory and every memory cell have address associated with it.

• We can print address of variable using & Operator.

UNIT-II Inheritance & Pointer


24-11-2022 9
Department Of Computer Engineering
3 Pointer.
Example:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{ int a=10;
int b=20;
cout<<“\n address of a is :”<<&a;
cout<<“\n address of b is :”<<&b;
getch();
} Output:
address of a is :0x28fefc
address of b is :0x28fef8
UNIT-II Inheritance & Pointer
24-11-2022 10
Department Of Computer Engineering
3 Pointer.
3.3 Memory Management : New and Delete

There are two types of memory management operators in C++:


• new
• delete

• New and delete operators are used in memory allocation in c++.

• New is used to allocated memory whereas delete is used to0 deallocate the memory.

UNIT-II Inheritance & Pointer


24-11-2022 11
Department Of Computer Engineering
3 Pointer.
3.3 Memory Management : New and Delete

New operator:
The new operator in C++ is used for dynamic storage allocation. This operator can be used to create
object of any type.

General syntax of new operator in C++:

pointer variable = new datatype;

new is a keyword and the pointer variable is a variable of type datatype.

UNIT-II Inheritance & Pointer


24-11-2022 12
Department Of Computer Engineering
3 Pointer.
For example:

1. int *a = new int;


2. *a = 20;
or
3. int *a = new int(20);

delete operator:
The delete operator in C++ is used for releasing memory space when the object is no longer needed.
Once a new operator is used, it is efficient to use the corresponding delete operator for release of memory.

General syntax of delete operator in C++:


delete pointer variable;

UNIT-II Inheritance & Pointer


24-11-2022 13
Department Of Computer Engineering
3 Pointer.
3.4 : Pointer to Object:

• A variable that holds an address value is called a pointer variable or simply pointer.
• Pointer can point to objects as well as to simple data types and arrays.

• Pointer is used to access class members.


• Pointer can point to object also.
• Pointer can be created in normal way.

•sometimes we don’t know, at the time that we write the program , how many objects we want to
create . when this is the case we can use new to create objects while the program is running. new
returns a pointer to an unnamed objects

UNIT-II Inheritance & Pointer


24-11-2022 14
Department Of Computer Engineering
3 Pointer.
Example:
#include <iostream.h>
#include <string.h>
#include<conio.h>
class student
{
private:
int rollno;
string name;
public:
student():
rollno(0),name("")
{
UNIT-II Inheritance & Pointer
} 24-11-2022
Department Of Computer Engineering
15
3 Pointer.
student(int r, string n): rollno(r),name (n)
{}
void get()
{ cout<<"enter roll no";
cin>>rollno;
cout<<"enter name";
cin>>name;
}
void print()
{ cout<<"roll no is "<<rollno;
cout<<"name is "<<name;
}
};
UNIT-II Inheritance & Pointer
24-11-2022 16
Department Of Computer Engineering
3 Pointer.
void main ()
{
student *ps=new student;
(*ps).get();
(*ps).print();
delete ps;
}

UNIT-II Inheritance & Pointer


24-11-2022 17
Department Of Computer Engineering
3 Pointer.
3.5 : this Pointer :

• In C++ programming, this is a keyword that refers to the current instance of the class. There can be
3 main usage of this keyword in C++.

• It can be used to pass current object as a parameter to another method.

• It can be used to refer current class instance variable.

• It can be used to declare indexers.

UNIT-II Inheritance & Pointer


24-11-2022 18
Department Of Computer Engineering
3 Pointer.
Example:
#include <iostream.h>
#include<stdio.h>
#include<conio.h>
class Employee {
public:
int id; //data member (also instance variable)
float salary;
Employee(int id, float salary)
{
this->id = id;
this->salary = salary;
}

UNIT-II Inheritance & Pointer


24-11-2022 19
Department Of Computer Engineering
3 Pointer.
void display()
{
cout<<id<<" "<<salary<<endl;
}
};
int main(void)
{
Employee e1 =Employee(101, 890000); //creating an object of Employee
Employee e2=Employee(102, 59000); //creating an object of Employee
e1.display();
e2.display();
return 0;
}
UNIT-II Inheritance & Pointer
24-11-2022 20
Department Of Computer Engineering
3 Pointer.
Output:
101 890000
102 59000

UNIT-II Inheritance & Pointer


24-11-2022 21
Department Of Computer Engineering
3 Pointer.
3.6 Pointer vs Array :
Pointer Array
Arrays are declared as type Pointers are declared as type * var_name;
var_name[size];
Collection of elements of similar data Store the address of another variable.
type.
An array can decide the number of The pointer can store the address of only
elements it can store. one variable.
Arrays are allocated at compile time. Pointers are allocated at run-time.
Memory allocation is in sequence. Memory allocation is random.
Java Support the concept of an array. Java Does not support pointers.
int *marks; //pointer declaration int mark[5] = {19, 10, 8, 17, 9}; //array
declaration
UNIT-II Inheritance & Pointer
24-11-2022 22
Department Of Computer Engineering
3 Pointer.
3.6.1 Accessing Arrays Using pointer :

• Pointers are the variables that hold address. Not only can pointers store address of single variable, it
can also store address of an array.

• Example:
int *ptr;
int a[5];
ptr=& a[5];//&a[2] is the address of third element of a[5].

It is also possible to access the elements of array using pointer.

UNIT-II Inheritance & Pointer


24-11-2022 23
Department Of Computer Engineering
3 Pointer.
Example:
#include <iostream.h>
#include<stdio.h>
#include<conio.h>
int main()
{
int a[5]={10,20,30,40,50};
int *p;
p=a
cout<<“\n Printing array elements using pointer”;
for(int i=0;i<5;i++)
{ cout<<“\n”<<*p;
p++;//point to next element
UNIT-II Inheritance & Pointer
24-11-2022 24
Department Of Computer Engineering
3 Pointer.
}
return 0;
}
Output:
printing array elements using pointer.
10
20
30
40
50

UNIT-II Inheritance & Pointer


24-11-2022 25
Department Of Computer Engineering
3 Pointer.
3.7 Arrays Of pointer :

• We can also declare point to whole array instead of only one element of the array.

• Syntax:
data_type(*var_name)[size of array];

Example:
int (*ptr)[10];

Here ptr is pointer that can point to an array of 10 integers.

UNIT-II Inheritance & Pointer


24-11-2022 26
Department Of Computer Engineering
3 Pointer.
Example:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int ptr[5];
int *ptr1;
ptr1=ptr;
cout<<"Enters The Array of Elements...";
for(int i=0;i<5;i++)
{cin>>ptr[i];
}
cout<<“The Values Are:”;
for(int j=0;j<5;j++)
{
UNIT-II Inheritance & Pointer
24-11-2022 27
Department Of Computer Engineering
3 Pointer.
cout<<"\n"<<ptr1[j];
}
getch();
}
Output:
printing array elements using pointer.
10
20
30
40
50
The Values are:
10
20
30
40
50
UNIT-II Inheritance & Pointer
24-11-2022 28
Department Of Computer Engineering
3 Pointer.
3.8 Pointer to Pointer :
• It is possible to for pointer to store another pointer.

• It is called multiple indirection .

• Pointer contains address of variable.

• In pointer to pointer , first pointer contains address of second pointer , second pointer contains
address of actual value.

• Variable with pointer to pointer can declared in following manner

Syntax:

data_type **p;

UNIT-II Inheritance & Pointer


24-11-2022 29
Department Of Computer Engineering
3 Pointer.
Example:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int ptr[5]=10;
int *ptr1;
int **ptr2;
ptr1=&ptr;
ptr2=&ptr1;
cout<<“Value:“<<ptr;
cout<<“First Pointer:”<<*ptr1;
cout<<“Another Pointer:”<<**ptr2;
getch()
}
UNIT-II Inheritance & Pointer
24-11-2022 30
Department Of Computer Engineering
3 Pointer.
Output:
Value:10;
First Pointer:10
Another Pointer:10

UNIT-II Inheritance & Pointer


24-11-2022 31
Department Of Computer Engineering
3 Pointer.
3.9 Function Pointer:

• Function pointer are used in dynamic binding.

• It acts as base for pointer to members.

• It is also known as call back function.

• Function can be passed as argument to another function. When function is passed, it is


passed as pointer during.

Syntax:

return_data type (*function name)();


UNIT-II Inheritance & Pointer
24-11-2022 32
Department Of Computer Engineering
3 Pointer.
Example:
#include<iostream.h>
#include<conio.h>
void add(int a, int b)
{
cout<<"Addition:"<<a+b<<"\n";
}
void mul(int a, int b)
{
cout<<"Multiplication:"<<a*b<<"\n";
}
void main()
{
clrscr();
void (*ptr)(int ,int);
ptr=add;
UNIT-II Inheritance & Pointer
24-11-2022 33
Department Of Computer Engineering
3 Pointer.
ptr(10,30);
ptr=mul;
ptr(5,3);
getch();
}

Output:

Addition:40
Multiplication:15

UNIT-II Inheritance & Pointer


24-11-2022 34
Department Of Computer Engineering
3 Pointer.
3.10 Pointer to Derived Class:

• We can declare pointer to point to the base class as well as derived class.

• Consider below example to understand pointer to derived class


Example:
#include<iostream.h>
#include<conio.h>
class student
{
public:
int roll;
public:
void get()
UNIT-II Inheritance & Pointer
{ 24-11-2022
Department Of Computer Engineering
35
3 Pointer.
cout<<"Enter your Roll No:";
cin>>roll;
}
void put()
{
cout<<"your Roll No:"<<roll;
}
};
class Exam:public student
{
public:
int marks;
public:
UNIT-II Inheritance & Pointer
24-11-2022 36
Department Of Computer Engineering
3 Pointer.
void getdata()
{
get();
cout<<"\n your Marks:";
cin>>marks;
}
void putdata()
{
put();
cout<<"\n Marks:"<<marks;
} };
void main()
{ clrscr();
UNIT-II Inheritance & Pointer
24-11-2022 37
Department Of Computer Engineering
3 Pointer.
Exam *ptr;
Exam e1;
ptr=&e1;
ptr->getdata();
ptr->putdata();
getch();
}
Output:
Enter Your Roll No:101
Enter Your Marks:65
Your Roll No: 101
Your Marks:65
UNIT-II Inheritance & Pointer
24-11-2022 38
Department Of Computer Engineering
3 Pointer.
3.11 : Passing pointer to function:

C++ Allows you to pass pointer to function . If a pointer passed to a function as a parameter to be
modified then changes made to the pointer does not reflect back outside that outside function.

This is only copy of the pointer is passed to the function.

One of the use of pointer variables in c++ is in passing arguments to function.

By default argument are passed to a function by value.

UNIT-II Inheritance & Pointer


24-11-2022 39
Department Of Computer Engineering
3 Pointer.
#include <iostream>
using namespace std;
int global_Var = 42;
// function to change pointer value
void changePointerValue(int* pp)
{
pp = &global_Var;
}
int main()
{ int var = 23;
int* ptr_to_var = &var;
cout << "Passing Pointer to function:" << endl;
cout << "Before :" << *ptr_to_var << endl; // display 23

UNIT-II Inheritance & Pointer


24-11-2022 40
Department Of Computer Engineering
3 Pointer.
changePointerValue(ptr_to_var);
cout << "After :" << *ptr_to_var << endl; // display 23
return 0;
}
Output:
Passing Pointer to function:
Before :23
After :23

UNIT-II Inheritance & Pointer


24-11-2022 41
Department Of Computer Engineering
3 Pointer.
3.12 : Return pointers from function:
• C++ allows to return an array from a function, similar way C++ allows you to return a pointer
from a function.
• C++ is a variable which is used to store the memory address of another variable. We can pass
pointers to the function as well as return pointer from a function.
int * myFunction()
{
.
.
.
}
•Second point to remember is that, it is not good idea to return the address of a local variable to
outside of the function

UNIT-II Inheritance & Pointer


24-11-2022 42
Department Of Computer Engineering
3 Pointer.
#include <iostream>
using namespace std;
double & GetWeeklyHours()
{
double h = 46.50;
double &hours = h;
return hours;
}
double * GetSalary()
{
double salary = 26.48;
double *HourlySalary = &salary;
return HourlySalary;
}
int main()
{ double hours = GetWeeklyHours();
UNIT-II Inheritance & Pointer
doub24l-11-2
e s0a22lary = *GetSalary(); Department Of Computer Engineering
43
3 Pointer.

cout << "Weekly Hours: " << hours << endl;


cout << "Hourly Salary: " << salary << endl;
double WeeklySalary = hours * salary;
cout << "Weekly Salary: " << WeeklySalary << endl;
return 0;
}
Output:
Weekly Hours: 46.5
Hourly Salary: 26.48
Weekly Salary: 1231.32

UNIT-II Inheritance & Pointer


24-11-2022 44
Department Of Computer Engineering
3 Pointer.
3.12 : Null pointer:
• This is done at the time of variable declaration. A pointer that is assigned NULL is called
a null pointer.

• The NULL pointer is a constant with a value of zero defined in several standard libraries,
including iostream. Consider the following program −
#include <iostream>
using namespace std; int main ()
{ int *ptr = NULL;
cout << "The value of ptr is " << ptr ;
return 0;
}
Output:
The value of ptr is 0
UNIT-II Inheritance & Pointer
24-11-2022 45
Department Of Computer Engineering
3 Pointer.
3.12 : Null pointer:
• This is done at the time of variable declaration. A pointer that is assigned NULL is called
a null pointer.

• The NULL pointer is a constant with a value of zero defined in several standard libraries,
including iostream. Consider the following program −
#include <iostream>
using namespace std; int main ()
{ int *ptr = NULL;
cout << "The value of ptr is " << ptr ;
return 0;
}
Output:
The value of ptr is 0
UNIT-II Inheritance & Pointer
24-11-2022 46
Department Of Computer Engineering
3 Pointer.
3.13 : Void pointer:

• A void pointer is a pointer that has no associated data type with it. A void pointer can hold address
of any type and can be type casted to any type.

• In C++, we cannot assign the address of a variable to the variable of a different data type.

Example:
int a = 10;
char b = 'x';
void *p = &a; // void pointer holds address of int 'a'
p = &b; // void pointer holds address of char 'b’

UNIT-II Inheritance & Pointer


24-11-2022 47
Department Of Computer Engineering
3 Pointer.
#include <iostream.h>
#include<stdio.h>
#include<conio.h>
int main()
{ void *ptr; // void pointer declaration
int *ptr1; // integer pointer declaration
int data=10; // integer variable initialization
ptr=&data; // storing the address of data variable in void pointer variable
ptr1=(int *)ptr; // assigning void pointer to integer pointer
cout << "The value of *ptr1 is : " <<*ptr1<< endl;
return 0;
}
Output:
The 2v4-a11l-u20e22 of *ptr1 is : 10 UNIT-II Inheritance & Pointer
48
Department Of Computer Engineering

You might also like