Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review of Last Lecture. What we have learned last lecture? What does a constructor do? What is the way to define a constructor? Can it be defined under.

Similar presentations


Presentation on theme: "Review of Last Lecture. What we have learned last lecture? What does a constructor do? What is the way to define a constructor? Can it be defined under."— Presentation transcript:

1 Review of Last Lecture

2 What we have learned last lecture? What does a constructor do? What is the way to define a constructor? Can it be defined under a "private" section? How many different constructors one class can have? How to initialize member variables in a constructor? How to manage dynamic memory allocation? Why do we need destructor and how to define it? Using "const" in class and define member functions with "const" type. Why do we need that? Using "static" members in class. Why do we need that? Overloaded operator functions. Why do we need that? What is the difference when overloading operator functions as the member functions vs. non-member functions? Friend functions (especially for overloaded operators). How do we define them? Why do we need that?

3 class Student { private: int peopleSoft; float score; public: void input(int number1,float score1){ peopleSoft =number1; score=score1; } void output(){ cout<<"number:"<< peopleSoft <<"score:"<<score<<endl; } }; Student s1, s2; Is the following definition and initialization of the objects legal or not based on the previous definition?

4 class Student { private: int peopleSoft; float score; public: void input(int number1,float score1){ peopleSoft =number1; score=score1; } void output(){ cout<<"number:"<< peopleSoft <<"score:"<<score<<endl; } }; Student s1, s2; Is the following definition and initialization of the objects legal or not based on the previous definition? Yes.

5 class Student { private: int peopleSoft; float score; public: Student(int number1, float score1): peopleSoft(number1), score(score1){} void input(int number1,float score1){ peopleSoft=number1; score=score1; } void output(){ cout<<"number:"<< peopleSoft <<"score:"<<score<<endl; } }; Student s1, s2; Is the following still legal?

6 class Student { private: int peopleSoft; float score; public: Student(int number1, float score1): peopleSoft(number1), score(score1){} void input(int number1,float score1){ peopleSoft=number1; score=score1; } void output(){ cout<<"number:"<< peopleSoft <<"score:"<<score<<endl; } }; Student s1, s2; Is the following still legal? No, there is no a default constructor defined!

7 class Student { private: int peopleSoft; float score; public: Student(int number1=0, float score1=0): peopleSoft(number1), score(score1){} void input(int number1,float score1){ peopleSoft=number1; score=score1; } void output(){ cout<<"number:"<< peopleSoft <<"score:"<<score<<endl; } }; Student s1, s2;

8 #include class clock { private: int hour, minute, second; public: clock(int h, int m=0, int s=0); void set(int h, int m, int s); void display( ); void ~clock(); }; clock(int h, int m=0, int s=0) : hour(h), minute(m), second(s) { cout<<“Call constructor.”<<endl; } void clock:: set (int h,int m,int s) { hour=h; minute=m; second=s; } void clock::display( ) { cout<<hour<<":"<<minute<<":"<<second<<endl; } void clock::~clock() { cout<<“Call destructor.”<<endl; } //main function void main( ) { clock clockA, clockB; clockA. set (6,30,0); clockB. set (12,0,0); //output clockA.display( ); clockB.display( ); } What is the output?

9 #include class clock { private: int hour, minute, second; public: clock(int h, int m=0, int s=0); void set(int h, int m, int s); void display( ); void ~clock(); }; clock(int h, int m=0, int s=0) : hour(h), minute(m), second(s) { cout<<“Call constructor.”<<endl; } void clock:: set (int h,int m,int s) { hour=h; minute=m; second=s; } void clock::display( ) { cout<<hour<<":"<<minute<<":"<<second<<endl; } void clock::~clock() { cout<<“Call destructor.”<<endl; } //main function void main( ) { clock clockA, clockB; clockA. set (6,30,0); clockB. set (12,0,0); //output clockA.display( ); clockB.display( ); } It cannot be compiled!

10 #include class clock { private: int hour, minute, second; public: clock(int h=0, int m=0, int s=0); void set(int h, int m, int s); void display( ); ~clock(); }; clock::clock(int h=0, int m=0, int s=0) : hour(h), minute(m), second(s) { cout<<“Call constructor.”<<endl; } void clock:: set (int h,int m,int s) { hour=h; minute=m; second=s; } void clock::display( ) { cout<<hour<<":"<<minute<<":"<<second<<endl; } clock::~clock() { cout<<“Call destructor.”<<endl; } //main function void main( ) { clock clockA, clockB; clockA. set (6,30,0); clockB. set (12,0,0); //output clockA.display( ); clockB.display( ); } What is the output?

