CSC241 Object-Oriented Programming (OOP) Lecture No. 12.

Slides:



Advertisements
Similar presentations
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Advertisements

Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
Introduction to Programming Lecture 39. Copy Constructor.
Web Application Development Slides Credit Umair Javed LUMS.
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
OOP Etgar 2008 – Recitation 61 Object Oriented Programming Etgar 2008 Recitation 6.
OOP Spring 2007 – Recitation 41 Object Oriented Programming Spring 2007 Recitation 4.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Inheritence Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September.
CONSTRUCTORS AND THEIR TYPES. Prepared by. MURLI MANOHAR. PGT (COMP
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+
C++ Lecture 4 Tuesday, 15 July Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
CSC241 Object-Oriented Programming (OOP) Lecture No. 8.
Object Oriented Programming (OOP) Lecture No. 11.
Object Oriented Programming (OOP) Lecture No. 10.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
CSC241 Object-Oriented Programming (OOP) Lecture No. 7.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Chapter 9 Classes: A Deeper Look, Part I Part II.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Inheritance.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Object Management. Constructors –Compiler-generated –The Initializer List –Copy Constructors –Single-arg (conversion ctors) The Assignment Operator.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
CSC241 Object-Oriented Programming (OOP) Lecture No. 14.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
The Slicing Problem CS240 Computer Science II. Some relationship between base and derived classes: data members Derived class inherits data members of.
Object-Oriented Programming (OOP) Lecture No. 24.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Object Oriented Programming (OOP) Lecture No.2 Downloaded From:
Chapter 2 Objects and Classes
Yan Shi CS/SE 2630 Lecture Notes
Object-Oriented Programming (OOP) Lecture No. 15
Object Oriented Programming (OOP) Lecture No. 8
Object Oriented Programming (OOP) Lecture No. 9
Object-Oriented Programming (OOP) Lecture No. 14
Department of Computer and Information Science, School of Science, IUPUI CSCI 265 Classes Dale Roberts, Lecturer Computer Science, IUPUI
Object-Oriented Programming (OOP) Lecture No. 25
Object Oriented Programming (OOP) Lecture No. 13
Inheritance: Polymorphism and Virtual Functions
Object-Oriented Programming (OOP) Lecture No. 22
CS148 Introduction to Programming II
CS 144 Advanced C++ Programming February 21 Class Meeting
Object Oriented Programming (OOP) Lecture No. 11
Object oriented programming (OOP) Lecture No. 6
Object-Oriented Programming (OOP) Lecture No. 23
Object-Oriented Programming (OOP) Lecture No. 27
Presentation transcript:

CSC241 Object-Oriented Programming (OOP) Lecture No. 12

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(); };

