Object-Oriented Programming (OOP) Lecture No. 24.

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.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Operator Overloading Fundamentals
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
What is a copy constructor? A copy constructor is a special constructor for a class/struct that is used to make a copy of an existing instance during initialization.
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
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.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
CSC241 Object-Oriented Programming (OOP) Lecture No. 12.
The Rest of the Story.  Constructors  Compiler-generated  The Initializer List  Copy Constructors  Single-arg (conversion ctors)  The Assignment.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
Dale Roberts 1 Classes Constructors & Destructors Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer.
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
Operator Overloading Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
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+
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
CSC241 Object-Oriented Programming (OOP) Lecture No. 8.
Object Oriented Programming (OOP) Lecture No. 11.
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.
More C++ Features True object initialisation
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
Object Management. Constructors –Compiler-generated –The Initializer List –Copy Constructors –Single-arg (conversion ctors) The Assignment Operator.
Programming in C++ Michal Brabec Petr Malý. Class / Struct Class / Struct consists of: data members function members constructors destructor copy constructor.
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.
1 2/21/05CS250 Introduction to Computer Science II Destructors, Get and Set, and Default Memberwise Assignment.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Constructors Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
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.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
CSC241 Object-Oriented Programming (OOP) Lecture No. 22.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Object Oriented Programming (OOP) Lecture No.2 Downloaded From:
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
Classes Copy Constructors
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
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
Inheritance: Polymorphism and Virtual Functions
Object-Oriented Programming (OOP) Lecture No. 22
Classes Static Members
CS148 Introduction to Programming II
Passing Arguments and The Big 5
Dynamic Memory Copy Challenge
Passing Arguments and The Big 5
Lab – 2018/04/12 implement getTotalCount to return how many ‘nMatrix’s are allocated(exist) (modify constructor and destructor, use static variable and.
Object oriented programming (OOP) Lecture No. 6
Object-Oriented Programming (OOP) Lecture No. 23
Presentation transcript:

Object-Oriented Programming (OOP) Lecture No. 24

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; … }