11 #include class clock { private: int hour, minute, second; public: clock(int h=0, int m=0, int s=0); void set(int h, int m, int s); void display( ); ~clock(); }; clock::clock(int h=0, int m=0, int s=0) : hour(h), minute(m), second(s) { cout<<“Call constructor.”<<endl; } void clock:: set (int h,int m,int s) { hour=h; minute=m; second=s; } void clock::display( ) { cout<<hour<<":"<<minute<<":"<<second<<endl; } clock::~clock() { cout<<“Call destructor.”<<endl; } //main function void main( ) { clock clockA, clockB; clockA. set (6,30,0); clockB. set (12,0,0); //output clockA.display( ); clockB.display( ); } Call constructor 6:30:0 12:0:0 Call destructor

12 #include class clock { private: int hour, minute, second; public: clock(int h=0, int m=0, int s=0); void set(int h, int m, int s); void display( ); ~clock(); }; clock::clock(int h=0, int m=0, int s=0) : hour(h), minute(m), second(s) { cout<<“Call constructor.”<<endl; } void clock:: set (int h,int m,int s) { hour=h; minute=m; second=s; } void clock::display( ) { cout<<hour<<":"<<minute<<":"<<second<<endl; } clock::~clock() { cout<<“Call destructor.”<<endl; } //main function void main( ) { clock clockA, clockB; clockA. set (6,30,0); clockB. set (12,0,0); clock clockC = clockA+clockB; } Is the above calculation valid or not?

13 #include class clock { private: int hour, minute, second; public: clock(int h=0, int m=0, int s=0); void set(int h, int m, int s); void display( ); ~clock(); }; clock::clock(int h=0, int m=0, int s=0) : hour(h), minute(m), second(s) { cout<<“Call constructor.”<<endl; } void clock:: set (int h,int m,int s) { hour=h; minute=m; second=s; } void clock::display( ) { cout<<hour<<":"<<minute<<":"<<second<<endl; } clock::~clock() { cout<<“Call destructor.”<<endl; } //main function void main( ) { clock clockA, clockB; clockA. set (6,30,0); clockB. set (12,0,0); clock clockC = clockA+clockB; } //overload + operator! const clock operator +(const &clock1, const &clock2) { return clock(clock1.hour+clock2.hour, clock1.minute+clock2.minute clock1.second+clock2.second); } Will the above work?

14 #include class clock { private: int hour, minute, second; public: clock(int h=0, int m=0, int s=0); void set(int h, int m, int s); void display( ); ~clock(); friend const clock operator +(const &clock1, const &clock2); }; clock::clock(int h=0, int m=0, int s=0) : hour(h), minute(m), second(s) { cout<<“Call constructor.”<<endl; } void clock:: set (int h,int m,int s) { hour=h; minute=m; second=s; } void clock::display( ) { cout<<hour<<":"<<minute<<":"<<second<<endl; } clock::~clock() { cout<<“Call destructor.”<<endl; } //main function void main( ) { clock clockA, clockB; clockA. set (6,30,0); clockB. set (12,0,0); clock clockC = clockA+clockB; } //overload + operator! const clock operator +(const &clock1, const &clock2) { return clock(clock1.hour+clock2.hour, clock1.minute+clock2.minute clock1.second+clock2.second); } set it as a friend function!

15 class Student { public: Student (); //default constructor Student (int id); Student (int id, const char name[20]); ~Student(); // destructor …… private: // define private member variables int peopleSoft; char *name; …… }; Student::Student (int id, const char name[20]) //constructor : peopleSoft(id) { this->name = new char[20]; //dynamic memory allocation (to a pointer variable) strcpy(this->name, name); } //main function void main( ) { Student s1(12345, “john smith”), s2; s2 = s1; } Student::~Student() //destructor { if (name != NULL) // we need to correctly release the dynamically allocated memory delete [] name; }

16 class Student { public: Student (); //default constructor Student (int id); Student (int id, const char name[20]); ~Student(); // destructor …… private: // define private member variables int peopleSoft; char *name; …… }; Student::Student (int id, const char name[20]) //constructor : peopleSoft(id) { this->name = new char[20]; //dynamic memory allocation (to a pointer variable) strcpy(this->name, name); } //main function void main( ) { Student s1(12345, “john smith”), s2; s2 = s1; } Student::~Student() //destructor { if (name != NULL) // we need to correctly release the dynamically allocated memory delete [] name; } The above assignment will fail. To address that, we need to implement the overloaded function for the operator ‘=‘.

17 class Student { public: Student (); //default constructor Student (int id); Student (int id, const char name[20]); ~Student(); // destructor …… private: // define private member variables int peopleSoft; char *name; …… }; Student::Student (int id, const char name[20]) //constructor : peopleSoft(id) { this->name = new char[20]; //dynamic memory allocation (to a pointer variable) strcpy(this->name, name); } //main function void main( ) { Student s1(12345, “john smith”), s2; s2 = s1; } Student::~Student() //destructor { if (name != NULL) // we need to correctly release the dynamically allocated memory delete [] name; } The above assignment will fail. Because the default assignment operator will not automatically take care of the memory allocation for pointer type of member variable. So what could happen here?


Download ppt "Review of Last Lecture. What we have learned last lecture? What does a constructor do? What is the way to define a constructor? Can it be defined under."

Similar presentations


Ads by Google