Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();

Slides:



Advertisements
Similar presentations
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Advertisements

Operator overloading redefine the operations of operators
Class and Objects.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 15: Operator Overloading
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator overloading Object Oriented Programming.
Operator Overloading in C++
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Review of C++ Programming Part II Sheng-Fang Huang.
Chapter 10 Defining Classes. User-defined Types Are data types defined by programmers. Include: – typedef: simple data definition – struct: a structure.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
11 Introduction to Object Oriented Programming (Continued) Cats.
Class Miscellanea Details About Classes. Review We’ve seen that a class has two sections: class Temperature { public: //... public members private: //...
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
CONSTRUCTOR AND DESTRUCTORS
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
CS 132 Spring 2008 Chapter 2 Object-Oriented Design (OOD) and C++ Ideas: * Inheritance (and protected members of a class) ** Operator overloading Pointer.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Friend Functions.  An ordinary function that is given special access to the private members of a class  NOT a member function of the class  Prototype.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
11 Introduction to Object Oriented Programming (Continued) Cats.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Operator Overloading What is operator overloading? Most predefined operators (arithmetic, logic, etc.) in C++ can be overloaded when applied to objects.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
group work #hifiTeam
Introduction to Classes
Operator Overloading; String and Array Objects
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Operator Overloading; String and Array Objects
(4 – 2) Introduction to Classes in C++
Presentation transcript:

Chapter 11 Friends and Overloaded Operators

Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate(); // default constructor CDate(int m, int d, int y); // parameterized constructor CDate(const CDate& aDay); // copying constructor ~CDate(); // destructor int getMonth(); // accessor int getDay(); // accessor void setMonth(int m); // mutator void setDay(int d); // mutator void display(); // a member function int year; private: int month; int day; }; #endif #include “Date.h” bool equal (CDate day1, CDate day2); void main () { CDate myBD(3, 31, 1996), yourBD; cout << "Enter your birthday as month, day, year ”; int m, d; cin >> m >> d >> yourBD.year; yourBD.setMonth(m); yourBD.setDay(d); if (equal(myBD, yourBD)) cout << “Let’s celebrate together.”; } bool equal (CDate day1, CDate day2) { return (day1.getMonth() == day2.getMonth()) && day1. getDay() == day2. getDay()) && day1.year == day2.year); }

Function equal prototype of equal is outside of the class. equal is a nonmember function of the class requiring 2 objects To access the members, it must still use member accessor functions since the attributes are private and can only be accessed by public member functions. It does not change the data members. Only member functions or friend functions should be able to alter private members.

Friend functions A non-member function that has membership privileges. Notes: – Prototype goes in public section with keyword friend in front – Function call is like a regular function - does not use dot operator – When to use: Functions between two members of the same class

Friend function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate(); // default constructor CDate(int m, int d, int y); // parameterized constructor CDate(const CDate& aDay); // copying constructor ~CDate(); // destructor friend bool equal(CDate day1, CDate day2); int getMonth(); // accessor int getDay(); // accessor void setMonth(int m); // mutator void setDay(int d); // mutator void display(); // a member function int year; private: int month; int day; }; #endif #include “Date.h” void main () { CDate myBD(3, 31, 1996), yourBD; cout << "Enter your birthday as month, day, year ”; int m, d; cin >> m >> d >> yourBD.year; yourBD.setMonth(m); yourBD.setDay(d); if (equal(myBD, yourBD)) cout << “Let’s celebrate together.”; } bool equal (CDate day1, CDate day2) { return (day1.month == day2.month) && day1. day == day2. day) && day1.year == day2.year); }

The const Parameter Modifier It is more efficient to call by reference than by value. – value: passes in a copy of the variable. If the variable is large or structured this takes up extra space. – reference: passes in the address only but it does allow changes Solution: call by reference but put const modifier which prevents change - generates error message. Used for classes, structs and arrays. const used after function prototype and definition prevents function from changing calling objects - generates error message. – void output (ostream& outs) const; If the function prototype and it’s definition do not both have the const modifier, then you will get a linkage error. Accessor and output functions should be const.

Friend function equal with const // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate(); // default constructor CDate(int m, int d, int y); // parameterized constructor CDate(const CDate& aDay); // copying constructor ~CDate(); // destructor friend bool equal(const CDate& day1, const CDate& day2); int getMonth(); // accessor int getDay(); // accessor void setMonth(int m); // mutator void setDay(int d); // mutator void display(); // a member function int year; private: int month; int day; }; #endif #include “Date.h” void main () { CDate myBD(3, 31, 1996), yourBD; cout << "Enter your birthday as month, day, year ”; int m, d; cin >> m >> d >> yourBD.year; yourBD.setMonth(m); yourBD.setDay(d); if (equal(myBD, yourBD)) cout << “Let’s celebrate together.”; } bool equal (const CDate& day1, const CDate& day2) { return (day1.month == day2.month) && day1. day == day2. day) && day1.year == day2.year); }

Overloading Operator Changing the definition of an operator from the default operation to a new operation. Allows for smooth functionality in ADT’s.

Overloading Operator (example) Want to write as: if (myBD == yourBD) // syntax error Current operation == does not take operands typed CDate. Need to redefine == operator to take 2 CDate objects. Function definition is exactly the same as equal. Declared as a friend function. Has the keyword operator.

Friend function equal with const // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate(); // default constructor CDate(int m, int d, int y); // parameterized constructor CDate(const CDate& aDay); // copying constructor ~CDate(); // destructor friend bool operator ==(const CDate& day1, const CDate& day2); int getMonth(); // accessor int getDay(); // accessor void setMonth(int m); // mutator void setDay(int d); // mutator void display(); // a member function int year; private: int month; int day; }; #endif #include “Date.h” void main () { CDate myBD(3, 31, 1996), yourBD; cout << "Enter your birthday as month, day, year ”; int m, d; cin >> m >> d >> yourBD.year; yourBD.setMonth(m); yourBD.setDay(d); if (myBD == yourBD) cout << “Let’s celebrate together.”; } bool operator ==(const CDate& day1, const CDate& day2) { return (day1.month == day2.month) && day1. day == day2. day) && day1.year == day2.year); }

Rules for Overloading Operators At least one argument must be of the class type. Overloaded operators can be friends or member functions. Can only overload existing operators Can’t change the number of arguments from the original operator (binary (+), unary (++)) Can’t change precedence rules Can’t overload dot operator(.), scope resolution operator (::). Overloading the assignment operator is a special case.

Overloading Unary Operators Has only 1 object in the parameter list: – Negate: - – Increment: ++ – Decrement: --

Overloading >> and << The compiler does not automatically know how to output or input user-defined classes. Need to specify how each attribute is to be entered. Want input to be user friendly. – Ex: cin >> yourBD; // as format mm-dd-yyyy Want output to be clear and in standard form. – Ex: cout << yourBD; // as format mm/dd/yyyy

Arrays and Classes An array can be of any type. Composite types (struct or class) can be stored in arrays. Ex: CDate studentBDs[25]; int n=5; if (studentBDs[n] == myBD) if (studentBDs[n].year < 1998) if (studentBDs[n].getMonth() == 3)

Arrays as Class Member Any composite type can contain an array. Ex class CStudentInfo { … char name; SDate bDay; float quiz[5]; }; CStudentInfo students[25], myBestStudent; myBestStudent.quiz[0] = 10; students[2].quiz[0] = 8;

Class CComplex definition //Complex.h #ifndef _COMPLEX_ #define _COMPLEX_ #include using namespace std; class CComplex { public: CComplex(void); CComplex(double, double); CComplex(const CComplex&); ~CComplex(); // Unary operator to negate the values of a complex number: // -(r, i) = (-r, -i) friend CComplex operator - (const CComplex& c); // operator to add 2 complex numbers: // (r1, i1) + (r2, i2) = (r1+r2, i1+i2) friend CComplex operator + (const CComplex& left, const CComplex& right); // operator to subtract 2 complex numbers: // (r1, i1) - (r2, i2) = (r1-r2, i1-i2) friend CComplex operator - (const CComplex& left, const CComplex& right); // operator to multiply 2 complex numbers: // (r1, i1)(r2, i2) = (r1r2-i1i2, i1r2+r1i2) friend CComplex operator * (const CComplex& left, const CComplex& right); // operator to compare if 2 complex numbers are the same friend bool operator == (const CComplex& left, const CComplex& right); // extraction operator to get input for real, imaginary friend istream& operator >> (istream& ins, CComplex& c); // insertion operator to display a complex number as (a + bi) // where a is real and b is imag friend ostream& operator << (ostream& outs, const CComplex& c); private: double real; double imag; }; #endif

Class CComplex implementation CComplex::CComplex(void) : real(0), imag(0) {} CComplex::CComplex(double r, double i) : real(r), imag(i) {} CComplex::CComplex(const CComplex& c) real(c.real), imag(c.imag) {} CComplex::~CComplex() {} CComplex operator - (const CComplex& c) { CComplex temp; temp.real = -c.real; temp.imag = -c..imag; return temp; } CComplex operator + (const CComplex& left, const CComplex& right) { CComplex temp; temp.real = left.real + right.real; temp.imag = left.imag + right.imag; return temp; } CComplex operator - (const CComplex& left, const CComplex& right) { CComplex temp; temp.real = left.real - right.real; temp.imag = left.imag - right.imag; return temp; } CComplex operator * (const CComplex& left, const CComplex& right) { CComplex temp; temp.real = left.real*right.real - left.imag*right.imag; temp.imag = left.imag*right.real + left.real*right.imag; return temp; } bool operator == (const CComplex& left, const CComplex& right) { return (left.real == right.real) && (left.imag == right.imag); } istream& operator >> (istream& ins, CComplex& c) { ins >> c.real >> c.imag; return ins; } ostream& operator << (ostream& outs, const CComplex& c) { outs.setf(ios::fixed); outs.setf(ios::showpoint); outs.precision(2); outs << '(' << c.real << ", "; if (c.imag > 0.0) outs << '+'; outs << c.imag << “i)”; return outs; }

