SlideShare a Scribd company logo
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;
x y
50
6
Reference variable – functions
• Functions can take reference parameters
x
10
y
y = y + 10
y = 10 + 10
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 << “nEnter feet: “; cin >> feet;
cout << “Enter inches: “; cin >> 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
7550
10 3.75
10 3.75
7550
10 3.75
10 3.75
10 3.75
10 3.75
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)
(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
3.14159
704 704
16
Dynamic memory – array
• new operator can be used to allocate arrays
dynamically
*gradeArray 704
704 708 712 716 720
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:
0
0
0
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

More Related Content

PPT
Lecture2.ppt
PDF
Memory Management for C and C++ _ language
PPTX
OOP.pptx
PPTX
Chapter 1 (2) array and structure r.pptx
PPTX
Object orinted programming lecture| Overlaoded Functions
PPT
C chap16
PPT
Classes and data abstraction
PPT
Pointer
Lecture2.ppt
Memory Management for C and C++ _ language
OOP.pptx
Chapter 1 (2) array and structure r.pptx
Object orinted programming lecture| Overlaoded Functions
C chap16
Classes and data abstraction
Pointer

Similar to Object orinted programming lecture| Variables (20)

PDF
Unit3_OOP-converted.pdf
PDF
CS225_Prelecture_Notes 2nd
PPT
Classes and objects constructor and destructor
PDF
C++ Interview Questions and Answers PDF By ScholarHat
PPTX
Object Oriented Design and Programming Unit-02
PPT
02 functions, variables, basic input and output of c++
PPTX
C++11: Feel the New Language
PPTX
OOP WITH C++-ppt Slide show unitwise NOTES
PDF
Object Oriented Programming using C++ - Part 4
PDF
(4) cpp abstractions references_copies_and_const-ness
PPTX
BBACA-SEM-III-Datastructure-PPT(0) for third semestetr
PPTX
Chapter 4
PPTX
pointers in c programming - example programs
PPTX
Extending Flux - Writing Your Own Functions by Adam Anthony
PPTX
Introduction of function in c programming.pptx
PDF
Lec16-CS110 Computational Engineering
PPTX
constructocvbcvbcvbcvbr-Destructor (1).pptx
PPT
Database structure Structures Link list and trees and Recurison complete
PPT
Memory Management In C++
Unit3_OOP-converted.pdf
CS225_Prelecture_Notes 2nd
Classes and objects constructor and destructor
C++ Interview Questions and Answers PDF By ScholarHat
Object Oriented Design and Programming Unit-02
02 functions, variables, basic input and output of c++
C++11: Feel the New Language
OOP WITH C++-ppt Slide show unitwise NOTES
Object Oriented Programming using C++ - Part 4
(4) cpp abstractions references_copies_and_const-ness
BBACA-SEM-III-Datastructure-PPT(0) for third semestetr
Chapter 4
pointers in c programming - example programs
Extending Flux - Writing Your Own Functions by Adam Anthony
Introduction of function in c programming.pptx
Lec16-CS110 Computational Engineering
constructocvbcvbcvbcvbr-Destructor (1).pptx
Database structure Structures Link list and trees and Recurison complete
Memory Management In C++
Ad

Recently uploaded (20)

PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
How to Manage Starshipit in Odoo 18 - Odoo Slides
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Onica Farming 24rsclub profitable farm business
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PDF
Open folder Downloads.pdf yes yes ges yes
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
English Language Teaching from Post-.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Introduction and Scope of Bichemistry.pptx
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Pre independence Education in Inndia.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Insiders guide to clinical Medicine.pdf
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
How to Manage Starshipit in Odoo 18 - Odoo Slides
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Onica Farming 24rsclub profitable farm business
Cardiovascular Pharmacology for pharmacy students.pptx
Open folder Downloads.pdf yes yes ges yes
UPPER GASTRO INTESTINAL DISORDER.docx
Week 4 Term 3 Study Techniques revisited.pptx
English Language Teaching from Post-.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Introduction and Scope of Bichemistry.pptx
The Final Stretch: How to Release a Game and Not Die in the Process.
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Pre independence Education in Inndia.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Insiders guide to clinical Medicine.pdf
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
Ad

Object orinted programming lecture| Variables

  • 1. 1 CSC241: Object Oriented Programming Lecture No 05
  • 2. 2 Previous Lecture • Overloaded function – Constructor • const (constant) – object – member function – data member – object as function argument • friend function • this pointer
  • 3. 3 Today’s Lecture • Reference variable • this pointer – Cascading function calls • Dynamic memory allocation • static class member – static functions – static data member • sdaf
  • 4. 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. 5 Cont. – int x; – int& y = x; – y = 50; – cout<<x; – cout<<y; x y 50
  • 6. 6 Reference variable – functions • Functions can take reference parameters x 10 y y = y + 10 y = 10 + 10 y = 20 20
  • 7. 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 << “nEnter feet: “; cin >> feet; cout << “Enter inches: “; cin >> 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. 8 * , -> and dot operator with structure *p s 7550 10 3.75 10 3.75 7550 10 3.75 10 3.75 10 3.75 10 3.75
  • 9. 9 Implicitly and Explicitly Use this Pointer Go to program
  • 10. 10 Cascading function calls using this pointer • In cascaded member-function calls invoked multiple functions in the same statement (Ref. of object t) (Ref. of object t)
  • 12. 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. 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. 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. 15 Dynamic memory – basic types • C++ allows you to provide an initializer for a newly created fundamental-type variable delete ptr; *ptr 3.14159 704 704
  • 16. 16 Dynamic memory – array • new operator can be used to allocate arrays dynamically *gradeArray 704 704 708 712 716 720
  • 17. 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. 18 Dynamic memory – objects • Consider the following declaration and statement: 0 0 0 hours minutes seconds *timePtr Go to program
  • 19. 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. 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