Pointer Declaration Syntax: Unit 4
Pointer Declaration Syntax: Unit 4
Pointer Declaration Syntax: Unit 4
The datatype is the base type of the pointer which must be a valid C+
+ data type.
The variable_name is should be the name of the pointer variable.
Asterisk used above for pointer declaration is similar to asterisk used
to perform multiplication operation. It is the asterisk that marks the
variable as a pointer.
The dereference operator (*) helps us get the value that has been stored in
a memory address.
For example:
If we have a variable given the name num, stored in the address 0x234 and
storing the value 28.
The reference operator (&) will return 0x234.
Example 1:
#include <iostream>
using namespace std;
int main() {
int x = 27;
int *ip;
ip = &x;
cout << "Value of x is : ";
cout << x << endl;
cout << "Value of ip is : ";
cout << ip<< endl;
cout << "Value of *ip is : ";
cout << *ip << endl;
return 0;
}
Output:
Incrementing a Pointer
We prefer using a pointer in our program instead of an array because the variable
pointer can be incremented, unlike the array name which cannot be incremented
because it is a constant pointer. The following program increments the variable
pointer to access each succeeding element of the array −
Live Demo
#include <iostream>
int main () {
int var[MAX] = {10, 100, 200};
int *ptr;
return 0;
}
When the above code is compiled and executed, it produces result something as
follows −
Address of var[0] = 0xbfa088b0
Value of var[0] = 10
Address of var[1] = 0xbfa088b4
Value of var[1] = 100
Address of var[2] = 0xbfa088b8
Value of var[2] = 200
Decrementing a Pointer
The same considerations apply to decrementing a pointer, which decreases its
value by the number of bytes of its data type as shown below −
Live Demo
#include <iostream>
int main () {
int var[MAX] = {10, 100, 200};
int *ptr;
return 0;
}
When the above code is compiled and executed, it produces result something as
follows −
Address of var[3] = 0xbfdb70f8
Value of var[3] = 200
Address of var[2] = 0xbfdb70f4
Value of var[2] = 100
Address of var[1] = 0xbfdb70f0
Value of var[1] = 10
Pointer Comparisons
Pointers may be compared by using relational operators, such as ==, <, and >. If p1
and p2 point to variables that are related to each other, such as elements of the
same array, then p1 and p2 can be meaningfully compared.
The following program modifies the previous example one by incrementing the
variable pointer so long as the address to which it points is either less than or equal
to the address of the last element of the array, which is &var[MAX - 1] −
Live Demo
#include <iostream>
int main () {
int var[MAX] = {10, 100, 200};
int *ptr;
return 0;
}
When the above code is compiled and executed, it produces result something as
follows −
Address of var[0] = 0xbfce42d0
Value of var[0] = 10
Address of var[1] = 0xbfce42d4
Value of var[1] = 100
Address of var[2] = 0xbfce42d8
Value of var[2] = 200
Object.*pointerToMember
Copy
and with pointer to object, it can be accessed by writing,
ObjectPointer->*pointerToMember
int main()
{
Data d, *dp;
dp = &d; // pointer to object
d.*ptr=10;
d.print();
dp->*ptr=20;
dp->print();
}
Output:
a is 10
a is 20
C++ 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++.
1. #include <iostream>
2. class Employee {
3. public:
4. int id; //data member (also instance variable)
5. string name; //data member(also instance variable)
6. float salary;
7. Employee(int id, string name, float salary)
8. {
9. this->id = id;
10. this->name = name;
11. this->salary = salary;
12. }
13. void display()
14. {
15. cout<<id<<" "<<name<<" "<<salary<<endl;
16. }
17. };
18. int main() {
19. Employee e1 =Employee(101, "Sonoo", 890000); //creating an object of Em
ployee
20. Employee e2=Employee(102, "Nakul", 59000); //creating an object of Emplo
yee
21. e1.display();
22. e2.display();
23. return 0;
24. }
Output:
In C++, we can declare a pointer points to the base class as well as derive class.
Consider below example to understand pointer to derived class
#include<iostream.h>
class base
{
public:
int n1;
void show()
{
cout<<”\nn1 = “<<n1;
}
};
class derive : public base
{
public:
int n2;
void show()
{
cout<<”\nn1 = “<<n1;
cout<<”\nn2 = “<<n2;
}
};
int main()
{
base b;
base *bptr; //base pointer
cout<<”Pointer of base class points to it”;
bptr=&b; //address of base class
bptr->n1=44; //access base class via base pointer
bptr->show();
derive d;
cout<<”\n”;
bptr=&d; //address of derive class
bptr->n1=66; //access derive class via base pointer
bptr->show();
return 0;
}
Output
Pointer of base class points to it
n1 = 44
Pointer of base class points to derive class
n1=66
Here the show() method is the overridden method, bptr execute show() method of ‘base’ class
twice and display its content. Even though bptr first points to ‘base’ and second time points to
‘derive’ ,both the time bptr->show() executes the ‘base’ method show()
C++ Polymorphism
The term "Polymorphism" is the combination of "poly" + "morphs" which means many
forms. It is a greek word. In object-oriented programming, we use 3 main concepts:
inheritance, encapsulation, and polymorphism.
1. class A // base class declaration.
2. {
3. int a;
4. public:
5. void display()
6. {
7. cout<< "Class A ";
8. }
9. };
10. class B : public A // derived class declaration.
11. {
12. int b;
13. public:
14. void display()
15. {
16. cout<<"Class B";
17. }
18. };
In the above case, the prototype of display() function is the same in both the base
and derived class. Therefore, the static binding cannot be applied. It would be great
if the appropriate function is selected at the run time. This is known as run time
polymorphism.
It is less flexible as mainly all the things It is more flexible as all the things
execute at the compile time. execute at the run time.
1. #include <iostream>
2. using namespace std;
3. class Animal {
4. public:
5. void eat(){
6. cout<<"Eating...";
7. }
8. };
9. class Dog: public Animal
10. {
11. public:
12. void eat()
13. { cout<<"Eating bread...";
14. }
15. };
16. int main(void) {
17. Dog d = Dog();
18. d.eat();
19. return 0;
20. }
I. Function Overloading
Function overloading means one function can perform many tasks. In C++, a single
function is used to perform many tasks with the same name and different types of
arguments. In the function overloading function will call at the time of program
compilation. It is an example of compile-time polymorphism.
In the below example, A function ADD() is used to perform two tasks. The two asks
would be to add two integer values and add two strings (concatenate).
Source Code:
1 #include <iostream>
class Addition {
3
public:
4
8 }
9 int ADD() { // Function with same name but without parameter
string a= "HELLO";
10
string b="SAM"; // in this function concatenation is performed
11
string c= a+b;
12
cout<<c<<endl;
13
14
}
15
};
16 int main(void) {
17 Addition obj; // Object is created
return 0;
20
}
21
22
Output
143
HELLOSAM
In the above example, we use function ADD() to perform many tasks which is a property
of polymorphism in C++.
II. Operator Overloading
Operator overloading means defining additional tasks to operators without changing its
actual meaning. We do this by using operator function.
Source Code
1 #include <iostream>
class A
3
{
4
5
string x;
6
public:
7
A(){}
8 A(string i)
9 {
10 x=i;
11 }
12 void operator+(A);
void display();
13
};
14
15
void A:: operator+(A a)
16
{
17
18
string m = x+a.x;
19
cout<<"The result of the addition of two objects is : "<<m;
20
21
22
}
23 int main()
24 {
25 A a1("Welcome");
26 A a2("back");
27 a1+a2;
return 0;
28
}
29
30
Output
2. Runtime Polymorphism
In a Runtime polymorphism, functions are called at the time the program execution.
Hence, it is known as late binding or dynamic binding.
I. Function overriding
In function overriding, we give the new definition to base class function in the derived
class. At that time, we can say the base function has been overridden. It can be only
possible in the ‘derived class’. In function overriding, we have two definitions of the
same function, one in the superclass and one in the derived class. The decision about
which function definition requires calling happens at runtime. That is the reason we call
it ‘Runtime polymorphism’.
Source code
1
2 #include <iostream>
4 class Animal {
public:
5
void function(){
6
cout<<"Eating..."<<endl;
7
}
8
};
9 class Man: public Animal
10 {
11 public:
12 void function()
13 {
cout<<"Walking ..."<<endl;
14
}
15
};
16
int main(void) {
17
Animal A =Animal();
18
A.function(); //parent class object
19 Man m = Man();
20 m.function(); // child class object
21
22 return 0;
23 }
24
Output
Eating …..
Walking……
II. Virtual Function
A virtual function is declared by keyword virtual. The return type of virtual function may
be int, float, void.
A virtual function is a member function in the base class. We can redefine it in a derived
class. It is part of run time polymorphism. The declaration of the virtual function must be
in the base class by using the keyword virtual. A virtual function is not static.
The virtual function helps to tell the compiler to perform dynamic binding or late binding
on the function.
If it is necessary to use a single pointer to refer to all the different classes’ objects. This is
because we will have to create a pointer to the base class that refers to all the derived
objects.
But, when the base class pointer contains the derived class address, the object always
executes the base class function. For resolving this problem, we use the virtual function.
When we declare a virtual function, the compiler determines which function to invoke at
runtime.
Let’s see the below example for understanding how the program execution happens
without virtual function and with virtual function.
Source code
1 //without virtual Function
2
4
#include <iostream>
5
using namespace std;
6
class Add
7
{
8
int x=5, y=20;
9
public:
10 void display() //overridden function
11 {
13 }
14 };
21 }
22 };
23 int main()
{
24
Add *m; //base class pointer .it can only access the base class memb
25
Substract s; // making object of derived class
26
m = &s;
27
m->display(); // Accessing the function by using base class pointer
28 return 0;
29 }
30
Output
Value of x is: 25
Source Code
1 #include<iostream>
3
class Add
4
{
5
public:
6
virtual void print ()
7
{ int a=20, b=30;
8 cout<< " base class Action is:"<<a+b <<endl;
9 }
10
11 void show ()
13 };
14
class Sub: public Add
15
{
16
public:
17
void print () //print () is already virtual function in derived class, we could
18
{ int x=20,y=10;
19
20
cout<< " derived class Action:"<<x-y <<endl; }
21
22 void show ()
23 { cout<< "show derived class" <<endl; }
24
25
};
26
27
//main function
28
int main()
29 {
30 Add *aptr;
31 Sub s;
32 aptr = &s;
33
aptr->print();
35
36
// Non-virtual function, binded at compile time
37
aptr->show();
38
39
return 0;
40
}
41
42
Output
When the function has no definition, we call such functions as “Do-nothing function or
Pure virtual function”. The declaration of this function happens in the base class with no
definition.
Source Code
1 #include <iostream>
2 using namespace std;
3 class Animal
4 {
5 public:
6 virtual void show() = 0; //Pure virtual function declaration.
7 };
8 class Man: public Animal
9 {
10 public:
11 void show()
12 {
13 cout << "Man is the part of animal husbandry " << endl;
14 }
15 };
16 int main()
17 {
18 Animal *aptr; //Base class pointer
19 //Animal a;
20 Man m; //derived class object creation.
21 aptr = &m;
22 aptr->show();
23 return 0;
24 }
Unary Operator Overloading:
Q. Write a C++ program to overload unary operators that is increment and decrement.
Answer:
Output:
As the name suggests, those operators which operate on two operands or data are called binary
operators.
Here is an example to show how binary operators are overloaded in C++.
Example: C++ program to illustrate binary operator overloading
#include<iostream>
using namespace std;
class complex
{
private:
int real,imag;
public:
void getvalue()
{
cout<<"Enter the value of real number:";
cin>>real;
cout<<"Enter the value of imaginary number:";
cin>>imag;
}
complex operator+(complex obj)
{
complex temp;
temp.real=real+obj.real;
temp.imag=imag+obj.imag;
return(temp);
}
complex operator-(complex obj)
{
complex temp;
temp.real=real-obj.real;
temp.imag=imag-obj.imag;
return(temp);
}
void display()
{
cout<<real<<"+"<<"("<<imag<<")"<<"i"<<"\n";
}
};
int main()
{
complex c1,c2,c3,c4;
c1.getvalue();
c2.getvalue();
c3 = c1+c2;
c4 = c1-c2;
cout<<"Result is:\n";
c3.display();
c4.display();
return 0;
}