SlideShare a Scribd company logo
THE BIG THREE

June 2009
Roman Okolovich
Example
class CDummy
{
public:
  CDummy()
   {m_intCount++; cout << "create" << endl;}
  ~CDummy()
   {m_intCount--; cout << "destroy" << endl;}

     static int m_intCount;
};

int CDummy::m_intCount = 0;

void foo(CDummy object)
{ /* Argument passed by value */ }

int main(void)                                          Result:
{
  CDummy obj;                                           Create
  cout << "Count = " << obj.m_intCount << endl;          cnt = 1
  foo(obj);                                             destroy
  cout << "Count = " << obj.m_intCount << endl;          cnt = 0
  foo(obj);                                             destroy
  cout << "Count = " << obj.m_intCount << endl;          cnt = -1
  return 0;                                             destroy
}

     08.02.2010                         The Big Three               2
Special member functions
   Special member functions in C++ are functions which the compiler will
    automatically generate if they are used, but not declared explicitly by the
    programmer. The special member functions are:
       Default constructor (if no other constructor is explicitly declared)
       Copy constructor
       Copy assignment operator
       Destructor
   The rule of three (also known as the Law of The Big Three or The Big Three) is a
    rule of thumb in C++ that claims that if a class defines one of the following it
    should probably explicitly define all three:
       Copy constructor
       Copy assignment operator
       Destructor
class CDefault
{
 public:
  CDefault();                                                //   default constructor
  CDefault(const CDefault& object);                          //   copy constructor
  virtual ~CDefault();                                       //   default destructor
  Cdefault& operator = (const CDefault& object);             //   copy assignment operator
};

    08.02.2010                               The Big Three                                   3
Overloaded member function
class CDummy
{
  public:
   CDummy(int p) : m_intValue(p) { cout << "create" << m_intValue << endl; }
   virtual ~CDummy()             { cout << "destroy" << endl; }

     CDummy(const CDummy& object) {m_intValue = object.m_intValue;}

     // polymorphic functions (different data types)
     void overloaded(void)       { cout << "overload "       << ++m_intValue << endl; }
     void overloaded(void) const { cout << "overload const " << m_intValue << endl; }

     int m_intValue;
};

int main(void)
{
  CDummy const dd(2);
  dd.overloaded();
  CDummy bb(3);
  bb.overloaded();

    Cdummy const obj(bb); // copy constructor can be without const
    Cdummy cobj(obj);     // copy constructor must be with const
}


     08.02.2010                           The Big Three                                   4
Default class declaration
class CDefault
{
public:
 // default constructor
 CDefault();
 // copy constructor
 CDefault(const CDefault& object);
 // default destructor
 ~CDefault();

 // copy assignment operator
 CDefault& operator = (const CDefault& object);

 // Optional
 // Note: overloaded operator should be overloaded with const also

 CDefault&         operator*();
 CDefault const&   operator*() const;
 CDefault*         operator->();
 CDefault const*   operator->() const;
};



  08.02.2010                         The Big Three                   5
References
 Special member functions
 Assignment operator in C++
 Type polymorphism




    08.02.2010        The Big Three   6

More Related Content

PDF
JavaScript - Agora nervoso
DOCX
Class array
PDF
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
PDF
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
DOCX
Est 8 2 nd
PDF
JavaSE7 Launch Event: Java7xGroovy
PPTX
Queue oop
JavaScript - Agora nervoso
Class array
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
Est 8 2 nd
JavaSE7 Launch Event: Java7xGroovy
Queue oop

What's hot (20)

