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

Lecture 03

The document discusses different types of pointers in C++ including regular pointers, constant pointers, and constant pointer to constant data. It provides examples of code using these different pointer types and explains what each type means. The examples show how to declare the different pointer types, assign values to them, and what operations are allowed or forbidden on each type of pointer.

Uploaded by

radhelinuxpc
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)
11 views

Lecture 03

The document discusses different types of pointers in C++ including regular pointers, constant pointers, and constant pointer to constant data. It provides examples of code using these different pointer types and explains what each type means. The examples show how to declare the different pointer types, assign values to them, and what operations are allowed or forbidden on each type of pointer.

Uploaded by

radhelinuxpc
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/ 34

Introduction to C++

Prabhat
Basanta R. K.
Pujahari
Nandi
IIT Madras
IIT Bombay
A few words on C++ and STL
v Pointers: These are the variables that hold address values

v Syntax: int *ptr;

v Program # 37: pointer1.cpp v Analysis

#include <iostream>
using namespace std; There are two asterisk used.

int main()
{
int length = 10; 1. int *ptr: It’s a pointer to integer
int *ptr; variable
ptr = & length;
2. *ptr = 20: This is a dereference
*ptr = 20; operator.
This means that the value of
cout << “Value of length = “ << length << endl;
cout << “Value of length = “ << *ptr << endl;
the pointer pointed to by the
pointer “ptr”.
return 0;
}
A few words on C++ and STL : different types of pointers
v Program # 46: pointer8.cpp 1. Pointers – Syntax
int * ptr;
#include <iostream>
using namespace std;

int main()
{
int jj = 5;
int * ptr;

ptr = &jj;
cout << “ STEP 1: *ptr = “ << *ptr << endl;
jj = 10;
cout << “ STEP 2: *ptr = “ << *ptr << endl;
int jk = 15;
*ptr = &jk;
cout << “ STEP 3: *ptr = “ << *ptr << endl;

*ptr = 20;
cout << “ STEP 4: *ptr = “ << *ptr << endl;

return 0;
}
A few words on C++ and STL : different types of pointers
v Program # 46: pointer9.cpp 2. Constant Pointers – Syntax
const int * ptr;
#include <iostream>
using namespace std;
Ø This means ptr is a pointer
int main() pointed to constant integer
{
int jj = 5;
const int * ptr;

ptr = &jj;
cout << “ STEP 1: *ptr = “ << *ptr << endl;
jj = 10;
cout << “ STEP 2: *ptr = “ << *ptr << endl;
int jk = 15;
*ptr = &jk;
cout << “ STEP 3: *ptr = “ << *ptr << endl;

*ptr = 20;
cout << “ STEP 4: *ptr = “ << *ptr << endl;

return 0;
}
A few words on C++ and STL : different types of pointers
v Program # 46: pointer9.cpp 2. Constant Pointers – Syntax
const int * ptr;
#include <iostream>
using namespace std;
Ø This means ptr is a pointer
int main() pointed to constant integer
{
int jj = 5;
const int * ptr;

ptr = &jj;
cout << “ STEP 1: *ptr = “ << *ptr << endl;
jj = 10;
cout << “ STEP 2: *ptr = “ << *ptr << endl;
int jk = 15;
*ptr = &jk;
cout << “ STEP 3: *ptr = “ << *ptr << endl;

Since this is pointed to constant


*ptr = 20;
cout << “ STEP 4: *ptr = “ << *ptr << endl; Integer, one can not assign another
value.
return 0;
}
A few words on C++ and STL : different types of pointers
v Program # 46: pointer10.cpp 3. Constant Pointers – Syntax
int * const ptr;
#include <iostream>
using namespace std;
Ø This means ptr is a constant
int main() pointer
{
int jj = 5;
int * const ptr = & jj;
cout << “ STEP 1: *ptr = “ << *ptr << endl;

*ptr = 10;
cout << “ STEP 2: *ptr = “ << *ptr << endl;

int jk = 15;
ptr = &jk;

cout << “ STEP 3: *ptr = “ << *ptr << endl;


return 0;
}
A few words on C++ and STL : different types of pointers
v Program # 46: pointer10.cpp 3. Constant Pointers – Syntax
int * const ptr;
#include <iostream>
using namespace std;
Ø This means ptr is a constant
int main() pointer
{
int jj = 5;
int * const ptr = & jj;
cout << “ STEP 1: *ptr = “ << *ptr << endl;

*ptr = 10;
cout << “ STEP 2: *ptr = “ << *ptr << endl; Since this is a constant pointer,
one has to assign immediately.
int jk = 15;
ptr = &jk; // ERROR

cout << “ STEP 3: *ptr = “ << *ptr << endl;


return 0;
}
A few words on C++ and STL : different types of pointers
v Program # 46: pointer10.cpp 3. Constant Pointers – Syntax
int * const ptr;
#include <iostream>
using namespace std;
Ø This means ptr is a constant
int main() pointer
{
int jj = 5;
int * const ptr;

ptr = &jj;
cout << “ STEP 1: *ptr = “ << *ptr << endl;

return 0;
}
A few words on C++ and STL : different types of pointers
v Program # 46: pointer10.cpp 3. Constant Pointers – Syntax
int * const ptr;
#include <iostream>
using namespace std;
Ø This means ptr is a constant
int main() pointer
{
int jj = 5;
int * const ptr;

ptr = &jj;
cout << “ STEP 1: *ptr = “ << *ptr << endl;
Since this is a constant pointer,
one has to assign immediately.
return 0;
}
Compiler message: cannot
initialize a variable of type
'int *const’ with a value of
type 'int'
A few words on C++ and STL : different types of pointers
v Program # 46: pointer11.cpp 3. Constant Pointers – Syntax
const int * const ptr;
#include <iostream>
using namespace std;
Ø This means ptr is a constant
int main() pointer and pointed to a
{
int jj = 5;
constant integer
const int * const ptr = & jj;
cout << “ STEP 1: *ptr = “ << *ptr << endl;

*ptr = 10; // ERROR


Since this is a constant pointer,
int jk = 15; one has to assign immediately.
*ptr = &jk // ERROR

jj = 10;
int * const ptr1;
ptr = &jj;
cout << “ STEP 2: *ptr1 = “ << *ptr1 << endl;

return 0;
}
A few words on C++ and STL
v Program # 301: reading1.cpp v Reading from the screen
#include <iostream>
cin >> jread;
int main()
{
using std::cout;
using std::endl; using namespace std;
using std::cin;

float jread;
cin >> jread;

cout << “jread = “ << jread << endl;

return 0;
}
A few words on C++ and STL
v Program # 302: reading2.cpp v Reading from the screen
#include <iostream>
cin >> jread;
using namespace std;

