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

Similar to Object orinted programming lecture| Variables (20)

PPT
Pointers_in_c.pptfffgfggdggffffrreeeggttr
MorfaSafi
 
DOCX
Arrry structure Stacks in data structure
lodhran-hayat
 
PPT
Pointers
sanya6900
 
PDF
CS225_Prelecture_Notes 2nd
Edward Chen
 
PPT
POINTERS IN C++ CBSE +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
PPTX
CSC2161Programming_in_Cpp_Lecture4-OOP Classes and Objects[1].pptx
winebaldbanituze
 
PDF
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
sindhus795217
 
PPT
C++ Programming Course
Dennis Chang
 
PPT
Ccourse 140618093931-phpapp02
Getachew Ganfur
 
PPTX
pointers.pptx
MuhammadAbubakar680442
 
PPT
Lecture2.ppt
Sabaunnisa3
 
PPTX
Data structure and Algorithms (C++).pptx
ammarasalmanqureshi7
 
PPTX
OOC MODULE1.pptx
1HK19CS090MOHAMMEDSA
 
PPTX
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Abu Saleh
 
PPT
Data structure and problem solving ch01.ppt
Ping261512
 
PPT
106da session5 c++
Mukund Trivedi
 
TXT
Advance C++notes
Rajiv Gupta
 
PPTX
C++ - UNIT_-_IV.pptx which contains details about Pointers
ANUSUYA S
 
PPTX
C++ overview
Prem Ranjan
 
Pointers_in_c.pptfffgfggdggffffrreeeggttr
MorfaSafi
 
Arrry structure Stacks in data structure
lodhran-hayat
 
Pointers
sanya6900
 
CS225_Prelecture_Notes 2nd
Edward Chen
 
POINTERS IN C++ CBSE +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
CSC2161Programming_in_Cpp_Lecture4-OOP Classes and Objects[1].pptx
winebaldbanituze
 
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
sindhus795217
 
C++ Programming Course
Dennis Chang
 
Ccourse 140618093931-phpapp02
Getachew Ganfur
 
pointers.pptx
MuhammadAbubakar680442
 
Lecture2.ppt
Sabaunnisa3
 
Data structure and Algorithms (C++).pptx
ammarasalmanqureshi7
 
OOC MODULE1.pptx
1HK19CS090MOHAMMEDSA
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Abu Saleh
 
Data structure and problem solving ch01.ppt
Ping261512
 
106da session5 c++
Mukund Trivedi
 
Advance C++notes
Rajiv Gupta
 
C++ - UNIT_-_IV.pptx which contains details about Pointers
ANUSUYA S
 
C++ overview
Prem Ranjan
 

Recently uploaded (20)

PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
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