PDF
5 1. character processing
PPTX
Untitled presentation(4)
PPTX
C++ hello world
PDF
D vs OWKN Language at LLnagoya
PPT
NS2: Binding C++ and OTcl variables
DOCX
Jarmo van de Seijp Shadbox ERC223
PDF
PDF
When RV Meets CEP (RV 2016 Tutorial)
PDF
Functions - complex first class citizen
PPTX
Php 7.x 8.0 and hhvm and
PDF
PyCon KR 2019 sprint - RustPython by example
PPTX
PHP in 2018 - Q1 - AFUP Limoges
PDF
Rcpp11 genentech
PPT
Function
DOC
PDF
Data structure programs in c++
PDF
completion_proc and history
PDF
20151224-games
PDF
function* - ES6, generators, and all that (JSRomandie meetup, February 2014)
5 1. character processing
Untitled presentation(4)
C++ hello world
D vs OWKN Language at LLnagoya
NS2: Binding C++ and OTcl variables
Jarmo van de Seijp Shadbox ERC223
When RV Meets CEP (RV 2016 Tutorial)
Functions - complex first class citizen
Php 7.x 8.0 and hhvm and
PyCon KR 2019 sprint - RustPython by example
PHP in 2018 - Q1 - AFUP Limoges
Rcpp11 genentech
Function
Data structure programs in c++
completion_proc and history
20151224-games
function* - ES6, generators, and all that (JSRomandie meetup, February 2014)
Ad

Viewers also liked (10)

PPT
Major record label research project
PPTX
The big 3 record labels
PPT
Record labels
PPTX
Record labels
PPTX
The big 3 major music labels
PPTX
Record label
PPT
Structure Of A Major Record Label
PPTX
Structure and breakdown of Record Labels
PPTX
Music Industry
PPTX
Music record labels 2 powerpoint
Major record label research project
The big 3 record labels
Record labels
Record labels
The big 3 major music labels
Record label
Structure Of A Major Record Label
Structure and breakdown of Record Labels
Music Industry
Music record labels 2 powerpoint
Ad

Similar to The Big Three (20)

PPT
Lec 45.46- virtual.functions
PPTX
OOPs & C++ UNIT 3
PPT
C++ Interview Questions
PPTX
21CSC101T best ppt ever OODP UNIT-2.pptx
PPT
Friend this-new&delete
PPTX
Chp 3 C++ for newbies, learn fast and earn fast
PPT
3d7b7 session4 c++
PPTX
Constructor in c++
PPTX
Week 3 - OOP Constructor (1)classtas.pptx
PPTX
CPP Homework Help
PPT
Constructor and destructor in C++
PDF
Assign
PPT
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
PPTX
PPT
Constructors and destructors in C++ part 2
PPTX
constructors
TXT
Advance C++notes
PDF
CS225_Prelecture_Notes 2nd
PDF
Classes-and-Objects-in-C++.pdf
PPT
C++ - Constructors,Destructors, Operator overloading and Type conversion
Lec 45.46- virtual.functions
OOPs & C++ UNIT 3
C++ Interview Questions
21CSC101T best ppt ever OODP UNIT-2.pptx
Friend this-new&delete
Chp 3 C++ for newbies, learn fast and earn fast
3d7b7 session4 c++
Constructor in c++
Week 3 - OOP Constructor (1)classtas.pptx
CPP Homework Help
Constructor and destructor in C++
Assign
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Constructors and destructors in C++ part 2
constructors
Advance C++notes
CS225_Prelecture_Notes 2nd
Classes-and-Objects-in-C++.pdf
C++ - Constructors,Destructors, Operator overloading and Type conversion

More from Roman Okolovich (11)

PPTX
Unit tests and TDD
PPTX
C# XML documentation
PPTX
code analysis for c++
PPT
Using QString effectively
PDF
Ram Disk
PDF
64 bits for developers
PDF
Virtual Functions
PDF
Visual Studio 2008 Overview
PDF
State Machine Framework
PDF
Parallel Programming
PDF
Smart Pointers
Unit tests and TDD
C# XML documentation
code analysis for c++
Using QString effectively
Ram Disk
64 bits for developers
Virtual Functions
Visual Studio 2008 Overview
State Machine Framework
Parallel Programming
Smart Pointers