int main()
{

float jread;
cin >> jread;

cout << “jread = “ << jread << endl;

return 0;
}
A few words on C++ and STL
v Program # 303: reading3.cpp v Reading from a file
#include <iostream>
1 2.5
#include <fstream>
2 6.3
using namespace std;
3 5.7
4 8.5
int main()
5 2.4
{

Ifstream infile(“read.txt”); Ifstream infile(“read.txt”);


int jcol; int jcol;
float fval; float fval;
infile >> jcol >> fval;
while(1)
{
infile >> jcol >> fval;

if (infile.eof()) break;

return 0;
}
A few words on C++ and STL
v Program # 304: reading4.cpp v Reading from a file
#include <iostream>
1 2.5
#include <fstream>
2 6.3
using namespace std;
3 5.7
4 8.5
int main()
5 2.4
{
Ifstream infile(“read.txt”);
Ifstream infile(“read.txt”);
ofstream outfile(“write.txt”);
int jcol;
int jcol;
float fval;
float fval;
infile >> jcol >> fval;
while(1)
{
v Writing to a file
infile >> jcol >> fval;
if (infile.eof()) break;
ofstream infile(“write.txt”);
int jcol;
outfile << jcol << “ “ << fval << endl;
float fval;
outfile << jcol << fval;
}

return 0;
}
A few words on C++ and STL
v Program # 303: reading5.cpp v Reading from a file
#include <iostream>
1 2.5
2 6.3
using namespace std;
3 5.7
4 8.5
int main()
5 2.4
{

FILE *fpr = fopen(“read.txt”,”r”);

int jcol;
float fval;

for(int i = 0; i < 5; i++)


{
fscanf(fpr,”%d %f \n”, &jcol, &fval);
}

return 0;
}
A few words on C++ and STL
v Program # 303: reading6.cpp v Reading from a file
#include <iostream>
1 2.5
2 6.3
using namespace std;
3 5.7
4 8.5
int main()
5 2.4
{

FILE *fpr = fopen(“read.txt”,”r”);


FILE *fpw = fopen(“write.txt”,”w”);

int jcol;
float fval;

for(int i = 0; i < 5; i++)


{
fscanf(fpr,”%d %f \n”, &jcol, &fval);
fprintf(fpw,”%d %f \n”, jcol, fval);

}
return 0;
}
A few words on C++ and STL
Container :

Array : int aa[50]; Limitation


Cann’t be changed inside the program
Pointer: int *aa; Limitation
aa = new int [50]; Need to know a priori the length
---------------
---------------
---------------
delete [] aa;
aa = new int[100];
------------------
------------------
------------------
delete [] aa

If no idea about the length ? Solution - Standard Template Library


A few words on C++ and STL
v STL :

Ø Container : to store data

Ø Algorithm : procedures that are applied to containers to process their


data in various ways.

For example, sort, find,…

Ø Iterator : generalization of the concept of pointers. They point to


elements in the container.

It’s like a cable which connects container and algorithm

Ref : STL Tutorial and Reference Guide / C++ Programming with


the Standard Template Library by David R. Musser, Atul Saini

Generic Programming and the STL by Matthew H. Austern


A few words on C++ and STL

Ø Container : 2 types – sequence and associative

§ Sequence : vector, list, deque

O Array: Fixed dimension/size.


Quick random access, but slow to insert in the middle

Vector: Expandable size


Quick random access, but slow to insert/erase in the middle.
Quick to insert/erase at end.
list: Expandable size
Quick random access, quick to insert/erase at any pint.
Quick to insert/erase at both the ends.
deque: like vector, but can be accessed at either end
Quick random access, slow to insert/erase in the middle.
Quick to insert/erase at both the ends.
A few words on C++ and STL

Ø Container : 2 types – sequence and associative

§ Associative : set, map

set: Stores only the key objects. Only one key of each value allowed.

map: Associates key object with value object.


Only one key of each value allowed.
A few words on C++ and STL
v Program v vector

#include <iostream> 10 20 30 200


#include <vector>
Int main()
{
vector <int> aa;
aa.push_back(10);
aa.push_back(20);
……….
aa.push_back(200);
cout << aa.size() << endl; // prints the dimension of aa
for (int i=0; i<aa.size(); i++)
{
cout << aa[i] << endl; // prints all the elements contained in aa
}

aa.erase(aa.begin(),aa.end()); // erase all the elements contained in aa


aa.clear(); // cleans everything

return 0;
}
A few words on C++ and STL
v Program v list

#include <iostream> 10 20 30 200


#include <list>
Int main()
{
list <int> aa;
aa.push_back(20);
aa.push_back(30);
……….
aa.push_back(200);
aa.push_front(10)
cout << aa.size() << endl; // prints the dimension of aa

while (!aa.empty())
{
cout << aa.front() << endl; // prints all the elements contained in aa
aa.pop_front();
}

return 0;
}
A few words on C++ and STL
v Program v deque

#include <iostream> 10 20 30 200


#include <deque>
Int main()
{
deque <int> aa;
aa.push_back(20);
aa.push_back(30);
……….
aa.push_back(200);
aa.push_front(10);
cout << aa.size() << endl; // prints the dimension of aa
for (int i=0; i<aa.size(); i++)
{
cout << aa[i] << endl; // prints all the elements contained in aa
}

aa.erase(aa.begin(),aa.end()); // erase all the elements contained in aa


aa.clear(); // cleans everything

return 0;
}
A few words on C++ and STL
v Program v Algorithm - sort

#include <iostream>
#include <algorithm>

Int main()
{
int aa[7] = {100, 1, 10, 5, 55, 22, 11};

sort(aa, aa+7); // sorts the elements in ascending order

for (int i=0; i < 7; i++)


{
cout << aa[i] << endl; // prints in ascending order
}

return 0;
}
A few words on C++ and STL
v Class

A class is a collection of variables with related functions.


A class enables us to bundle various parts and various
Functions into one collection which is called an Object.

Ø Let us design a class for a Cube.

Ø A Cube has the following things to calculate: Area, Volume

Ø It has only one parameter, i.e. length


A few words on C++ and STL : design of a class
#include <iostream>
using namespace std;
class Cube
{
public:
float FindArea()
{ v Analysis:
float area = 6.* length * length;
return area;
Ø No data encapsulation
}
float FindVolume()
{
float vol = length * length * length;
Ø Need improvement
return vol;
}

float Length;
int main()
};
{
Cube aa;
aa.length = 5.;
cout << “ area = “ << aa.FindArea() << endl;

return 0;
}
A few words on C++ and STL : design of a class
#include <iostream>
using namespace std;
class Cube
v Analysis:
{
public: Ø aa.Length = 5. will not work
float FindArea()
{ Ø Need to have a public function
float area = 6 * Length * Length; to access the data member
return area;
}
float FindVolume()
{
float vol = Length * Length * Length;
return area;
}
void SetLength(float len)
{
int main()
Length = len;
{
} Cube aa;
private: aa.Length = 5.;
float Length; cout << “ area = “ << aa.FindArea() << endl;
};
return 0;
}
A few words on C++ and STL : design of a class
#include <iostream>
using namespace std;
class Cube
v Analysis:
{
public: Ø You can not check what value
float FindArea() is set to the data member
{
float area = 6.* Length * Length;
Ø Need to have a public function
return area; to access the data member
}
float FindVolume()
{
float vol = Length * Length * Length;
return vol;
}
void SetLength(float len)
int main()
{
{
Length = len; Cube aa;
} float length = 5.;
private: aa.SetLength(length);
float Length; cout << “ area = “ << aa.FindArea() << endl;
};
return 0;
}
A few words on C++ and STL : design of a class
#include <iostream>
using namespace std;
class Cube
v Analysis:
{
public: Ø We are not changing the value
float FindArea()
{
float area = 6.* Length * Length;
Ø Hence, it is better to define it
return area; constant.
}
float FindVolume()
{
float vol = Length * Length * Length;
return vol;
}
void SetLength(float itsLength) int main()
{
{
Cube aa;
Length = itsLength; float length = 5.;
} aa.SetLength(length);
float GetLength() {return Length;} cout << aa.GetLength() << endl;
private: cout << “ area = “ << aa.FindArea() << endl;
float Length;
}; return 0;
}
A few words on C++ and STL : design of a class
#include <iostream>
using namespace std;
class Cube
v Analysis:
{
public: Ø Data members are not initialized
float FindArea()
{
float area = 6.* Length * Length;
Ø Can be done through constructor
return area;
} Ø The moment constructor is defined
float FindVolume()
{
we need to define destructor
float vol = Length * Length * Length;
return vol;
}
void SetLength(float itsLength) int main()
{
{
Cube aa;
Length = itsLength; float length = 5.;
} aa.SetLength(length);
void GetLength() const {return Length;} cout << aa.GetLength() << endl;
private: cout << “ area = “ << aa.FindArea() << endl;
float Length;
}; return 0;
}
#include <cube.h>
A few words on C++ and STL Cube::Cube():
Length(0.),
Area(0.),
cube.h Volume(0.) cube.cxx
{
}
#include <iostream>
Cube::Cube(float itsLength):
using namespace std;
Length(itsLength),
class Cube
Area(0.),
{
Volume(0.)
public:
{
Cube();
}
Cube(float itsLength);
Cube::~Cube()
~Cube();
{
void FindArea();
}
void FindVolume();
void Cube::FindArea()
void SetLength(float itsLength);
{
float GetLength() const;
Area = 6. * Length * Length;
float GetArea() const;
}
float GetVolume() const;
void Cube::SetLength(float itsLength)
{
private:
Length = itsLength;
float Length;
}
float Area;
float Cube::GetLength() const
float Volume;
{
};
return Length;
}
A few words on C++ and STL

Ø How to use it?

#include <iostream>
int main()
{
using std::cout;
using std::endl;
float x = 5.;

Cube aa(x);
aa.FindVolume();
cout << “Volume = “ << aa.GetVolume()
<< endl;

return 0;
}
A few words on C++ and STL

Ø How to use it?

#include <iostream>
int main()
{
using std::cout;
using std::endl;
float x = 5.;

Cube *aa = new Cube(x);


aa->FindVolume();
cout << “Volume = “ << aa->GetVolume()
<< endl;

return 0;
}
A few words on C++ and STL
class Shape
Abstract Class {
public:

Shape
};

Cube Sphere Tube

class Cube:public Shape class Sphere:public Shape How to use it?


{ { #include <iostream>
public: public: int main()
…….. …….. {
Shape *aa;
protected: protected: aa = new Cube;
…….. …….. ………………..
}; }; ………………..
………………..
aa = new Sphere;
……………………
……………………
return 0;
}

You might also like