SlideShare a Scribd company logo
U T T A M G A N D H I
S Y N E R Z I P
H T T P : / / W W W . L I N K E D I N . C O M / P U B / U T T A M -
G A N D H I / 2 1 / 7 A A / 2 4
Effective C++
——
Acknowledgments
—  This presentation is based on Scott Meyers well
known book ‘Effective C++’
—  http://www.amazon.com/Effective-Specific-
Improve-Programs-Designs/dp/0321334876
Variable Declaration
—  Delay as far as possible
¡  can avoid construction and destruction,
¡  keeps the context in which it is required
¡  for loop construction, consider creating in loop based on cost
of assignment v/s cost of construct destructor
Passing of Parameters
—  Pass by value
¡  Primitive types
¡  STL function objects, iterators
—  Pass by reference
¡  User defined objects
÷  Efficient than pass by value
÷  No slicing
—  Return by reference
¡  Local data should not be returned by reference
const 1/2
—  Replacing #define with const
¡  Reduced object code
¡  Better error message
¡  Control over scope
—  Enum
¡  Can be used instead of const
¡  Cannot take address of enum
—  const_iterator
const 2/2
—  const return type
—  const input parameter
—  const member function of a class
—  const overloading of member functions
—  Use of mutable
casts
—  static_cast
—  dynamic_cast
—  const_cast
—  reinterpret_cast
Member initialization list
—  Efficiency
¡  Default constructor plus assignment are replaced by copy
constructor
—  Order of declaration in class
¡  Order of initialization list
—  Const and reference member
¡  must be initialized in member initializer list
inlining
—  inlining does in-place replacement of code
—  Improves efficiency
—  Can lead to increased or decreased code size
—  Inlining is a request to compiler
—  Certain functions may not be inlined
¡  Called through function pointers
¡  Containing loops
¡  Virtual functions
¡  Constructor and destructor are worst candidates for inlining
Compilation
—  forward declare where possible
—  Keep declaration and definition in separate files
¡  Not possible for templates
Non-local static object initialization
—  The order of initialization in different translation
units is undefined
¡  Solution-> use function which return static object by reference
Default functions 1/2
—  Default functions
¡  default constructor,
¡  Assignment
¡  copy constructor
¡  destructor
¡  & operator
—  If parameterized constructor is user defined, default
constructor will not be provided
—  default copy constructor does bitwise copy
—  copy constructor and assignment operator not
generated if, there is a ref member or const member
Default functions 2/2
—  To avoid default copy constructor
¡  declare them private and don't define them
¡  Derive from base class with private copy constructor
¡  Use boost::noncopyable
constructor destructor
—  Virtual destructor
¡  Needed for base classes
¡  Compiler will invoke the base destructor only without it
—  Pure virtual destructor
¡  To create abstract classes
¡  Having body in pure virtual destructor will help compiler while
invoking the base class destructor
—  Throwing exception
¡  Destructor should never throw exception
Inheritance 1/4
—  Design
¡  public inheritance
÷  Should always represent is-a relationship
¢  Penguin is not a bird
¢  Square is not a rectangle
Inheritance 2/4
—  Design
¡  Inheritance of interface v/s inheritance of implementation.
¡  public inheritance = inheritance of interfaces
¡  Pure virtual functions = inheritance of interface
¡  impure virtual functions = inheritance of interface plus
inheritance of a default implementation.
¡  Non-virtual functions = inheritance of interface plus
inheritance of a mandatory implementation.
Inheritance 3/4
—  Private inheritance (is implemented in terms of )
¡  When only implementation is required
¡  e.g. class adapter pattern
—  Names in derived classes hide names in base classes
—  To make them visible
¡  Using declaration
¡  Forwarding function
—  don't redefine non-virtual inherited function
Inheritance 4/4
—  Multiple inheritance complexities
¡  lead to ambiguity issues
¡  May need virtual inheritance.
¡  Virtual inheritance costs
÷  size, speed, and complexity of initialization and assignment.
¡  It's practical when base classes have no data.
¡  Some practical uses
÷  public inheritance for an Interface and private inheritance for
implementation
Composition
—  has-a relationship
¡  e.g. car has wheels
¡  Aggregation v/s composition
÷  In C++ a class having pointer members is composition whereas
class having concrete members is aggregation
÷  Aggregation has full control of aggregated object
÷  Composition can share the contained object with other classes
Virtual Function
—  Don’t call virtual function from constructor or
destructor
¡  Derived part is not yet constructed when in base class
constructor
—  Never redefine an inherited default parameter value
¡  default parameter values are statically bound,
¡  virtual functions are dynamically bound.
Operator overloading
—  Assignment operator should return reference to *this
¡  To support a = b = c ( chaining )
—  Check for self-copy in assignment operator
¡  If copying is deleting the existing object first, the object may
delete itself
Copy constructor
—  copy constructor should call base copy constructor
RAII
—  Use constructor and destructor for resource
management
—  Use std::shared_ptr or std::scoped_ptr instead of
raw pointers
—  Use std::vector instead of arrays
—  std::shared_ptr, with function object as argument
for action other than delete
Misc
—  new[] delete[] should be matched
—  Namespace usage
¡  package extensibility
¡  better encapsulation
¡  precise control over what to include
Designing Interfaces
—  Simple to use correctly and hard to misuse
¡  Consistency with built-in types
÷  a = b = c
¡  Restriction on types
¡  Constraining object values
÷  Date, Calendar
¡  Eliminating client side resource management