Recently uploaded (20)

PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Approach and Philosophy of On baking technology
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPT
Teaching material agriculture food technology
PDF
Machine learning based COVID-19 study performance prediction
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Electronic commerce courselecture one. Pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
MYSQL Presentation for SQL database connectivity
PDF
cuic standard and advanced reporting.pdf
PDF
Modernizing your data center with Dell and AMD
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Empathic Computing: Creating Shared Understanding
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Spectral efficient network and resource selection model in 5G networks
Reach Out and Touch Someone: Haptics and Empathic Computing
Approach and Philosophy of On baking technology
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Digital-Transformation-Roadmap-for-Companies.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Advanced methodologies resolving dimensionality complications for autism neur...
Teaching material agriculture food technology
Machine learning based COVID-19 study performance prediction
Building Integrated photovoltaic BIPV_UPV.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Electronic commerce courselecture one. Pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
MYSQL Presentation for SQL database connectivity
cuic standard and advanced reporting.pdf
Modernizing your data center with Dell and AMD
NewMind AI Monthly Chronicles - July 2025
Empathic Computing: Creating Shared Understanding
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Spectral efficient network and resource selection model in 5G networks

The Big Three

  • 1. THE BIG THREE June 2009 Roman Okolovich
  • 2. Example class CDummy { public: CDummy() {m_intCount++; cout << "create" << endl;} ~CDummy() {m_intCount--; cout << "destroy" << endl;} static int m_intCount; }; int CDummy::m_intCount = 0; void foo(CDummy object) { /* Argument passed by value */ } int main(void) Result: { CDummy obj; Create cout << "Count = " << obj.m_intCount << endl; cnt = 1 foo(obj); destroy cout << "Count = " << obj.m_intCount << endl; cnt = 0 foo(obj); destroy cout << "Count = " << obj.m_intCount << endl; cnt = -1 return 0; destroy } 08.02.2010 The Big Three 2
  • 3. Special member functions  Special member functions in C++ are functions which the compiler will automatically generate if they are used, but not declared explicitly by the programmer. The special member functions are:  Default constructor (if no other constructor is explicitly declared)  Copy constructor  Copy assignment operator  Destructor  The rule of three (also known as the Law of The Big Three or The Big Three) is a rule of thumb in C++ that claims that if a class defines one of the following it should probably explicitly define all three:  Copy constructor  Copy assignment operator  Destructor class CDefault { public: CDefault(); // default constructor CDefault(const CDefault& object); // copy constructor virtual ~CDefault(); // default destructor Cdefault& operator = (const CDefault& object); // copy assignment operator }; 08.02.2010 The Big Three 3
  • 4. Overloaded member function class CDummy { public: CDummy(int p) : m_intValue(p) { cout << "create" << m_intValue << endl; } virtual ~CDummy() { cout << "destroy" << endl; } CDummy(const CDummy& object) {m_intValue = object.m_intValue;} // polymorphic functions (different data types) void overloaded(void) { cout << "overload " << ++m_intValue << endl; } void overloaded(void) const { cout << "overload const " << m_intValue << endl; } int m_intValue; }; int main(void) { CDummy const dd(2); dd.overloaded(); CDummy bb(3); bb.overloaded(); Cdummy const obj(bb); // copy constructor can be without const Cdummy cobj(obj); // copy constructor must be with const } 08.02.2010 The Big Three 4
  • 5. Default class declaration class CDefault { public: // default constructor CDefault(); // copy constructor CDefault(const CDefault& object); // default destructor ~CDefault(); // copy assignment operator CDefault& operator = (const CDefault& object); // Optional // Note: overloaded operator should be overloaded with const also CDefault& operator*(); CDefault const& operator*() const; CDefault* operator->(); CDefault const* operator->() const; }; 08.02.2010 The Big Three 5
  • 6. References  Special member functions  Assignment operator in C++  Type polymorphism 08.02.2010 The Big Three 6