SlideShare a Scribd company logo
CONSTRUCTORS
&
DESTRUCTORS
#109 Rimsha Nasrallah
#118 shamsa Nawaz
#139 Sana Khalid
Constructors
• C++ requires a construct call for each object it has created
• This ensure that object is initialized properly before it is used
• If there is no constructor, the compiler provides a default constructor that is, a
constructor with no parameters
• Name of constructor function is same as name of class
A counter example
• Data member
• Count
• Member function
• Constructor
• void inc_count()
• int get_count()
Automatic initialization
• An object of type Counter is first created, we want its count to be
initialized to 0
• Options are
• set_count() function (call it with an argument of 0)
• zero_count() function, to set count to 0.
• Such functions would need to be executed every time we created a
Counter object
Cont.
• A programmer may forget to initialize the object after creating it
• It’s more reliable and convenient to cause each object to initialize implicitly when
it is created
• In the Counter class, the constructor Counter() is called automatically whenever a
new object of type Counter is created
• Counter c1, c2;
creates two objects. Constructor is called with each object separately
Constructor Name
• First, constructor name must be same as the name of class
• This is one way the compiler knows they are constructors
• Second, no return type is used for constructors
• Why not? Since the constructor is called automatically by the system, there’s no
program for it to return anything to; a return value wouldn’t make sense
• This is the second way the compiler knows they are constructors
Initializer List
• One of the most common tasks a constructor carries out is initializing data members
• In the Counter class the constructor must initialize the count member to 0
• The initialization takes place following the member function declarator but before the function body.
• Initialization in constructor’s function body
Counter()
{ count = 0; }
this is not the preferred approach
Cont.
• It’s preceded by a colon.The value is placed in parentheses following the member
data.
Counter() : count(0)
{ }
• If multiple members must be initialized, they’re separated by commas.
• someClass() : m1(7), m2(33), m3(4) ←initializer list { }
Destructors
• Destructor is a function called implicitly when an object is destroyed
• The name of the destructor for a class is the tilde character (~) followed by the
class name
• No arguments and no return type for a destructor
• The most common use of destructors is to deallocate memory that was allocated
for the object by the constructor
Validating Data with set Functions
• A set function should validate the value before assigning to private data member
• Set function can return a value or may display a message if invalid data is assign to
object
Constructor with default arguments
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 ;
}
void add_dist( Distance, Distance );
};
void Distance::add_dist(Distance d2,
Distance d3) {
inches = d2.inches + d3.inches;
feet = 0;
if(inches >= 12.0) {
inches -= 12.0;
feet++;
}
feet += d2.feet + d3.feet;
}
main()
{
Distance dist1, dist3;
Distance dist2(11, 6.5);
dist1.getdist();
dist3.add_dist(dist1, dist2);
cout << “ndist1 = “; dist1.showdist();
cout << “ndist2 = “; dist2.showdist();
cout << “ndist3 = “; dist3.showdist();
}
feet
inches
dist1
0
0.0
`feet
inches
dist3
0
0.0
feet
inches
dist2
11
6.5
5
7.0
feet
inches
d1
feet
inches
d2
5
7.0
11
6.5
13.5
0
1.5
117
main()
{
Distance dist1, dist3;
Distance dist2(11, 6.5);
dist1.getdist();
dist3.add_dist(dist1, dist2);
cout << “ndist1 = “; dist1.showdist();
cout << “ndist2 = “; dist2.showdist();
cout << “ndist3 = “; dist3.showdist();
}
void getdist(){
cout << “nEnter feet: “; cin >> feet;
cout << “Enter inches: “; cin >> inches;
}
void Distance::add_dist(Distance d1, Distance d2) {
inches = d1.inches + d2.inches;
feet = 0;
if(inches >= 12.0) {
inches -= 12.0;
feet++;
}
feet += d1.feet + d2.feet;
}
void showdist(){
cout << feet << “:” << inches<<endl ;
}
dist1 = 5 : 7.0
dist2 = 11 : 6.5
dist3 = 17 : 1.5
Overloaded Constructors
• It’s convenient to be able to give variables of type Distance a
value when they are first created
Distance dist2(11, 6.25);
• which defines an object, and initializes it to a value of 11 for feet
and 6.25 for inches.
• Distance dist1, dist2; then No-argument constructor is
called/invoked (the default constructor)
• Since there are now two constructors with the same name,
Distance(), we say the constructor is overloaded
Member Functions Defined Outside the
Class
• Such functions, needs to have a prototype/declaration within the
class
• The function name, add_dist(), is preceded by the class name,
Distance, and a new symbol—the double colon (::).This symbol is
called the scope resolution operator.
• It is a way of specifying what class something is associated with
• In this situation, Distance::add_dist() means “the add_dist() member
function of the Distance class”
void Distance::add_dist(Distance d2, Distance d3)
Questions??

More Related Content

What's hot

Rxjs ngvikings
Rxjs ngvikingsRxjs ngvikings
Rxjs ngvikings
Christoffer Noring
 
Map kit light
Map kit lightMap kit light
Map kit light
CocoaHeads France
 
Monads in Swift
Monads in SwiftMonads in Swift
Monads in Swift
Vincent Pradeilles
 
Java script
Java scriptJava script
Java script
Dhananjay Kumar
 
Cocoa heads 09112017
Cocoa heads 09112017Cocoa heads 09112017
Cocoa heads 09112017
Vincent Pradeilles
 
LinkedIn TBC JavaScript 100: Functions
 LinkedIn TBC JavaScript 100: Functions LinkedIn TBC JavaScript 100: Functions
LinkedIn TBC JavaScript 100: Functions
Adam Crabtree
 
Expert JavaScript tricks of the masters
Expert JavaScript  tricks of the mastersExpert JavaScript  tricks of the masters
Expert JavaScript tricks of the masters
Ara Pehlivanian
 
Rxjs swetugg
Rxjs swetuggRxjs swetugg
Rxjs swetugg
Christoffer Noring
 
React hooks beyond hype
React hooks beyond hypeReact hooks beyond hype
React hooks beyond hype
Magdiel Duarte
 
Bindings: the zen of montage
Bindings: the zen of montageBindings: the zen of montage
Bindings: the zen of montage
Kris Kowal
 
Introduction to RxJS
Introduction to RxJSIntroduction to RxJS
Introduction to RxJS
Brainhub
 
React new features and intro to Hooks
React new features and intro to HooksReact new features and intro to Hooks
React new features and intro to Hooks
Soluto
 
iOS Beginners Lesson 4
iOS Beginners Lesson 4iOS Beginners Lesson 4
iOS Beginners Lesson 4
Calvin Cheng
 
Rxjs vienna
Rxjs viennaRxjs vienna
Rxjs vienna
Christoffer Noring
 
Deep Dive into React Hooks
Deep Dive into React HooksDeep Dive into React Hooks
Deep Dive into React Hooks
Felix Kühl
 
SQL Functions
SQL FunctionsSQL Functions
SQL Functions
ammarbrohi
 
Douglas Crockford: Serversideness
Douglas Crockford: ServersidenessDouglas Crockford: Serversideness
Douglas Crockford: Serversideness
WebExpo
 
Advanced functional programing in Swift
Advanced functional programing in SwiftAdvanced functional programing in Swift
Advanced functional programing in Swift
Vincent Pradeilles
 
Functional Core, Reactive Shell
Functional Core, Reactive ShellFunctional Core, Reactive Shell
Functional Core, Reactive Shell
Giovanni Lodi
 
ReactiveCocoa in Practice
ReactiveCocoa in PracticeReactiveCocoa in Practice
ReactiveCocoa in Practice
Outware Mobile
 

What's hot (20)

Rxjs ngvikings
Rxjs ngvikingsRxjs ngvikings
Rxjs ngvikings
 
Map kit light
Map kit lightMap kit light
Map kit light
 
Monads in Swift
Monads in SwiftMonads in Swift
Monads in Swift
 
Java script
Java scriptJava script
Java script
 
Cocoa heads 09112017
Cocoa heads 09112017Cocoa heads 09112017
Cocoa heads 09112017
 
LinkedIn TBC JavaScript 100: Functions
 LinkedIn TBC JavaScript 100: Functions LinkedIn TBC JavaScript 100: Functions
LinkedIn TBC JavaScript 100: Functions
 
Expert JavaScript tricks of the masters
Expert JavaScript  tricks of the mastersExpert JavaScript  tricks of the masters
Expert JavaScript tricks of the masters
 
Rxjs swetugg
Rxjs swetuggRxjs swetugg
Rxjs swetugg
 
React hooks beyond hype
React hooks beyond hypeReact hooks beyond hype
React hooks beyond hype
 
Bindings: the zen of montage
Bindings: the zen of montageBindings: the zen of montage
Bindings: the zen of montage
 
Introduction to RxJS
Introduction to RxJSIntroduction to RxJS
Introduction to RxJS
 
React new features and intro to Hooks
React new features and intro to HooksReact new features and intro to Hooks
React new features and intro to Hooks
 
iOS Beginners Lesson 4
iOS Beginners Lesson 4iOS Beginners Lesson 4
iOS Beginners Lesson 4
 
Rxjs vienna
Rxjs viennaRxjs vienna
Rxjs vienna
 
Deep Dive into React Hooks
Deep Dive into React HooksDeep Dive into React Hooks
Deep Dive into React Hooks
 
SQL Functions
SQL FunctionsSQL Functions
SQL Functions
 
Douglas Crockford: Serversideness
Douglas Crockford: ServersidenessDouglas Crockford: Serversideness
Douglas Crockford: Serversideness
 
Advanced functional programing in Swift
Advanced functional programing in SwiftAdvanced functional programing in Swift
Advanced functional programing in Swift
 
Functional Core, Reactive Shell
Functional Core, Reactive ShellFunctional Core, Reactive Shell
Functional Core, Reactive Shell
 
ReactiveCocoa in Practice
ReactiveCocoa in PracticeReactiveCocoa in Practice
ReactiveCocoa in Practice
 

Similar to Oop presentation

Friend this-new&delete
Friend this-new&deleteFriend this-new&delete
Friend this-new&delete
Shehzad Rizwan
 
OOP.pptx
OOP.pptxOOP.pptx
OOP.pptx
saifnasir3
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
C++ process new
C++ process newC++ process new
C++ process new
敬倫 林
 
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptxconstructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
AshrithaRokkam
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)
Asfand Hassan
 
C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Language
mspline
 
C++ Secure Programming
C++ Secure ProgrammingC++ Secure Programming
C++ Secure Programming
Marian Marinov
 
Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
Lovely Professional University
 
Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2
Lovely Professional University
 
C++ Functions.pptx
C++ Functions.pptxC++ Functions.pptx
C++ Functions.pptx
DikshaDani5
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
rehan16091997
 
lecture56.ppt
lecture56.pptlecture56.ppt
lecture56.ppt
AqeelAbbas94
 
Day 1
Day 1Day 1
data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1
Teksify
 
Modern C++ Lunch and Learn
Modern C++ Lunch and LearnModern C++ Lunch and Learn
Modern C++ Lunch and Learn
Paul Irwin
 
Linq
LinqLinq
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
TarekHemdan3
 
C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8
Christian Nagel
 
The Challenge of Bringing FEZ to PlayStation Platforms
The Challenge of Bringing FEZ to PlayStation PlatformsThe Challenge of Bringing FEZ to PlayStation Platforms
The Challenge of Bringing FEZ to PlayStation Platforms
Miguel Angel Horna
 

Similar to Oop presentation (20)

Friend this-new&delete
Friend this-new&deleteFriend this-new&delete
Friend this-new&delete
 
OOP.pptx
OOP.pptxOOP.pptx
OOP.pptx
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
C++ process new
C++ process newC++ process new
C++ process new
 
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptxconstructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)
 
C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Language
 
C++ Secure Programming
C++ Secure ProgrammingC++ Secure Programming
C++ Secure Programming
 
Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
 
Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2
 
C++ Functions.pptx
C++ Functions.pptxC++ Functions.pptx
C++ Functions.pptx
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
 
lecture56.ppt
lecture56.pptlecture56.ppt
lecture56.ppt
 
Day 1
Day 1Day 1
Day 1
 
data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1
 
Modern C++ Lunch and Learn
Modern C++ Lunch and LearnModern C++ Lunch and Learn
Modern C++ Lunch and Learn
 
Linq
LinqLinq
Linq
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8
 
The Challenge of Bringing FEZ to PlayStation Platforms
The Challenge of Bringing FEZ to PlayStation PlatformsThe Challenge of Bringing FEZ to PlayStation Platforms
The Challenge of Bringing FEZ to PlayStation Platforms
 

Recently uploaded

ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
ssuser13ffe4
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
MJDuyan
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Leena Ghag-Sakpal
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
Chevonnese Chevers Whyte, MBA, B.Sc.
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 

Recently uploaded (20)

ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 

Oop presentation

  • 2. Constructors • C++ requires a construct call for each object it has created • This ensure that object is initialized properly before it is used • If there is no constructor, the compiler provides a default constructor that is, a constructor with no parameters • Name of constructor function is same as name of class
  • 3. A counter example • Data member • Count • Member function • Constructor • void inc_count() • int get_count()
  • 4. Automatic initialization • An object of type Counter is first created, we want its count to be initialized to 0 • Options are • set_count() function (call it with an argument of 0) • zero_count() function, to set count to 0. • Such functions would need to be executed every time we created a Counter object
  • 5. Cont. • A programmer may forget to initialize the object after creating it • It’s more reliable and convenient to cause each object to initialize implicitly when it is created • In the Counter class, the constructor Counter() is called automatically whenever a new object of type Counter is created • Counter c1, c2; creates two objects. Constructor is called with each object separately
  • 6. Constructor Name • First, constructor name must be same as the name of class • This is one way the compiler knows they are constructors • Second, no return type is used for constructors • Why not? Since the constructor is called automatically by the system, there’s no program for it to return anything to; a return value wouldn’t make sense • This is the second way the compiler knows they are constructors
  • 7. Initializer List • One of the most common tasks a constructor carries out is initializing data members • In the Counter class the constructor must initialize the count member to 0 • The initialization takes place following the member function declarator but before the function body. • Initialization in constructor’s function body Counter() { count = 0; } this is not the preferred approach
  • 8. Cont. • It’s preceded by a colon.The value is placed in parentheses following the member data. Counter() : count(0) { } • If multiple members must be initialized, they’re separated by commas. • someClass() : m1(7), m2(33), m3(4) ←initializer list { }
  • 9. Destructors • Destructor is a function called implicitly when an object is destroyed • The name of the destructor for a class is the tilde character (~) followed by the class name • No arguments and no return type for a destructor • The most common use of destructors is to deallocate memory that was allocated for the object by the constructor
  • 10. Validating Data with set Functions • A set function should validate the value before assigning to private data member • Set function can return a value or may display a message if invalid data is assign to object
  • 12. 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 ; } void add_dist( Distance, Distance ); }; void Distance::add_dist(Distance d2, Distance d3) { inches = d2.inches + d3.inches; feet = 0; if(inches >= 12.0) { inches -= 12.0; feet++; } feet += d2.feet + d3.feet; } main() { Distance dist1, dist3; Distance dist2(11, 6.5); dist1.getdist(); dist3.add_dist(dist1, dist2); cout << “ndist1 = “; dist1.showdist(); cout << “ndist2 = “; dist2.showdist(); cout << “ndist3 = “; dist3.showdist(); }
  • 13. feet inches dist1 0 0.0 `feet inches dist3 0 0.0 feet inches dist2 11 6.5 5 7.0 feet inches d1 feet inches d2 5 7.0 11 6.5 13.5 0 1.5 117 main() { Distance dist1, dist3; Distance dist2(11, 6.5); dist1.getdist(); dist3.add_dist(dist1, dist2); cout << “ndist1 = “; dist1.showdist(); cout << “ndist2 = “; dist2.showdist(); cout << “ndist3 = “; dist3.showdist(); } void getdist(){ cout << “nEnter feet: “; cin >> feet; cout << “Enter inches: “; cin >> inches; } void Distance::add_dist(Distance d1, Distance d2) { inches = d1.inches + d2.inches; feet = 0; if(inches >= 12.0) { inches -= 12.0; feet++; } feet += d1.feet + d2.feet; } void showdist(){ cout << feet << “:” << inches<<endl ; } dist1 = 5 : 7.0 dist2 = 11 : 6.5 dist3 = 17 : 1.5
  • 14. Overloaded Constructors • It’s convenient to be able to give variables of type Distance a value when they are first created Distance dist2(11, 6.25); • which defines an object, and initializes it to a value of 11 for feet and 6.25 for inches. • Distance dist1, dist2; then No-argument constructor is called/invoked (the default constructor) • Since there are now two constructors with the same name, Distance(), we say the constructor is overloaded
  • 15. Member Functions Defined Outside the Class • Such functions, needs to have a prototype/declaration within the class • The function name, add_dist(), is preceded by the class name, Distance, and a new symbol—the double colon (::).This symbol is called the scope resolution operator. • It is a way of specifying what class something is associated with • In this situation, Distance::add_dist() means “the add_dist() member function of the Distance class” void Distance::add_dist(Distance d2, Distance d3)