More Related Content

What's hot

Thinking in Functions
Thinking in FunctionsThinking in Functions
Thinking in Functions
Alexandru Bolboaca
 
Java.util.concurrent.concurrent hashmap
Java.util.concurrent.concurrent hashmapJava.util.concurrent.concurrent hashmap
Java.util.concurrent.concurrent hashmap
Srinivasan Raghvan
 
Client sidescripting javascript
Client sidescripting javascriptClient sidescripting javascript
Client sidescripting javascript
Selvin Josy Bai Somu
 
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
Swift Tutorial Part 1. The Complete Guide For Swift Programming LanguageSwift Tutorial Part 1. The Complete Guide For Swift Programming Language
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
Hossam Ghareeb
 
FregeDay: Design and Implementation of the language (Ingo Wechsung)
FregeDay: Design and Implementation of the language (Ingo Wechsung)FregeDay: Design and Implementation of the language (Ingo Wechsung)
FregeDay: Design and Implementation of the language (Ingo Wechsung)
Dierk König
 
Functions and tasks in verilog
Functions and tasks in verilogFunctions and tasks in verilog
Functions and tasks in verilog
Nallapati Anindra
 
Valgrind
ValgrindValgrind
Valgrind
Melvin Cabatuan
 
Effective c# part1
Effective c# part1Effective c# part1
Effective c# part1
Yuriy Seniuk
 
Object to string transformer
Object to string transformerObject to string transformer
Object to string transformer
Rahul Kumar
 
Functional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNextFunctional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNext
Unfold UI
 
C++ vs C#
C++ vs C#C++ vs C#
C++ vs C#
sudipv
 
Java 8 by example!
Java 8 by example!Java 8 by example!
Java 8 by example!
Mark Harrison
 
Kotlin For Android - Properties (part 4 of 7)
Kotlin For Android - Properties (part 4 of 7)Kotlin For Android - Properties (part 4 of 7)
Kotlin For Android - Properties (part 4 of 7)
Gesh Markov
 
Functional Programming in Ruby
Functional Programming in RubyFunctional Programming in Ruby
Functional Programming in RubyAlex Teut
 
Notes: Verilog Part 5 - Tasks and Functions
Notes: Verilog Part 5 - Tasks and FunctionsNotes: Verilog Part 5 - Tasks and Functions
Notes: Verilog Part 5 - Tasks and Functions
Jay Baxi
 
Why functional programming in C# & F#
Why functional programming in C# & F#Why functional programming in C# & F#
Why functional programming in C# & F#
Riccardo Terrell
 
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
Dierk König
 
Swift, a quick overview
Swift, a quick overviewSwift, a quick overview
Swift, a quick overview
Julian Król
 
Hanoi JUG: Java 8 & lambdas
Hanoi JUG: Java 8 & lambdasHanoi JUG: Java 8 & lambdas
Hanoi JUG: Java 8 & lambdas
Benoît de CHATEAUVIEUX
 

