cs101 Lecture14
cs101 Lecture14
• Constructors
• Copy Constructors
• Destructors
• Operator overloading
• Overloading the assignment operator
• Access control
• Classes
• Graphics and input/output classes
Motivational Example: The Queue
Struct in Taxi Dispatch
const int N=100;
struct queue{ • Once the queue is
int elements[N], created, we expect it to
be used only through the
nwaiting,front;
member functions, insert
bool insert(int v){ and remove
… • We do not expect
} elements, nWaiting, front
to be directly accessed
book remove(int &v){
…
}
};
Main Program Using Queue
int main(){ • Main program does use q through
Queue q; operations insert and remove
q.front = q.nWaiting = 0; • However, at the beginning, q.front and
while(true){ q.nWaiting is directly manipulated
char c; cin >> c; • This is against the philosophy of software
if(c == ‘d’){ packaging
int driver; cin >> driver; • When we create a queue, we will always
if(!q.insert(driver)) set q.nWaiting and q.front to 0
cout <<“Q is full\n”; • C++ provides a way by which the
} initialization can be made to happen
else if(c == ‘c’){ automatically, and also such that
int driver; programs using Queue do not need to
if(!q.remove(driver)) access the data members directly
cout <<“No taxi.\n”; • Just defining Queue q; would by itself set
else cout <<“Assigning << q.nWaiting and q.front to 0!
driver<< endl; – Next
}
}
Constructor Example
• In C++, the programmer may define a struct Queue{
special member function called a int elements[N], front,
constructor which will always be nWaiting;
called when an instance of the struct Queue(){ // constructor
is created nWaiting = 0;
• A constructor has the same name as front = 0;
the struct, and no return type }
• The code inside the constructor can // other member functions
perform initializations of members };
• When q is created in the main int main(){
program, the constructor is called Queue q;
automatically // no need to set
// q.nWaiting, q.front
// to 0.
}
Constructors In General
struct Queue{
int elements[N], nWaiting, front;
…
~Queue(){ //Destructor
if(nWaiting>0) cout << “Warning:”
<<“ non-empty queue being destroyed.”
<< endl;
}
};
Operator Overloading
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;
}
Remarks
struct Queue{
...
Queue & operator=(Queue &rhs){
front = rhs.front;
nWaiting = rhs.nWaiting;
for(int i=0; i<nWaiting; i++){
elements[i] = rhs.elements[i];
i = (i+1) % N;
}
return *this;
}
};
// only the relevant elements are copied
Access Control
class Queue{
int elements[N], nWaiting, front;
public:
Queue(){…}
bool remove(int &v){…}
bool insert(int v){…}
};
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
}
Concluding Remarks
• The notion of a packaged software component is important.
• Making data members private: hiding the implementation
from the user
• Making some member functions public: providing an
interface using which the object can be used
• Separation of the concerns of the developer and the user
• Idea similar to what we discussed in connection with
ordinary functions
– The specification of the function must be clearly written
down (analogous to interface)
– The user should not worry about how the function does its
work (analogous to hiding data members)