CSC241: Object Oriented Programming

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
1 CSC241: Object Oriented Programming Lecture No 21.
Introduction to Programming Lecture 39. Copy Constructor.
Destructors Math 130 Lecture # xx Mo/Da/Yr B Smith: New lecture for 05. Use this for evolving to Java B Smith: New lecture for 05. Use this for evolving.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Pointer Data Type and Pointer Variables
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
1 CSC241: Object Oriented Programming Lecture No 22.
Object Oriented Programming with C++/ Session 4/ 1 of 49 Operator Overloading Session 4.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
1 CSC241: Object Oriented Programming Lecture No 02.
1 CSC241: Object Oriented Programming Lecture No 05.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 CSC241: Object Oriented Programming Lecture No 03.
1 CSC241: Object Oriented Programming Lecture No 17.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Learners Support Publications Constructors and Destructors.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
A First Book of C++ Chapter 12 Extending Your Classes.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Pointers and Dynamic Arrays
Visit for more Learning Resources
Visit for more Learning Resources
CSC241: Object Oriented Programming
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
C++, OBJECT ORIENTED PROGRAMMING
Polymorphism & Virtual Functions
Constructors & Destructors
LinkedList Class.
Lecture Constructors and Destructors
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Introduction to Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Contents Introduction to Constructor Characteristics of Constructor
HYBRID INHERITANCE : AMBIGUITY REMOVAL
Pointers, Dynamic Data, and Reference Types
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Chapter 15 Pointers, Dynamic Data, and Reference Types
Inheritance: Polymorphism and Virtual Functions
Constructors and Destructors
CISC/CMPE320 - Prof. McLeod
Polymorphism CMSC 202, Version 4/02.
Object Oriented Programming Using C++
Inheritance: Polymorphism and Virtual Functions
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Introduction to Classes and Objects
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
VIRTUAL FUNCTIONS RITIKA SHARMA.
CIS 199 Final Review.
Inheritance: Polymorphism and Virtual Functions
CS410 – Software Engineering Lecture #8: Advanced C++ III
Destructors, Copy Constructors & Copy Assignment Operators
Pointers, Dynamic Data, and Reference Types
Destructors, Copy Constructors & Copy Assignment Operators
Objects as Function Arguments
Video: The Sound of Raining
Chapter 8 Destructor (P.323) & Operator Overloading
Computer Science II for Majors
Presentation transcript:

CSC241: Object Oriented Programming Lecture No 18

c Previous lecture e house Example program – inheritance Relationship between class Association Aggregation Composition c *emp name ali ID 6 name ali e house name Length Name Width 1 rooms[2]

Today’s Lecture Default copy constructor Memory management String class using new operator Pointer to object Array of pointers to objects Polymorphism

Default Copy constructor Two ways to initialize objects. no-argument constructor Distance d1; multi-argument constructor Distance d2(4, 5,76); An object can be initialize with another object of the same type Distance d1 = d2; A default copy constructor will be called One argument constructor whose argument is an object of same class

Cont. If no copy constructor define in class then complier adds default copy constructor When default copy constructor is invoked When object is pass as an argument to a function Distance d1, d2, d3; … d3.add_dist(d1, d2); When an object is return by value Prototype : Distance add_dist(Distance d); Function call: d3 = d2.add_dist(d1);

Copy constructor—Function Arguments The copy constructor is invoked when an object is passed by value to a function. It creates the copy that the function operates on. Thus if the function void func(alpha a); called by the statement func(a1); then the copy constructor would be invoked to create a copy of the a1 object for use by func(). Copy constructor is not invoked if the argument is pass by reference or by pointer. In these case no copy is created

Copy constructor—Function Return Values The copy constructor also creates a temporary object when a value (an object) is returned from a function. Suppose there was a function alpha func(); and this function was called by the statement a2 = func(); The copy constructor would be invoked to create a copy of the value returned by func(), and this value would be assigned to a2

Memory management It is an act of managing computer memory It provides ways to dynamically allocate memory to programs at their request, and freeing it for reuse when no longer needed In c++ new and delete operator are use to dynamically allocate and deallocate memory for C++ program