Date Class int main(){ Date aDate; aDate.IsLeapYear();//Error return 0; }

Creating SpecialDate Class Special Date DateSpecial Date AddSpecialYear...

Creating SpecialDate Class class SpecialDate : public Date{ … public: void AddSpecialYear(int i){... if (day == 29 && month == 2 && !IsLeapyear(year + i)){ //ERROR!... } };

Modify Access Specifier  We can modify access specifier “IsLeapYear” from private to public

Modified Date Class class Date{ public:... bool IsLeapYear(); };

Modified AddSpecialYear void SpecialDate::AddSpecialYear(int i){... if(day == 29 && month == 2 && !IsLeapyear(year+i)){... }

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

Modified Date Class class Date{ … protected: bool IsLeapYear(); }; int main(){ Date aDate; aDate.IsLeapYear(); //Error return 0; }

Modified AddSpecialYear void SpecialDate::AddSpecialYear(int i){... if (day == 29 && month == 2 && !IsLeapyear(year + i)){... }

Disadvantages  Breaks encapsulation  The protected member is part of base class’s implementation as well as derived class’s implementation

“IS A” Relationship  Public inheritance models the “IS A” relationship  Derived object IS A kind of base object

Example class Person{ char * name; public:... const char * GetName(); }; class Student : public Person{ int rollNo; public:... int GetRollNo(); };

Example int main() { Student sobj; cout << sobj.GetName(); cout << sobj.GetRollNo(); return 0; }

“IS A” Relationship  The base class pointer can point towards an object of derived class

Example int main(){ Person * pPtr = 0; Student s; pPtr = &s; cout GetName(); return 0; }

Example pPtr = &s ; derived member1 derived member2... base member1 base member2... s pPtr

Example int main(){ Person * pPtr = 0; Student s; pPtr = &s; //Error cout GetRollNo(); return 0; }

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

Member Access  The access to members is determined by static type  The static type of pPtr is Person  Following call is erroneous pPtr->GetRollNo();

“IS A” Relationship  We can use a reference of derived object where the reference of base object is required

Example int main(){ Person p; Student s; Person & refp = s; cout << refp.GetName(); cout << refp.GetRollNo(); //Error return 0; }

Example void Play(const Person& p){ cout << p.GetName()<< " is playing"; } void Study(const Student& s){ cout << s.GetRollNo() << " is Studying"; }

Example int main(){ Person p; Student s; Play(p); Play(s); return 0; }

Example class Person{ char * name; public: Person(char * = NULL); const char * GetName() const; ~Person(); };

Example class Student : public Person{ char* major; public: Student(char *, char *); void Print() const; ~Student(); };

Example Student::Student(char *_name, char *_maj) : Person(_name), major(NULL) { if (_maj != NULL) { major = new char[strlen(_maj) + 1]; strcpy(major, _maj); }

Example void Student::Print() const{ cout << "Name: " << GetName() << endl; cout << "Major: " << major << endl; }

Example int main(){ Student sobj1("Ali", "Computer Science"); { Student sobj2 = sobj1; //Student sobj2(sobj1); sobj2.Print(); } return 0; }

Example  The output is as follows: Name: Ali Major: Computer Science

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

Shallow Copy... major name sobj2 ALIALI... major name sobj1 C O M...

Example Person::Person(const Person& rhs){ // Code for deep copy } int main(){ Student sobj1("Ali", "Computer Science"); Student sobj2 = sobj1; sobj2.Print(); return 0; }

Example  The output is as follows: Name: Ali Major: Computer Science

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

Shallow Copy... major name sobj2 ALIALI... major name sobj1 C O M... ALIALI

Example Person::Person(const Person& rhs) { // Code for deep copy } Student::Student(const Student& rhs) { // Code for deep copy }

Example int main(){ Student sobj1("Ali","Computer Science"); Student sobj2 = sobj1; sobj2.Print(); return 0; }

Copy Constructor  The output will be as follows: Name: Major: Computer Science  Name of sobj2 was not copied from sobj1

Copy... major name sobj2 ALIALI... major name sobj1 C O M... C O M...

Modified Default Constructor Person::Person(char * aName){ if (aName == NULL) cout << "Person Constructor";... } int main(){ Student s("Ali", "Computer Science"); … }

Copy Constructor  The output of previous code will be as follows: Person Constructor Name: Major: Computer Science

Copy Constructor  Programmer must explicitly call the base class copy constructor from the copy constructor of derived class

Example Person::Person(const Person & prhs) { // Code for deep copy } Student::Student(const Student & srhs) :Person(srhs) { // Code for deep copy }

Example  main function shown previously will give following output Name: Ali Major: Computer Science

Copy... major name sobj2 ALIALI... major name sobj1 C O M... ALIALI C O M...

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 }

Example int main() { Student sobj1, sboj2("Ali", "CS"); sobj1 = sobj2; return 0; }

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

Assignment Operator  Programmer has to call operator of base class, if he is writing assignment operator of derived class

Example class Person{ public: Person & operator = (const Person & rhs){ cout << "Person Assignment"; // Code for deep copy assignment } };

Example class Student : public Person{ public: Student & operator = (const Student & rhs){ cout << “Student Assignment”; // Code for deep copy assignment } };

Example int main() { Student sobj1, sboj2("Ali", "CS"); sobj1 = sobj2; return 0; }

Example  The assignment operator of base class is not called  Output Student Assignment

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

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(); }

Explicitly Calling operator = Person & Person::operator = (const Person & prhs); Student & Student ::operator = (const Student & srhs){ Person::operator = (srhs); … return *this; }

Implicitly Calling operator = Student & Student ::operator = (const Student & srhs) { static_cast *this = srhs; // Person(*this) = srhs; // (Person)*this = srhs; … }