1 CSC241: Object Oriented Programming Lecture No 05.

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

1 CSC241: Object Oriented Programming Lecture No 21.
Introduction to Programming Lecture 39. Copy Constructor.
 2008 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
1 CSC241: Object Oriented Programming Lecture No 28.
Classes: A Deeper Look Systems Programming.  constconst  const objects and const member functions   Composition   Friendship  this  this pointer.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
1 Lecture Note 7- Classes Part II Outline Composition: Objects as Members of Classes Dynamic Memory static Class Members Operator Overloading.
Classes: A Deeper Look Systems Programming.
A Deeper Look at Classes CS-2303, C-Term A Deeper Look at Classes CS-2303 System Programming Concepts (Slides include materials from The C Programming.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Review of C++ Programming Part II Sheng-Fang Huang.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
 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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
1 CSC241: Object Oriented Programming Lecture No 13.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Classes: A Deeper Look, Part But what, to serve our private ends, Forbids the cheating of our friends? ◦ Charles Churchill Instead of this absurd.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Pointers & Dynamic Arrays Shinta P.
1 CSC241: Object Oriented Programming Lecture No 06.
1 CSC241: Object Oriented Programming Lecture No 22.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Object-Oriented Programming in C++
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
More C++ Features True object initialisation
Chapter 9 Classes: A Deeper Look, Part I Part II.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
1 CSC241: Object Oriented Programming Lecture No 02.
CS 1704 Introduction to Data Structures and Software Engineering.
Review 1 List Data Structure List operations List Implementation Array Linked List.
1 CSC241: Object Oriented Programming Lecture No 11.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 CSC241: Object Oriented Programming Lecture No 03.
Structs and Classes Structs A struct can be used to define a data structure type as follows: struct Complex { double real, imag;} // specifying a Complex.
Programming Techniques Classes II Important Class Features Spring 2009.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 7: Classes Part II Outline 7.1 Introduction 7.2 const (Constant) Objects and const Member Functions.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
Data Structures in C++ Pointers & Dynamic Arrays Shinta P.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Dynamic Storage Allocation
Visit for more Learning Resources
Lecture 5: Classes (continued) Feb 1, 2005
CSC241: Object Oriented Programming
CSC241: Object Oriented Programming
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
10.2 const (Constant) Objects and const Member Functions
Object Oriented Programming Using C++
Recitation Course 0520 Speaker: Liu Yu-Jiun.
Recitation Course 0603 Speaker: Liu Yu-Jiun.
A Deeper Look at Classes
Pointers, Dynamic Data, and Reference Types
Objects as Function Arguments
Presentation transcript:

1 CSC241: Object Oriented Programming Lecture No 05

2 Previous Lecture Overloaded function – Constructor const (constant) – object – member function – data member – object as function argument friend function this pointer

3 Today’s Lecture Reference variable this pointer – Cascading function calls Dynamic memory allocation static class member – static functions – static data member sdaf

4 C++ Reference variable C++ references allow you to create a second name for a memory location that you can use to read or modify the original data stored in that location Syntax – int& foo =....; – reference to an int When a reference is created, you must tell it which variable it will become an alias for

5 Cont. – int x; – int& y = x; – y = 50; – cout<<x; – cout<<y; xy 50

6 Reference variable – functions Functions can take reference parameters x 10 y y = y + 10 y = y = 20 20

7 const Objects as Function Arguments class Distance { //Distance class private: int feet; float inches; public: Distance() : feet(0), inches(0.0) { } Distance(int ft, float in) : feet(ft), inches(in) { } void getdist(){ cout > feet; cout > inches; } void showdist(){ cout << feet << “:” << inches<<endl ; } Distance add_dist(const Distance&) const; }; Distance Distance::add_dist(const Distance& d2) const { Distance temp; inches = 0; temp.inches = inches + d2.inches; if(temp.inches >= 12.0) { temp.inches -= 12.0; temp.feet = 1; } temp.feet += feet + d2.feet; return temp; }

8 *, -> and dot operator with structure *p s

9 Implicitly and Explicitly Use this Pointer Go to program

10 Cascading function calls using this pointer In cascaded member-function calls invoked multiple functions in the same statement (Ref. of object t)

11 Example – Time class

12 Cont. Why does the technique of returning *this as a reference work? The dot operator (.) associates from left to right first it evaluates t.setHour( 18 ) then returns a reference to object t The remaining expression is then interpreted as – t.setMinute( 30 ).setSecond( 22 ); t.setMinute( 30 ) call executes and returns a reference to the object t. – t.setSecond( 22 ); Go to program

13 Dynamic Memory Management C++ enables programmers to control the allocation and de-allocation of memory in a program for any built-in or user-defined type Performed with operators new and delete E.g. in Employee class – name char array – It has some size e.g. 25 Through dynamic memory we can allocate array space same as the number of character in name

14 Cont. Dynamically allocating memory in this fashion causes an array to be created in the free store (heap) Heap is a region of memory assigned to each program for storing objects created at execution time Once the memory is allocated in the free store, pointer points to the first byte of that allocated memory After used, memory can be return to heap by using delete operator

15 Dynamic memory – basic types C++ allows you to provide an initializer for a newly created fundamental-type variable delete ptr; *ptr

16 Dynamic memory – array new operator can be used to allocate arrays dynamically *gradeArray

17 Simple example Example 1: – char pointer – Allocate dynamic memory using new operator – Write string and display it – Delete the dynamic memory using delete operator Example 2: – struct definition – Allocate dynamic memory – Write string and display structure contents – Delete the dynamic memory Go to program

18 Dynamic memory – objects Consider the following declaration and statement: hours minutes seconds *timePtr Go to program

19 Composition – Objects as Members of Classes An AlarmClock object needs to know when it is supposed to sound its alarm So why not include a Time object as a member of the AlarmClock class It is refer as has-a relationship We will see how an object's constructor can pass arguments to member-object constructors

20 Example program Date.h Date class Employee.h Employee class Date birthday Date hiredate Source_emp.cpp Employee manager Go to program firstname lastname manager birthday day month year hireday day month year