A String Class Using new class String { private: char* str; public: String(char* s) { int length = strlen(s); str = new char[length+1]; strcpy(str, s); } ~String() { cout << “Deleting str.\n”; delete[ ] str; void display() { cout << str << endl; }; main() { String s1 = “Information Technology.”; cout << “s1=”; //display string s1.display(); } Program Output s1=Information Technology. Deleting str S1 *str Information Technology.

A problem with String class If you copy one String object to another, say with a statement like s2 = s1, Both objects now point to the same string in memory. But if you delete one string, the destructor will delete the char* string, leaving the other object with an invalid pointer S1 *str Information Technology. S2 *str S1 *str S2 *str Go to program

Pointer to Objects A pointer can point to objects as well as to simple data types and array main() { Distance dist; dist.getdist(); dist.showdist(); } feet inches dist1 0.0 5 6.5 Distance* distptr; *distptr distptr = new Distance; distptr->getdist(); feet inches 0.0 8 distptr->showdist(); 8.56

An Array of Pointers to Objects 4 bytes main() { Distance* d[3]; } 1 2 *d[3] d[0] = new Distance; d[1] = new Distance; d[2] = new Distance; feet inches 0.0 d[0] -> setdist(7, 5.6); d[0] -> showdist(); feet inches 0.0 7 feet inches 0.0 5.6

Polymorphism

Polymorphism It enables to write programs that process objects of classes (that are part of the same class hierarchy) as if they are all objects of the hierarchy's base class. base class D1 class D2 class D3 class

Polymorphism – Example 1 Program that simulates the movement of several types of animals for a biological study Classes Fish, Frog and Bird Each of these classes inherits from base class Animal contains a function move and maintains an animal's current location Each derived class implements function move Program maintains an array of pointers to objects of the various derived classes (Fish, Frog and Bird ) To simulate the animal’s movements, the program sends each object the same message, move()

Cont. Each object knows how to modify its location appropriately for its specific type of movement Relying on each object to know how to "do the right thing" in response to the same function call is the key concept of polymorphism animal fish frog bird

Polymorphism – Example 2 Suppose you have a objects of different classes All are inherited from a base class you want to put them all in an array perform a particular operation on them using the same function call. Shape Circle Square Triangle

Cont. 1 2 3 *ptrArray shape* ptrArray[4]; ptrArray[0] = new Circle; 1 2 3 *ptrArray shape* ptrArray[4]; ptrArray[0] = new Circle; ptrArray[1] = new Triangle; ptrArray[0] = new Square; for(int j=0; j<4; j++) ptrarr[j]->draw(); circle circle triangle square

Cont. This is an amazing capability: Completely different functions are executed by the same function call. If the pointer in ptrarray points to a circle, the function that draws a circle is called if it points to a triangle, the triangle-drawing function is called. This is called polymorphism, which means different forms

Condition for polymorphism Following condition must be meet in order to achieve polymorphic behaviour First, all the different classes of shapes, such as circle and triangles, must be inherited from a single base class Second, the draw() function must be declared to be virtual in the base class. Shape draw() Circle Square Triangle ptrarr[j]->draw();

Example programs Normal Member Functions Accessed with Pointers Virtual Member Functions Accessed with Pointers

Normal Member Functions Accessed class Base { public: void show() { cout << “Base\n”; } }; main() { } *ptr Derv1 dv1; Derv2 dv2; Base* ptr; ptr = &dv1; dv1 dv2 class Derv1 : public Base { public: void show() { cout << “Derv1\n”; } }; ptr->show(); ptr = &dv2; Program Output ptr->show(); Base Base Go to program class Derv2 : public Base { public: void show() { cout << “Derv2\n”; } }; Note: Compiler ignores the contents of the pointer ptr and chooses the member function that matches the type of the pointer

Non-virtual pointer access.

Virtual Member Functions Accessed class Base { public: virtual void show() { cout << “Base\n”; } }; main() { } *ptr Derv1 dv1; Derv2 dv2; Base* ptr; ptr = &dv1; dv1 dv2 class Derv1 : public Base { public: void show() { cout << “Derv1\n”; } }; ptr->show(); ptr = &dv2; Program Output ptr->show(); Derv1 Derv2 Go to program class Derv2 : public Base { public: void show() { cout << “Derv2\n”; } }; Note: Compiler selects the function based on the contents of the pointer ptr, not on the type of the pointer

Virtual pointer access