What's hot (20)

Thinking in Functions
Thinking in FunctionsThinking in Functions
Thinking in Functions
 
Java.util.concurrent.concurrent hashmap
Java.util.concurrent.concurrent hashmapJava.util.concurrent.concurrent hashmap
Java.util.concurrent.concurrent hashmap
 
Client sidescripting javascript
Client sidescripting javascriptClient sidescripting javascript
Client sidescripting javascript
 
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
Swift Tutorial Part 1. The Complete Guide For Swift Programming LanguageSwift Tutorial Part 1. The Complete Guide For Swift Programming Language
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
 
FregeDay: Design and Implementation of the language (Ingo Wechsung)
FregeDay: Design and Implementation of the language (Ingo Wechsung)FregeDay: Design and Implementation of the language (Ingo Wechsung)
FregeDay: Design and Implementation of the language (Ingo Wechsung)
 
Functions and tasks in verilog
Functions and tasks in verilogFunctions and tasks in verilog
Functions and tasks in verilog
 
Valgrind
ValgrindValgrind
Valgrind
 
Effective c# part1
Effective c# part1Effective c# part1
Effective c# part1
 
Object to string transformer
Object to string transformerObject to string transformer
Object to string transformer
 
Functional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNextFunctional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNext
 
C++ vs C#
C++ vs C#C++ vs C#
C++ vs C#
 
Managed Compiler
Managed CompilerManaged Compiler
Managed Compiler
 
Java 8 by example!
Java 8 by example!Java 8 by example!
Java 8 by example!
 
Kotlin For Android - Properties (part 4 of 7)
Kotlin For Android - Properties (part 4 of 7)Kotlin For Android - Properties (part 4 of 7)
Kotlin For Android - Properties (part 4 of 7)
 
Functional Programming in Ruby
Functional Programming in RubyFunctional Programming in Ruby
Functional Programming in Ruby
 
Notes: Verilog Part 5 - Tasks and Functions
Notes: Verilog Part 5 - Tasks and FunctionsNotes: Verilog Part 5 - Tasks and Functions
Notes: Verilog Part 5 - Tasks and Functions
 
Why functional programming in C# & F#
Why functional programming in C# & F#Why functional programming in C# & F#
Why functional programming in C# & F#
 
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
 
Swift, a quick overview
Swift, a quick overviewSwift, a quick overview
Swift, a quick overview
 
Hanoi JUG: Java 8 & lambdas
Hanoi JUG: Java 8 & lambdasHanoi JUG: Java 8 & lambdas
Hanoi JUG: Java 8 & lambdas
 

Viewers also liked

Modern C++
Modern C++Modern C++
Modern C++
Richard Thomson
 
C traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmersC traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmers
Richard Thomson
 
Smart Pointers
Smart PointersSmart Pointers
Smart Pointers
Roman Okolovich
 
Статический и динамический полиморфизм в C++, Дмитрий Леванов
Статический и динамический полиморфизм в C++, Дмитрий ЛевановСтатический и динамический полиморфизм в C++, Дмитрий Леванов
Статический и динамический полиморфизм в C++, Дмитрий Леванов
Yandex
 
High Order Function Computations in c++14 (C++ Dev Meetup Iasi)
High Order Function Computations in c++14 (C++ Dev Meetup Iasi)High Order Function Computations in c++14 (C++ Dev Meetup Iasi)
High Order Function Computations in c++14 (C++ Dev Meetup Iasi)
Ovidiu Farauanu
 
Dependency Injection in C++ (Community Days 2015)
Dependency Injection in C++ (Community Days 2015)Dependency Injection in C++ (Community Days 2015)
Dependency Injection in C++ (Community Days 2015)
Daniele Pallastrelli
 
С++ without new and delete
С++ without new and deleteС++ without new and delete
С++ without new and delete
Platonov Sergey
 
Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())
Sameer Rathoud
 
Михаил Матросов, “С++ без new и delete”
Михаил Матросов, “С++ без new и delete”Михаил Матросов, “С++ без new и delete”
Михаил Матросов, “С++ без new и delete”
Platonov Sergey
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Dustin Chase
 
Multithreading 101
Multithreading 101Multithreading 101
Multithreading 101
Tim Penhey
 
