Professional Documents
Culture Documents
Session 6 - Class Basics Contd : Deep Copies Custom Iostream Operators Conversion Operators
Session 6 - Class Basics Contd : Deep Copies Custom Iostream Operators Conversion Operators
Session 6 - Class Basics Contd : Deep Copies Custom Iostream Operators Conversion Operators
CONTD…
DEEP COPIES
CUSTOM IOSTREAM OPERATORS
CONVERSION OPERATORS
Event Loops - ( WS5 )
WinMain(){
while(!done){
if(‘R’)… // user pressed ‘R’
if(‘S’)… // user pressed ‘S’
if(‘L’)…
… [other keys]
… [other mouse clicks]
…
if(‘Clicked close’){
done = true;
}
}
Command line main()function
int main(){
step1();
step2();
step3();
cout << “done”;
return 0;
}
Detecting Keyboard Events ( WS5 )
ISBN
Shallow copies copy the ADDRESS of pointers
but they don’t copy the object they point TO.
(deep copy) Order
Order
ISBN* nbr; = ISBN* nbr;
ISBN = ISBN
Deep copies make copies of each data object
instead of just copying pointers to data objects.
If you want to do deep copies, you need to write
a copy constructor.
Order::Order(Order& source) {
// new ISBN object
this->isbn = ISBN(source.isbn);
ordered = source.ordered;
this->delivered = source.delivered;
// You don’t have to say “this”
}
If you want to do deep copies, you need to write a
copy constructor and an assignment operator.
Order::Order(Order& source) {
// new ISBN object
this->isbn = ISBN(source.isbn);
ordered = source.ordered;
this->delivered = source.delivered;
// You don’t have to say “this”
}
If you want to do deep copies, you need to write a
copy constructor and an assignment operator.
Order Order
ISBN* nbr; = ISBN* nbr;
(shallow copy)
ISBN
Resources
a pointer !
Copying
• If we were to make a shallow copy of a Student
object, the original and the copy would point to the
same resource: grade pointer
• If any changes the grades in the copy, the original
referred to the changed grades and no longer to the
original grades.
int main () {
ISBN foo = ISBN(“9330440556”);
class Student {
int no;
int semester;
char grade[M+1];
public:
Student();
friend ostream& operator<<(ostream& os,
const Student& s);
};
Overloading the << operator
return os;
}
Overloading the << operator
Int main () {
Student hans(00212,”ABCDF”);
cout << hans; // now it works!
}
Cascading
Card a, b, c, d, e;
foo = a + b + c + d; // foo = 33
}
}
Student nobita(13443,”BDCDF”);
Student doraemon(33333,”AABAD”);
void addCards(Card a,
Card b, Card * c ) {
*c = a + b;
return c; // this works
// it works because c comes from
// outside the function,
// so it doesn’t get deleted
// when the function closes.
}
String Input overflow
• The Solution
The string class addresses this indefinite-size
problem. An object of the string class accepts as
many characters as the user enters and allocates
as much memory as is needed to store the set of
characters.
The string class requires #include <string> for the
prototypes.
String Input overflow…
#include <string>
int main( ) {
string str;
getline(cin, str);
…
}
Conversion Operators
int main() {
int n;
Student hans(1234, “ABABF”);
Student::Student(int no) {
set(no, "");
} // single argument constructor
Student::Student(int no) {
set(no, "");
} // single argument constructor
?
Conversion Sequence…
Student harry = 1234; // ??
• In searching for a conversion the compiler steps
through definite stages. The compiler looks for
an exact match (for example, int to int, Student to
Student),
a promoted match (for example, char to int, float to
double),
a standard conversion match (for example, int to
double, int to float),
a derived data type conversion match (for example,
int to Student). ( This is the one it uses for the
example shown above.)
Explicit Constructors
// doesn’t work
Student harry = 1234;
// works
Student harry = Student(1234);