Download presentation
Presentation is loading. Please wait.
Published bySimon Lyons Modified over 9 years ago
1
CSC241 Object-Oriented Programming (OOP) Lecture No. 12
2
Date Class class Date{ int day, month, year; static Date defaultDate; public: void SetDay(int aDay); int GetDay() const; void AddDay(int x); … static void SetDefaultDate(int aDay, int aMonth, int aYear); private: bool IsLeapYear(); };
3
Date Class int main(){ Date aDate; aDate.IsLeapYear();//Error return 0; }
4
Creating SpecialDate Class Special Date DateSpecial Date AddSpecialYear...
5
Creating SpecialDate Class class SpecialDate : public Date{ … public: void AddSpecialYear(int i){... if (day == 29 && month == 2 && !IsLeapyear(year + i)){ //ERROR!... } };
6
Modify Access Specifier We can modify access specifier “IsLeapYear” from private to public
7
Modified Date Class class Date{ public:... bool IsLeapYear(); };
8
Modified AddSpecialYear void SpecialDate::AddSpecialYear(int i){... if(day == 29 && month == 2 && !IsLeapyear(year+i)){... }
9
Protected members Protected members can not be accessed outside the class Protected members of base class become protected member of derived class in Public inheritance
10
Modified Date Class class Date{ … protected: bool IsLeapYear(); }; int main(){ Date aDate; aDate.IsLeapYear(); //Error return 0; }
11
Modified AddSpecialYear void SpecialDate::AddSpecialYear(int i){... if (day == 29 && month == 2 && !IsLeapyear(year + i)){... }
12
Disadvantages Breaks encapsulation The protected member is part of base class’s implementation as well as derived class’s implementation
13
“IS A” Relationship Public inheritance models the “IS A” relationship Derived object IS A kind of base object
14
Example class Person{ char * name; public:... const char * GetName(); }; class Student : public Person{ int rollNo; public:... int GetRollNo(); };
15
Example int main() { Student sobj; cout << sobj.GetName(); cout << sobj.GetRollNo(); return 0; }
16
“IS A” Relationship The base class pointer can point towards an object of derived class
17
Example int main(){ Person * pPtr = 0; Student s; pPtr = &s; cout GetName(); return 0; }
18
Example pPtr = &s ; derived member1 derived member2... base member1 base member2... s pPtr
19
Example int main(){ Person * pPtr = 0; Student s; pPtr = &s; //Error cout GetRollNo(); return 0; }
20
Static Type The type that is used to declare a reference or pointer is called its static type The static type of pPtr is Person The static type of s is Student
21
Member Access The access to members is determined by static type The static type of pPtr is Person Following call is erroneous pPtr->GetRollNo();
22
“IS A” Relationship We can use a reference of derived object where the reference of base object is required
23
Example int main(){ Person p; Student s; Person & refp = s; cout << refp.GetName(); cout << refp.GetRollNo(); //Error return 0; }
24
Example void Play(const Person& p){ cout << p.GetName()<< " is playing"; } void Study(const Student& s){ cout << s.GetRollNo() << " is Studying"; }
25
Example int main(){ Person p; Student s; Play(p); Play(s); return 0; }
26
Example class Person{ char * name; public: Person(char * = NULL); const char * GetName() const; ~Person(); };
27
Example class Student : public Person{ char* major; public: Student(char *, char *); void Print() const; ~Student(); };
28
Example Student::Student(char *_name, char *_maj) : Person(_name), major(NULL) { if (_maj != NULL) { major = new char[strlen(_maj) + 1]; strcpy(major, _maj); }
29
Example void Student::Print() const{ cout << "Name: " << GetName() << endl; cout << "Major: " << major << endl; }
30
Example int main(){ Student sobj1("Ali", "Computer Science"); { Student sobj2 = sobj1; //Student sobj2(sobj1); sobj2.Print(); } return 0; }
31
Example The output is as follows: Name: Ali Major: Computer Science
32
Copy Constructor Compiler generates copy constructor for base and derived classes, if needed Derived class Copy constructor is invoked which in turn calls the Copy constructor of the base class The base part is copied first and then the derived part
33
Shallow Copy... major name sobj2 ALIALI... major name sobj1 C O M...
34
Example Person::Person(const Person& rhs){ // Code for deep copy } int main(){ Student sobj1("Ali", "Computer Science"); Student sobj2 = sobj1; sobj2.Print(); return 0; }
35
Example The output is as follows: Name: Ali Major: Computer Science
36
Copy Constructor Compiler generates copy constructor for derived class, calls the copy constructor of the base class and then performs the shallow copy of the derived class’s data members
37
Shallow Copy... major name sobj2 ALIALI... major name sobj1 C O M... ALIALI
38
Example Person::Person(const Person& rhs) { // Code for deep copy } Student::Student(const Student& rhs) { // Code for deep copy }
39
Example int main(){ Student sobj1("Ali","Computer Science"); Student sobj2 = sobj1; sobj2.Print(); return 0; }
40
Copy Constructor The output will be as follows: Name: Major: Computer Science Name of sobj2 was not copied from sobj1
41
Copy... major name sobj2 ALIALI... major name sobj1 C O M... C O M...
42
Modified Default Constructor Person::Person(char * aName){ if (aName == NULL) cout << "Person Constructor";... } int main(){ Student s("Ali", "Computer Science"); … }
43
Copy Constructor The output of previous code will be as follows: Person Constructor Name: Major: Computer Science
44
Copy Constructor Programmer must explicitly call the base class copy constructor from the copy constructor of derived class
45
Example Person::Person(const Person & prhs) { // Code for deep copy } Student::Student(const Student & srhs) :Person(srhs) { // Code for deep copy }
46
Example main function shown previously will give following output Name: Ali Major: Computer Science
47
Copy... major name sobj2 ALIALI... major name sobj1 C O M... ALIALI C O M...
48
Copy Constructors Person::Person(const Person &rhs) : name(NULL) { //code for deep copy } Student::Student(const Student & rhs) : major(NULL), Person(rhs){ //code for deep copy } 34516273451627
49
Example int main() { Student sobj1, sboj2("Ali", "CS"); sobj1 = sobj2; return 0; }
50
Assignment Operator Compiler generates copy assignment operator for base and derived classes, if needed Derived class copy assignment operator is invoked which in turn calls the assignment operator of the base class The base part is assigned first and then the derived part
51
Assignment Operator Programmer has to call operator of base class, if he is writing assignment operator of derived class
52
Example class Person{ public: Person & operator = (const Person & rhs){ cout << "Person Assignment"; // Code for deep copy assignment } };
53
Example class Student : public Person{ public: Student & operator = (const Student & rhs){ cout << “Student Assignment”; // Code for deep copy assignment } };
54
Example int main() { Student sobj1, sboj2("Ali", "CS"); sobj1 = sobj2; return 0; }
55
Example The assignment operator of base class is not called Output Student Assignment
56
Assignment Operator There are two ways of writing assignment operator in derived class Calling assignment operator of base class explicitly Calling assignment operator of base class implicitly
57
Calling Base Class Member Function Base class functions can be explicitly called with reference to base class itself //const char* Person::GetName() {...}; void Student::Print() { cout << GetName(); cout << Person::GetName(); }
58
Explicitly Calling operator = Person & Person::operator = (const Person & prhs); Student & Student ::operator = (const Student & srhs){ Person::operator = (srhs); … return *this; }
59
Implicitly Calling operator = Student & Student ::operator = (const Student & srhs) { static_cast *this = srhs; // Person(*this) = srhs; // (Person)*this = srhs; … }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.