Class CComplex implementation (used parameterized constructor) CComplex::CComplex(void) : real(0), imag(0) {} CComplex::CComplex(double r, double i) : real(r), imag(i) {} CComplex::CComplex(const CComplex& c) real(c.real), imag(c.imag) {} CComplex::~CComplex() {} CComplex operator - (const CComplex& c) { return CComplex(-c.real, -c.imag); } CComplex operator + (const CComplex& left, const CComplex& right) { return CComplex((left.real + right.real), (left.imag + right.imag)); } CComplex operator - (const CComplex& left, const CComplex& right) { return CComplex(left.real - right.real, left.imag - right.imag); } CComplex operator * (const CComplex& left, const CComplex& right) { return CComplex((left.real*right.real - left.imag*right.imag), (left.imag*right.real + left.real*right.imag)); } bool operator == (const CComplex& left, const CComplex& right) { return (left.real == right.real) && (left.imag == right.imag); } istream& operator >> (istream& ins, CComplex& c) { ins >> c.real >> c.imag; return ins; } ostream& operator << (ostream& outs, const CComplex& c) { outs.setf(ios::fixed); outs.setf(ios::showpoint); outs.precision(2); outs << '(' << c.real << ", "; if (c.imag > 0.0) outs << '+'; outs << c.imag << “i)”; return outs; }