C++ Dependency Management 2.0
C++ Dependency Management 2.0C++ Dependency Management 2.0
C++ Dependency Management 2.0
Patrick Charrier
 
STL Algorithms In Action
STL Algorithms In ActionSTL Algorithms In Action
STL Algorithms In Action
Northwest C++ Users' Group
 
C++ Memory Management
C++ Memory ManagementC++ Memory Management
C++ Memory Management
Anil Bapat
 
C++11 smart pointers
C++11 smart pointersC++11 smart pointers
C++11 smart pointers
chchwy Chang
 
Memory Management In C++
Memory Management In C++Memory Management In C++
Memory Management In C++
ShriKant Vashishtha
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKamal Acharya
 
Introduction to Bitcoin and ECDSA
Introduction to Bitcoin and ECDSAIntroduction to Bitcoin and ECDSA
Introduction to Bitcoin and ECDSANikesh Mistry
 
Multithreading done right
Multithreading done rightMultithreading done right
Multithreading done right
Platonov Sergey
 

Viewers also liked (20)

Modern C++
Modern C++Modern C++
Modern C++
 
C traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmersC traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmers
 
Smart Pointers
Smart PointersSmart Pointers
Smart Pointers
 
Статический и динамический полиморфизм в C++, Дмитрий Леванов
Статический и динамический полиморфизм в C++, Дмитрий ЛевановСтатический и динамический полиморфизм в C++, Дмитрий Леванов
Статический и динамический полиморфизм в C++, Дмитрий Леванов
 
High Order Function Computations in c++14 (C++ Dev Meetup Iasi)
High Order Function Computations in c++14 (C++ Dev Meetup Iasi)High Order Function Computations in c++14 (C++ Dev Meetup Iasi)
High Order Function Computations in c++14 (C++ Dev Meetup Iasi)
 
Dependency Injection in C++ (Community Days 2015)
Dependency Injection in C++ (Community Days 2015)Dependency Injection in C++ (Community Days 2015)
Dependency Injection in C++ (Community Days 2015)
 
С++ without new and delete
С++ without new and deleteС++ without new and delete
С++ without new and delete
 
Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())
 
Михаил Матросов, “С++ без new и delete”
Михаил Матросов, “С++ без new и delete”Михаил Матросов, “С++ без new и delete”
Михаил Матросов, “С++ без new и delete”
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Multithreading 101
Multithreading 101Multithreading 101
Multithreading 101
 
C++ Dependency Management 2.0
C++ Dependency Management 2.0C++ Dependency Management 2.0
C++ Dependency Management 2.0
 
STL Algorithms In Action
STL Algorithms In ActionSTL Algorithms In Action
STL Algorithms In Action
 
C++ Memory Management
C++ Memory ManagementC++ Memory Management
C++ Memory Management
 
File Pointers
File PointersFile Pointers
File Pointers
 
C++11 smart pointers
C++11 smart pointersC++11 smart pointers
C++11 smart pointers
 
Memory Management In C++
Memory Management In C++Memory Management In C++
Memory Management In C++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Introduction to Bitcoin and ECDSA
Introduction to Bitcoin and ECDSAIntroduction to Bitcoin and ECDSA
Introduction to Bitcoin and ECDSA
 
Multithreading done right
Multithreading done rightMultithreading done right
Multithreading done right
 

Similar to Effective c++notes

C++ Training
C++ TrainingC++ Training
C++ Training
SubhendraBasu5
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)rashmita_mishra
 
C++ language
C++ languageC++ language
C++ language
Elizabeth Pisarek
 
object oriented programming language.pptx
object oriented programming language.pptxobject oriented programming language.pptx
object oriented programming language.pptx
syedabbas594247
 
C questions
C questionsC questions
C questions
parm112
 
6_VirtualFunctions_AbstractClasses.pdf
6_VirtualFunctions_AbstractClasses.pdf6_VirtualFunctions_AbstractClasses.pdf
6_VirtualFunctions_AbstractClasses.pdf
RamiDiab8
 
Beware of Pointers
Beware of PointersBeware of Pointers
Beware of Pointers
ppd1961
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
nirajmandaliya
 
CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developement
frwebhelp
 
