Lecture 03
Lecture 03
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
#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;
*ptr = 10;
cout << “ STEP 2: *ptr = “ << *ptr << endl;
int jk = 15;
ptr = &jk;
*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
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;
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;
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;
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
{
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
{
int jcol;
float 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
{
int jcol;
float fval;
}
return 0;
}
A few words on C++ and STL
Container :
set: Stores only the key objects. Only one key of each value allowed.
return 0;
}
A few words on C++ and STL
v Program v list
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
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};
return 0;
}
A few words on C++ and STL
v Class
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
#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
#include <iostream>
int main()
{
using std::cout;
using std::endl;
float x = 5.;
return 0;
}
A few words on C++ and STL
class Shape
Abstract Class {
public:
Shape
};