Class with pointer data members #include "Date.h" CDate::Date () : month(1), day(1), year(new int(1900)) {} CDate::Date (int m, int d, int y) : month(1), day(1), year(new int(y)) {} CDate::~Date () {} int CDate::getMonth() { return month; } int CDate::getMonth() { return month; } int CDate::getDay() { return day; } void CDate::setMonth(int m) { month = m; } …. class CDate { public: CDate(); // default constructor CDate(int m, int d, int y); // parameterized constructor ~CDate(); // destructor int getMonth(); // accessor int getDay(); // accessor void setMonth(int m); // mutator void setDay(int d); // mutator void display(); // a member function int *year; private: int month; int day; };

Destructor Every class needs to tell the compiler how to destroy the object and return the memory to the freestore. Destructor is used to destroy a class object and return the memory to the heap. – In the case of standard variables, it will automatically return the memory to the heap and the destructor is empty. – In the case of dynamic variables, it will destroy the pointer but not return the memory to the heap unless we use the delete command. With dynamic variables, only the pointer would be deleted and not what it points to. Need to write the destructor specifically and use delete. Automatically called when scope ends

Destructor (example) class CDate { public: CDate(); // default constructor CDate(int m, int d, int y); // parameterized constructor ~CDate(); // destructor int getMonth(); // accessor int getDay(); // accessor void setMonth(int m); // mutator void setDay(int d); // mutator void display(); // a member function int *year; private: int month; int day; }; #include "Date.h" CDate::Date () : month(1), day(1), year(new int(1900)) {} CDate::Date (int m, int d, int y) : month(1), day(1), year(new int(y)) {} CDate::~Date () { cout <<“Bye-bye: ”<<month<<“-”<<day<<“-”<<*year<<endl; } … #include "Date.h“ void main () { CDate myBD (1, 1, 1990); CDate yourBD (12, 31, 2000); } Bye-bye: Bye-bye: Output: So, what happens to the memory location reserved for year? Still occupied

Destructor (example) class CDate { public: CDate(); // default constructor CDate(int m, int d, int y); // parameterized constructor ~CDate(); // destructor int getMonth(); // accessor int getDay(); // accessor void setMonth(int m); // mutator void setDay(int d); // mutator void display(); // a member function int *year; private: int month; int day; }; #include "Date.h" CDate::Date () : month(1), day(1), year(new int(1900)) {} CDate::Date (int m, int d, int y) : month(1), day(1), year(new int(y)) {} CDate::~Date () { cout <<“Bye-bye: ”<<month<<“-”<<day<<“-”<<*year<<endl; delete year; } … #include "Date.h“ void main () { CDate myBD (1, 1, 1990); CDate yourBD (12, 31, 2000); } Bye-bye: Bye-bye: Output: So, what happens to the memory address reserved for year? Freed

Copy Constructor Creates a copy of an object in a different memory location. Parameter list has one variable that is an object of the class. The parameter is preceded by const and is a call by reference. – Ex: CDate (const CDate& d); Called automatically whenever a function has a parameter of the class type. – Ex: CDate myBD(yourBD); Any classes using pointers and new operator (dynamic variables) should have a copy constructor. Otherwise, the compiler will only copy the pointers so they both have the same address (both point to the same memory address, rather than each point to the same data but in different addresses. This is called a “shallow copy”).

Copy Constructor (example) #include "Date.h“ CDate::CDate () : month(1), day(1), year(new int(1900)) {} CDate::CDate (int m, int d, int y) : month(1), day(1), year(new int(y)) {} CDate::~CDate () { delete year; } CDate::CDate (const CDate& d) : month(d.month), day(d.day), year(d.year) {} CDate::~CDate () { delete year; } … class CDate { public: CDate(); // default constructor CDate(int m, int d, int y); // parameterized constructor CDate(const CDate&); // copy constructor ~CDate(); // destructor int getMonth(); // accessor int getDay(); // accessor void setMonth(int m); // mutator void setDay(int d); // mutator void display(); // a member function int *year; private: int month; int day; };

Copy Constructor (const.) #include "Date.h“ void main () { CDate myBD (1, 1, 1990); { CDate yourBD (myBD); } cout << “My birth year is: ” << myBD.year << endl; } What happens to the memory address reserved for myBD’s year? Destroyed by yourBD’s destructor Output: ??? (garbage) Why garbage??? myBD’s year yourBD’s year 1900 myBD’s year yourBD’s year ? Both myBD’s year and yourBD’s year point to the same memory address. When one is destroyed, the other becomes dangling pointer. 10,000

Copy Constructor (example) class CDate { public: CDate(); // default constructor CDate(int m, int d, int y); // parameterized constructor CDate(const CDate&); // copy constructor ~CDate(); // destructor int getMonth(); // accessor int getDay(); // accessor void setMonth(int m); // mutator void setDay(int d); // mutator void display(); // a member function int *year; private: int month; int day; }; #include "Date.h" CDate::CDate () : month(1), day(1), year(new int(1900)) {} CDate::CDate (int m, int d, int y) : month(1), day(1), year(new int(y)) {} CDate::~CDate () { delete year; } CDate::CDate (const CDate& d) : month(d.month), day(d.day) { year = new int; *year = *(d.year); } CDate::~CDate () { delete year; } …

Copy Constructor (const.) #include "Date.h“ void main () { CDate myBD (1, 1, 1990); { CDate yourBD (myBD); } cout << “My birth year is: ” << myBD.year << endl; } What happens to the memory address reserved for myBD’s year? Still there and used by myBD’s year Output: ??? 1990 Why exception NOT raised??? myBD’s year yourBD’s year 1900 myBD’s year yourBD’s year ? Both myBD’s year points to a different memory address, not related to the address of yourBD’s year. 10, , ,000

Overloading the Assignment Operator If NOT overloaded, the compiler will create one and that assignment operator will copy all the values of data members of the assigner (on the right) to the corresponding ones of the assignee (on the left). The “shallow copy” will happen if a data member is a pointer. So, the assignment operator needs to be overloaded to assign the pointer to another memory address. void CDate::operator = (const CDate& d) { month = d.month; day = d.day; year = d.year; }

Assignment Operator not overloaded #include "Date.h“ void main () { CDate myBD (1, 1, 1990); {CDate yourBD = myBD; } cout << “My birth year is: ” << myBD.year << endl; } What happens to the memory address reserved for myBD’s year? Destroyed by yourBD’s destructor Output: ??? (garbage) Why garbage??? Both myBD’s year and yourBD’s year point to the same memory address. When one is destroyed, the other becomes dangling pointer. myBD’s year yourBD’s year 1900 myBD’s year yourBD’s year ? 10,000

Overloaded Assignment Operator (example) class CDate { public: CDate(); // default constructor CDate(int m, int d, int y); // parameterized constructor CDate(const CDate&); // copy constructors ~CDate(); // destructor int getMonth(); // accessor int getDay(); // accessor void setMonth(int m); // mutator void setDay(int d); // mutator void display(); // a member function void operator = (const CDate& d); // Assignment op. int *year; private: int month; int day; }; #include "Date.h" CDate::CDate () : month(1), day(1), year(new int(1900)) {} CDate::CDate (int m, int d, int y) : month(1), day(1), year(new int(y)) {} CDate::~CDate () { delete year; } CDate::CDate (const CDate& d) : month(d.month), day(d.day) { year = new int; *year = *(d.year); } CDate::~CDate () { delete year; } void CDate::operator = (const CDate& d) { month = d.month; day = d.day; year = new int; *year = *(d.year); } …

Overloaded Assignment Operator (const.) #include "Date.h“ void main () { CDate myBD (1, 1, 1990); { CDate yourBD (myBD); } cout << “My birth year is: ” << myBD.year << endl; } What happens to the memory address reserved for myBD’s year? Still there and used by myBD’s year Output: ??? 1990 Why exception NOT raised??? myBD’s year yourBD’s year 1900 myBD’s year yourBD’s year ? Both myBD’s year points to a different memory address, not related to the address of yourBD’s year. 10, , ,000

The Big Three Copy constructor, assignment operator and destructor are the big three of a class. Rule that if you need to define one of them, you need to define all of them or the compiler will create them but they might not work correctly!