Building a p2 update site using Buckminster
Building a p2 update site using BuckminsterBuilding a p2 update site using Buckminster
Building a p2 update site using Buckminsterguest5e2b6b
 
Ej Chpt#4 Final
Ej Chpt#4 FinalEj Chpt#4 Final
Ej Chpt#4 Final
Chandan Benjaram
 
Unit3_OOP-converted.pdf
Unit3_OOP-converted.pdfUnit3_OOP-converted.pdf
Unit3_OOP-converted.pdf
PowerfullBoy1
 
2. oop with c++ get 410 day 2
2. oop with c++ get 410   day 22. oop with c++ get 410   day 2
2. oop with c++ get 410 day 2Mukul kumar Neal
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)nirajmandaliya
 
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
ppd1961
 
New c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNew c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_i
Nico Ludwig
 
Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.ppt
ishan743441
 
Functions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupFunctions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrup
SyedHaroonShah4
 

Similar to Effective c++notes (20)

C++ Training
C++ TrainingC++ Training
C++ Training
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
 
C++ language
C++ languageC++ language
C++ language
 
object oriented programming language.pptx
object oriented programming language.pptxobject oriented programming language.pptx
object oriented programming language.pptx
 
C questions
C questionsC questions
C questions
 
6_VirtualFunctions_AbstractClasses.pdf
6_VirtualFunctions_AbstractClasses.pdf6_VirtualFunctions_AbstractClasses.pdf
6_VirtualFunctions_AbstractClasses.pdf
 
C++ Interview Questions
C++ Interview QuestionsC++ Interview Questions
C++ Interview Questions
 
Beware of Pointers
Beware of PointersBeware of Pointers
Beware of Pointers
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
 
CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developement
 
Building a p2 update site using Buckminster
Building a p2 update site using BuckminsterBuilding a p2 update site using Buckminster
Building a p2 update site using Buckminster
 
Ej Chpt#4 Final
Ej Chpt#4 FinalEj Chpt#4 Final
Ej Chpt#4 Final
 
Unit3_OOP-converted.pdf
Unit3_OOP-converted.pdfUnit3_OOP-converted.pdf
Unit3_OOP-converted.pdf
 
2. oop with c++ get 410 day 2
2. oop with c++ get 410   day 22. oop with c++ get 410   day 2
2. oop with c++ get 410 day 2
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)
 
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
 
Metaprogramming
MetaprogrammingMetaprogramming
Metaprogramming
 
New c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNew c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_i
 
Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.ppt
 
Functions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupFunctions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrup
 

Recently uploaded

Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
ShamsuddeenMuhammadA
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
Launch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in MinutesLaunch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in Minutes
Roshan Dwivedi
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
QuickwayInfoSystems3
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 

Recently uploaded (20)

Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Launch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in MinutesLaunch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in Minutes
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 

