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

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

Introduction to Programming Lecture 39. Copy Constructor.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Class and Objects.
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.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CSC241 Object-Oriented Programming (OOP) Lecture No. 12.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
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.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
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.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
CSC241 Object-Oriented Programming (OOP) Lecture No. 8.
 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
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
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.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Learners Support Publications Constructors and Destructors.
Object-Oriented Programming (OOP) Lecture No. 24.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
SEG4110 – Advanced Software Design and Reengineering TOPIC I Introduction to C++ For those who know Java And OO Principles in General.
Object Oriented Programming (OOP) Lecture No.2 Downloaded From:
Yan Shi CS/SE 2630 Lecture Notes
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Pointers and Dynamic Arrays
Learning Objectives Pointers as dada members
Object Oriented Programming (OOP) Lecture No. 8
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
Chapter 15 Pointers, Dynamic Data, and Reference Types
Object Oriented Programming (OOP) Lecture No. 9
Constructors and Destructors
Constructors and Destructors
Object oriented programming (OOP) Lecture No. 7
CS410 – Software Engineering Lecture #5: C++ Basics III
Introduction to Data Structures and Software Engineering
Object Oriented Programming (OOP) Lecture No. 11
Object oriented programming (OOP) Lecture No. 6
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Object Oriented Programming (OOP) Lecture No. 12
Object Oriented Programming (OOP) Lecture No. 10
SPL – PS3 C++ Classes.
Presentation transcript:

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

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: Shallow Copy Student studentA(“Ahmad”); GPA Name RollNo studentB Heap AHMADAHMAD GPA Name RollNo studentA Student studentB = studentA;

Example: Shallow Copy int main(){ Student studentA(“Ahmad”, 1); { Student studentB = studentA; GPA Name RollNostudentB Heap AHMADAHMAD GPA Name RollNo studentA

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

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: Deep Copy

int main(){ Student studentA(“Ahmad”, 1); { Student studentB = studentA; AHMADAHMAD GPA Name RollNo studentA GPA Name RollNostudentB AHMADAHMAD Example: Deep Copy

int main(){ Student studentA(“Ahmad”, 1); { Student studentB = studentA; } } Heap GPA Name RollNo studentA AHMADAHMAD Example: Deep Copy

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

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

Example – Avoiding Error void Student::setRollNo(int aRollNo){ if (aRollNo < 0){ rollNo = 0; } else { rollNo = aRollNo; } }

Example - Getter class Student{ … int rollNo; public: int getRollNo(){ return rollNo; } };

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; 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: s1 rollNo, … Function Space getRollNo(), … s2 rollNo, … s3 rollNo, … s4 rollNo, … 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 rollNo, … s3 rollNo, … s4 rollNo, … address 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; } Student::Student(){ this->rollNo = 0; }

Review  Copy constructors  Destructor  Accessor Functions  this Pointer

this Pointer  There are situations where designer wants to return reference to current object from a function  In such cases reference is taken from this pointer like ( *this )

Example Student Student::setRollNo(int aNo) { … return *this; } Student Student::setName(char *aName) { … return *this; }

Example int main() { Student aStudent; Student bStudent; bStudent = aStudent.setName(“Ahmad”); … bStudent = aStudent.setName(“Ali”).setRollNo(2); return 0; }

Separation of interface and implementation  Public member function exposed by a class is called interface  Separation of implementation from the interface is good software engineering

Complex Number  There are two representations of complex number  Euler form  z = x + i y  Phasor form  z = |z| (cos  + i sin  )  z is known as the complex modulus and  is known as the complex argument or phase

Example float getX() float getY() void setNumber (float i, float j) (float i, float j)… float x float x float y float y Complex Old implementation float getX() float getY() void setNumber (float i, float j) (float i, float j)… float z float z float theta float theta Complex New implementation

Example class Complex{ //old float x; float y; public: void setNumber(float i, float j){ x = i; y = j; } … };

Example class Complex{ //new float z; float theta; public: void setNumber(float i, float j){ z = sqrt((i ^ 2) + (j ^ 2)); theta = arctan(j / i); } … };

Advantages  User is only concerned about ways of accessing data (interface)  User has no concern about the internal representation and implementation of the class

Separation of interface and implementation  Usually functions are defined in implementation files (.cpp) while the class definition is given in header file (.h)  Some authors also consider this as separation of interface and implementation

Student.h class Student{ int rollNo; public: void setRollNo(int aRollNo); int getRollNo(); … };

Student.cpp #include “student.h” void Student::setRollNo(int aNo){ … } int Student::getRollNo(){ … }

Driver.cpp #include “student.h” int main(){ Student aStudent; }

const Member Functions  There are functions that are meant to be read only  There must exist a mechanism to detect error if such functions accidentally change the data member

const Member Functions  Keyword const is placed at the end of the parameter list

const Member Functions Declaration: class ClassName{ ReturnVal Function() const; }; Definition: ReturnVal ClassName::Function() const{ … }

Example class Student{ public: int getRollNo() const{ return rollNo; } };

const Functions  Constant member functions cannot modify the state of any object  They are just “read-only”  Errors due to typing are also caught at compile time

Example bool Student::isRollNo(int aNo){ if (rollNo == aNo){ return true; } return false; }

Example bool Student::isRollNo(int aNo){ /*undetected typing mistake*/ if (rollNo = aNo){ return true; } return false; }

Example bool Student::isRollNo(int aNo)const{ /*compiler error*/ if (rollNo = aNo){ return true; } return false; }

const Functions  Constructors and Destructors cannot be const  Constructor and destructor are used to modify the object to a well defined state

Example class Time{ public: Time() const {} //error… ~Time() const {} //error… };

const Function  Constant member function cannot change data member  Constant member function cannot access non-constant member functions

Example class Student{ char * name; public: char *getName(); void setName(char * aName); int ConstFunc() const{ name = getName();//error setName(“Ahmad”);//error } };

this Pointer and const Member Function  this pointer is passed as constant pointer to const data in case of constant member functions const Student *const this; instead of Student *const this;