Lecture 21
Lecture 21
Puru
with
CS101 TAs and Staff
Chapter 17, 18
• Member functions
− New feature introduced in C++
− Actions/operations that effect the entity
};
~Queue(){ //Destructor
if(nWaiting>0) cout << “Warning:”
<<“ non-empty queue being destroyed.”
<< endl;
}
};
int main(){
V3 u(1,2,3), a(4,5,6), s;
double t=10;
s = u*t + a*t*t*0.5;
cout << s.x <<‘ ‘<< s.y << ‘ ‘<< s.z << endl;
}
• dptr = &d1;
• (*dptr).radius = 5; //changes the radius of d1
• Operator ->
– (*x).y is same as x->y
• dptr->radius = 5; // same effect as above
struct Disk2{
double radius;
Point *centerptr;
}
Point p={10,20};
Disk2 d;
d.centerptr = &p;
cout << d.centerptr->x << endl; // will print 10.
• Within the body of a member function, the keyword this points to the receiver
i.e., the struct on which the member function has been invoked.
struct V3{
double x, y, z;
double length(){
return sqrt(this->x * this->x
+ this->y * this->y
+ this->z * this->z);
}
}
};
// only the relevant elements are copied
struct Queue{
private:
int elements[N], nWaiting, front;
public:
Queue(){ … }
bool insert(int v){
..
}
bool remove(int &v){
..
}
};
class Queue{
int elements[N], nWaiting, front;
public:
Queue(){…}
bool remove(int &v){…}
bool insert(int v){…}
};
Chapter 22
• Now you can read from that file by invoking the >> operator!
ofstream outfile(“f2.txt”);
// constructor call. Object outfile is created and associated
// with f2.txt, which will get created in the current directory
repeat(10){
int v;
infile >> v;
outfile << v;
}
// f1.txt must begin with 10 numbers. These will be read and
// written to file f2.txt
}
• >> , <<, !
• put, write
str[5] = ‘\0’;
cout << str; // back to earth!
• Not objects!
CS101 Autumn 2019 @ CSE IIT Bombay 34
the string class
string str = “earth”;
int ans = 0;
for (int nx=0; nx < needles.size(); ++nx) {
char ch = needles[nx];
for (int hx = 0; hx < haystack.size(); ++hx) {
if (ch == haystack[hx]) {
++ans;
break; // quit on first match
}
} // ends haystack loop Generalize to work in
} // ends needles loop case needles can also
}
have repeated
characters
CS101 Autumn 2019 @ CSE IIT Bombay 39
Duplicate needles
• Two approaches
– Dedup needles before executing earlier code (reducing
to known problem)
– Dedup needles “on the fly” (inside the nx loop)
template<typename T>
A common template to unite T Abs(T x) {
them all.... if (x < 0)
return -x;
else return x;
}
CS101 Autumn 2019 @ CSE IIT Bombay 42
Template Class
• Like function templates, create class with templates.
template <class T> main () {
class Queue { Queue<V3> q;
int front, nWaiting; Queue<int> r;
T elements[100]; }
public:
bool insert(T value)
{...}
bool remove(T &val)
{...}
};
vector<int> v(10);
// somehow initialize v
sort(v.begin(), v.end());
double a[100];
// somehow initialize a
• Examples:
map<string,double> population;
Indices will have type string (country names), and elements
will have type double (population)
population[“India”] = 1.21;
// in billions. Map entry created
population[“China”] = 1.35;
population[“USA”] = 0.31;
population[“India”] = 1.22;
//update allowed
if(population.count(country)>0) {
// true if element with index = country
// was stored earlier
// count is a known member function
cout << population[country] << endl;
}
else cout << “Not known.\n”;
• If you wish to print all entries stored in a map, you will need
map<string,double>::iterator mi;
mi = population.begin();
// population.begin() : constant iterator
// points to the first element of population
// mi points to (India,1.21)
for(map<string,double>::iterator
mi = population.begin();
mi != population.end(); mi++)
for(map<string,double>::iterator
mi = population.begin();
mi != population.end();
mi++)
{
cout << (*mi).first << “: “ << (*mi).second << endl;
// or cout << mi->first << “: “ << mi->second << endl;
}
// will print out countries and population in
alphabetical order
map<string,double>::iterator
mi = population.find("India");
population.erase(mi);
• The library classes are very useful. Get some practice with them