Effective c++notes

  • 1. U T T A M G A N D H I S Y N E R Z I P H T T P : / / W W W . L I N K E D I N . C O M / P U B / U T T A M - G A N D H I / 2 1 / 7 A A / 2 4 Effective C++
  • 2. —— Acknowledgments —  This presentation is based on Scott Meyers well known book ‘Effective C++’ —  http://www.amazon.com/Effective-Specific- Improve-Programs-Designs/dp/0321334876
  • 3. Variable Declaration —  Delay as far as possible ¡  can avoid construction and destruction, ¡  keeps the context in which it is required ¡  for loop construction, consider creating in loop based on cost of assignment v/s cost of construct destructor
  • 4. Passing of Parameters —  Pass by value ¡  Primitive types ¡  STL function objects, iterators —  Pass by reference ¡  User defined objects ÷  Efficient than pass by value ÷  No slicing —  Return by reference ¡  Local data should not be returned by reference
  • 5. const 1/2 —  Replacing #define with const ¡  Reduced object code ¡  Better error message ¡  Control over scope —  Enum ¡  Can be used instead of const ¡  Cannot take address of enum —  const_iterator
  • 6. const 2/2 —  const return type —  const input parameter —  const member function of a class —  const overloading of member functions —  Use of mutable
  • 7. casts —  static_cast —  dynamic_cast —  const_cast —  reinterpret_cast
  • 8. Member initialization list —  Efficiency ¡  Default constructor plus assignment are replaced by copy constructor —  Order of declaration in class ¡  Order of initialization list —  Const and reference member ¡  must be initialized in member initializer list
  • 9. inlining —  inlining does in-place replacement of code —  Improves efficiency —  Can lead to increased or decreased code size —  Inlining is a request to compiler —  Certain functions may not be inlined ¡  Called through function pointers ¡  Containing loops ¡  Virtual functions ¡  Constructor and destructor are worst candidates for inlining
  • 10. Compilation —  forward declare where possible —  Keep declaration and definition in separate files ¡  Not possible for templates
  • 11. Non-local static object initialization —  The order of initialization in different translation units is undefined ¡  Solution-> use function which return static object by reference
  • 12. Default functions 1/2 —  Default functions ¡  default constructor, ¡  Assignment ¡  copy constructor ¡  destructor ¡  & operator —  If parameterized constructor is user defined, default constructor will not be provided —  default copy constructor does bitwise copy —  copy constructor and assignment operator not generated if, there is a ref member or const member
  • 13. Default functions 2/2 —  To avoid default copy constructor ¡  declare them private and don't define them ¡  Derive from base class with private copy constructor ¡  Use boost::noncopyable
  • 14. constructor destructor —  Virtual destructor ¡  Needed for base classes ¡  Compiler will invoke the base destructor only without it —  Pure virtual destructor ¡  To create abstract classes ¡  Having body in pure virtual destructor will help compiler while invoking the base class destructor —  Throwing exception ¡  Destructor should never throw exception
  • 15. Inheritance 1/4 —  Design ¡  public inheritance ÷  Should always represent is-a relationship ¢  Penguin is not a bird ¢  Square is not a rectangle
  • 16. Inheritance 2/4 —  Design ¡  Inheritance of interface v/s inheritance of implementation. ¡  public inheritance = inheritance of interfaces ¡  Pure virtual functions = inheritance of interface ¡  impure virtual functions = inheritance of interface plus inheritance of a default implementation. ¡  Non-virtual functions = inheritance of interface plus inheritance of a mandatory implementation.
  • 17. Inheritance 3/4 —  Private inheritance (is implemented in terms of ) ¡  When only implementation is required ¡  e.g. class adapter pattern —  Names in derived classes hide names in base classes —  To make them visible ¡  Using declaration ¡  Forwarding function —  don't redefine non-virtual inherited function
  • 18. Inheritance 4/4 —  Multiple inheritance complexities ¡  lead to ambiguity issues ¡  May need virtual inheritance. ¡  Virtual inheritance costs ÷  size, speed, and complexity of initialization and assignment. ¡  It's practical when base classes have no data. ¡  Some practical uses ÷  public inheritance for an Interface and private inheritance for implementation
  • 19. Composition —  has-a relationship ¡  e.g. car has wheels ¡  Aggregation v/s composition ÷  In C++ a class having pointer members is composition whereas class having concrete members is aggregation ÷  Aggregation has full control of aggregated object ÷  Composition can share the contained object with other classes
  • 20. Virtual Function —  Don’t call virtual function from constructor or destructor ¡  Derived part is not yet constructed when in base class constructor —  Never redefine an inherited default parameter value ¡  default parameter values are statically bound, ¡  virtual functions are dynamically bound.
  • 21. Operator overloading —  Assignment operator should return reference to *this ¡  To support a = b = c ( chaining ) —  Check for self-copy in assignment operator ¡  If copying is deleting the existing object first, the object may delete itself
  • 22. Copy constructor —  copy constructor should call base copy constructor
  • 23. RAII —  Use constructor and destructor for resource management —  Use std::shared_ptr or std::scoped_ptr instead of raw pointers —  Use std::vector instead of arrays —  std::shared_ptr, with function object as argument for action other than delete
  • 24. Misc —  new[] delete[] should be matched —  Namespace usage ¡  package extensibility ¡  better encapsulation ¡  precise control over what to include
  • 25. Designing Interfaces —  Simple to use correctly and hard to misuse ¡  Consistency with built-in types ÷  a = b = c ¡  Restriction on types ¡  Constraining object values ÷  Date, Calendar ¡  Eliminating client side resource management