Object Oriented Programming (OOP) Lecture No. 10

Slides:



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

F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Web Application Development Slides Credit Umair Javed LUMS.
C++ Programming Languages
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CSC241 Object-Oriented Programming (OOP) Lecture No. 12.
1 Object-Oriented Programming Using C++ CLASS 27.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
1 CSC241: Object Oriented Programming Lecture No 06.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
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.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
1 Lecture 3 More about C++. 2 Topic for today More about classMore about class –Init list –Inline functions –Const –Default function parameters –Static.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
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.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
1 COMS 261 Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
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:
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Learning Objectives Pointers as dada members
C++ Programming Functions
Review What is an object? What is a class?
Introduction to C++ Systems Programming.
Object Oriented Programming (OOP) Lecture No. 8
Object-Oriented Programming (OOP) Lecture No. 21
A First C++ Class – a Circle
Review: Two Programming Paradigms
Introduction to Classes
Class: Special Topics Copy Constructors Static members Friends this
More about OOP and ADTs Classes
This technique is Called “Divide and Conquer”.
Memberwise Assignment / Initialization
Introduction to Classes
Object Oriented Programming (OOP) Lecture No. 9
More about OOP and ADTs Classes
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Object Oriented Programming (OOP) Lecture No. 13
Object-Oriented Programming (OOP) Lecture No. 22
Object oriented programming (OOP) Lecture No. 7
Submitted By : Veenu Saini Lecturer (IT)
Web Design & Development Lecture 4
A Deeper Look at Classes
Object Oriented Programming (OOP) Lecture No. 11
Object oriented programming (OOP) Lecture No. 6
Object-Oriented Programming (OOP) Lecture No. 23
Separating Interface from Implementation
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Lecture 8 Object Oriented Programming (OOP)
Object Oriented Programming (OOP) Lecture No. 12
Classes and Objects Systems Programming.
Object Oriented Programming
Presentation transcript:

Object Oriented Programming (OOP) Lecture No. 10

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)

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 Complex Old implementation Complex New implementation float getX() float getY() void setNumber (float i, float j) … float x float y Complex Old implementation float getX() float getY() void setNumber (float i, float j) … float z 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){ 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… };

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;