Object Oriented Programming (OOP) Lecture No. 9

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Introduction to Programming Lecture 39. Copy Constructor.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Classes Separating interface from implementation
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.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
CSC241 Object-Oriented Programming (OOP) Lecture No. 12.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
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+
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
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.
More C++ Features True object initialisation
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
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.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CONSTRUCTOR AND DESTRUCTOR. Topics to be discussed……………….. 1. Introduction Introduction 2. FeaturesFeatures 3. Types of ConstructorTypes of Constructor.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
Learners Support Publications Constructors and Destructors.
1 Ugly Realities The Dark Side of C++ Chapter 12.
Object Oriented Programming (OOP) Lecture No.2 Downloaded From:
Memory Management.
Yan Shi CS/SE 2630 Lecture Notes
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Pointers and Dynamic Arrays
Learning Objectives Pointers as dada members
Overloading Operator MySting Example
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Object Oriented Programming (OOP) Lecture No. 8
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamic Memory Allocation Reference Variables
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Object-Oriented Programming (OOP) Lecture No. 14
Constructors and Destructors
Constructors and Destructors
Object-Oriented Programming (OOP) Lecture No. 22
Destructor CSCE 121.
Web Design & Development Lecture 4
CS410 – Software Engineering Lecture #5: C++ Basics III
COP 3330 Object-oriented Programming in C++
Object Oriented Programming (OOP) Lecture No. 11
Object oriented programming (OOP) Lecture No. 6
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
CS 144 Advanced C++ Programming April 30 Class Meeting
Object Oriented Programming (OOP) Lecture No. 12
Object Oriented Programming (OOP) Lecture No. 10
SPL – PS3 C++ Classes.
Presentation transcript:

Object Oriented Programming (OOP) Lecture No. 9

Review Member functions implementation Constructors Constructors overloading Copy constructors

Copy Constructor Copy constructor are used when: Initializing an object at the time of creation When an object is passed by value to a function

Example void func1(Student student){ … } int main(){ Student studentA(“Ahmad”); Student studentB = studentA; func1(studentA);

Copy Constructor (contd.) Student::Student( const Student &obj){ rollNo = obj.rollNo; name = obj.name; GPA = obj.GPA; }

Shallow Copy When we initialize one object with another then the compiler copies state of one object to the other This kind of copying is called shallow copying

Example Student studentA(“Ahmad”); Student studentB = studentA; Heap A H M D GPA Name RollNn studentA GPA Name RollNn studentB

Example studentA studentB int main(){ Student studentA(“Ahmad”,1); { Student studentB = studentA; Heap A H M D GPA Name RollNn studentA GPA Name RollNn studentB

Example studentA int main(){ Student studentA(“Ahmad”,1); { Student studentB = studentA; } } Heap GPA Name RollNn studentA

Copy Constructor (contd.) Student::Student( const Student & obj){ int len = strlen(obj.name); name = new char[len+1] strcpy(name, obj.name); … /*copy rest of the data members*/ }

Example studentB studentA int main(){ Student studentA(“Ahmad”,1); { Student studentB = studentA; A H M D GPA Name RollNn studentA GPA Name RollNn studentB A H M D

Example studentA int main(){ Student studentA(“Ahmad”,1); { Student studentB = studentA; } } Heap GPA Name RollNn studentA A H M D

Copy Constructor (contd.) Copy constructor is normally used to perform deep copy If we do not make a copy constructor then the compiler performs shallow copy

Destructor Destructor is used to free memory that is allocated through dynamic allocation Destructor is used to perform house keeping operations

Destructor (contd.) Destructor is a function with the same name as that of class, but preceded with a tilde ‘~’

Example class Student { … public: ~Student(){ if(name){ delete []name; }

Destructors cannot be overloaded Overloading Destructors cannot be overloaded

Sequence of Calls Constructors and destructors are called automatically Constructors are called in the sequence in which object is declared Destructors are called in reverse order

Example Student::Student(char * aName){ … cout << aName << “Cons\n”; } Student::~Student(){ cout << name << “Dest\n”; };

Example int main() { Student studentB(“Ali”); Student studentA(“Ahmad”); return 0; }

Example Output: Ali Cons Ahmad Cons Ahmad Dest Ali Dest

Accessor Functions Usually the data member are defined in private part of a class – information hiding Accessor functions are functions that are used to access these private data members Accessor functions also useful in reducing error

Example – Accessing Data Member class Student{ … int rollNo; public: void setRollNo(int aRollNo){ rollNo = aRollNo; } }; Never return a handle to a data member You are never sure that function accessing the reference will not change the value of the variable

Example – Avoiding Error void Student::setRollNo(int aRollNo){ if(aRollNo < 0){ rollNo = 0; } else { rollNo = aRollNo; Never return a handle to a data member You are never sure that function accessing the reference will not change the value of the variable

Example - Getter class Student{ … int rollNo; public: int getRollNo(){ return rollNo; } }; Never return a handle to a data member You are never sure that function accessing the reference will not change the value of the variable

this Pointer class Student{ int rollNo; char *name; float GPA; public: int getRollNo(); void setRollNo(int aRollNo); … };

this Pointer The compiler reserves space for the functions defined in the class Space for data is not allocated (since no object is yet created) Function Space getRollNo(), …

this Pointer Student s1, s2, s3; s2(rollNo,…) Function Space getRollNo(), … s1(rollNo,…) s2(rollNo,…) s3(rollNo,…)

this Pointer Function space is common for every variable Whenever a new object is created: Memory is reserved for variables only Previously defined functions are used over and over again

this Pointer Memory layout for objects created: rollNo, … Function Space getRollNo(), … s2 s3 s4 How does the functions know on which object to act?

this Pointer Address of each object is passed to the calling function This address is deferenced by the functions and hence they act on correct objects address s1 rollNo, … s2 s3 s4 The variable containing the “self-address” is called this pointer

Passing this Pointer Whenever a function is called the this pointer is passed as a parameter to that function Function with n parameters is actually called with n+1 parameters

Example void Student::setName(char *) is internally represented as void Student::setName(char *, const Student *)

Declaration of this DataType * const this;

Compiler Generated Code Student::Student(){ rollNo = 0; } this->rollNo = 0;