SlideShare a Scribd company logo
Constructors & Destructors
Review
CS 308 – Data Structures
What is a constructor?
• It is a member function which initializes a
class.
• A constructor has:
(i) the same name as the class itself
(ii) no return type
class rectangle {
private:
float height;
float width;
int xpos;
int ypos;
public:
rectangle(float, float); // constructor
void draw(); // draw member function
void posn(int, int); // position member function
void move(int, int); // move member function
};
rectangle::rectangle(float h, float w)
{
height = h;
width = w;
xpos = 0;
ypos = 0;
}
Comments on constructors
• A constructor is called automatically whenever a
new instance of a class is created.
• You must supply the arguments to the constructor
when a new instance is created.
• If you do not specify a constructor, the compiler
generates a default constructor for you (expects no
parameters and has an empty body).
void main()
{
rectangle rc(3.0, 2.0);
rc.posn(100, 100);
rc.draw();
rc.move(50, 50);
rc.draw();
}
• Warning: attempting to initialize a data member of
a class explicitly in the class definition is a syntax
error.
Comments on constructors (cont.)
Overloading constructors
• You can have more than one constructor in a class,
as long as each has a different list of arguments.
class rectangle {
private:
float height;
float width;
int xpos;
int ypos;
public:
rectangle(float, float); // constructor
rectangle(); // another constructor
void draw(); // draw member function
void posn(int, int); // position member function
void move(int, int); // move member function
};
Overloading constructors (cont.)
rectangle::rectangle()
{
height = 10;
width = 10;
xpos = 0;
ypos = 0;
}
void main()
{
rectangle rc1(3.0, 2.0);
rectangle rc2();
rc1.draw();
rc2.draw();
}
Composition: objects as
members of classes
• A class may have objects of other classes as
members.
class properties {
private:
int color;
int line;
public:
properties(int, int); // constructor
};
properties::properties(int c, int l)
{
color = c;
line = l;
}
class rectangle {
private:
float height;
float width;
int xpos;
int ypos;
properties pr; // another object
public:
rectangle(float, float, int, int ); // constructor
void draw(); // draw member function
void posn(int, int); // position member function
void move(int, int); // move member function
};
Composition: objects as
members of classes (cont.)
Composition: objects as
members of classes (cont.)
rectangle::rectangle(float h, float w, int c, int l):pr(c, l)
{
height = h;
width = w;
xpos = 0;
ypos = 0;
};
void main()
{
rectangle rc(3.0, 2.0, 1, 3);
C++ statements;
}
What is a destructor?
• It is a member function which deletes an object.
• A destructor function is called automatically when
the object goes out of scope:
(1) the function ends
(2) the program ends
(3) a block containing temporary variables ends
(4) a delete operator is called
• A destructor has:
(i) the same name as the class but is preceded by a tilde (~)
(ii) no arguments and return no values
class string {
private:
char *s;
int size;
public:
string(char *); // constructor
~string(); // destructor
};
string::string(char *c)
{
size = strlen(c);
s = new char[size+1];
strcpy(s,c);
}
string::~string()
{
delete []s;
}
Comments on destructors
• If you do not specify a destructor, the
compiler generates a default destructor for
you.
• When a class contains a pointer to memory
you allocate, it is your responsibility to
release the memory before the class
instance is destroyed.
What is a copy constructor?
• It is a member function which initializes an
object using another object of the same
class.
• A copy constructor has the following
general function prototype:
class_name (const class_name&);
class rectangle {
private:
float height;
float width;
int xpos;
int ypos;
public:
rectangle(float, float); // constructor
rectangle(const rectangle&); // copy constructor
void draw(); // draw member function
void posn(int, int); // position member function
void move(int, int); // move member function
};
rectangle::rectangle(const rectangle& old_rc)
{
height = old_rc.height;
width = old_rc.width;
xpos = old_rc.xpos;
ypos = old_rc.ypos;
}
void main()
{
rectangle rc1(3.0, 2.0); // use constructor
rectangle rc2(rc1); // use copy constructor
rectangle rc3 = rc1; // alternative syntax for
// copy constructor
C++ statements;
}
Defining copy constructors is
very important
• In the absence of a copy constructor, the C+
+ compiler builds a default copy
constructor for each class which is doing a
memberwise copy between objects.
• Default copy constructors work fine unless
the class contains pointer data members ...
why???
#include <iostream.h>
#include <string.h>
class string {
private:
char *s;
int size;
public:
string(char *); // constructor
~string(); // destructor
void print();
void copy(char *);
};
void string::print()
{
cout << s << endl;
}
void string::copy(char *c)
{
strcpy(s, c);
}
void main()
{
string str1("George");
string str2 = str1; // default copy constructor
str1.print(); // what is printed ?
str2.print();
str2.copy("Mary");
str1.print(); // what is printed now ?
str2.print();
}
Defining a copy constructor for the
above example:
class string {
private:
char *s;
int size;
public:
string(char *); // constructor
~string(); // destructor
string(const string&); // copy constructor
void print();
void copy(char *);
};
string::string(const string& old_str)
{
size = old_str.size;
s = new char[size+1];
strcpy(s,old_str.s);
}
void main()
{
string str1("George");
string str2 = str1;
str1.print(); // what is printed ?
str2.print();
str2.copy("Mary");
str1.print(); // what is printed now ?
str2.print();
}
Note: same results can be obtained by overloading the assignment
operator.

More Related Content

PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
PPTX
Virtual function in C++ Pure Virtual Function
PPTX
PPTX
Templates in C++
PPTX
Virtual base class
PPT
Operators in C++
PPTX
Polymorphism in C++
PPTX
Templates presentation
Basic Concepts of OOPs (Object Oriented Programming in Java)
Virtual function in C++ Pure Virtual Function
Templates in C++
Virtual base class
Operators in C++
Polymorphism in C++
Templates presentation

What's hot (20)

PDF
Object-oriented Programming-with C#
PPT
Inheritance
PPT
Abstract class in java
PPTX
Dynamic memory allocation
PPT
OOP in C++
PPTX
Friend function in c++
PPTX
Constructor and Destructor in c++
PPTX
inheritance c++
PPTX
Object Oriented Programming with C#
PDF
Wrapper classes
PPTX
polymorphism and virtual function
PPTX
OOPS In JAVA.pptx
PDF
Memory Management C++ (Peeling operator new() and delete())
PDF
Constructors and Destructors
PPTX
PPTX
Interface in java
PDF
Object oriented programming With C#
PPTX
Loops in C
PPTX
Inheritance in Object Oriented Programming
PPTX
Access Modifier.pptx
Object-oriented Programming-with C#
Inheritance
Abstract class in java
Dynamic memory allocation
OOP in C++
Friend function in c++
Constructor and Destructor in c++
inheritance c++
Object Oriented Programming with C#
Wrapper classes
polymorphism and virtual function
OOPS In JAVA.pptx
Memory Management C++ (Peeling operator new() and delete())
Constructors and Destructors
Interface in java
Object oriented programming With C#
Loops in C
Inheritance in Object Oriented Programming
Access Modifier.pptx
Ad

Viewers also liked (20)

PPTX
Constructors & destructors
PPT
Oop lec 5-(class objects, constructor & destructor)
PPTX
constructor & destructor in cpp
PPTX
Constructor ppt
PPT
Constructor & Destructor
PPT
constructor and destructor-object oriented programming
PDF
Constructor & destructor
PDF
Constructor and Destructor
PPTX
Constructor and destructor in c++
PPTX
Constructor and destructor
PDF
Dynamics allocation
PPTX
File system implementation
PPTX
File System Implementation
PPTX
Inheritance in OOPS
PPT
Ashish oot
PPTX
Constructor in java
PPTX
Constructor & destructor
PPT
Oop concepts
PPTX
01 introduction to oop and java
PPT
Inheritance
Constructors & destructors
Oop lec 5-(class objects, constructor & destructor)
constructor & destructor in cpp
Constructor ppt
Constructor & Destructor
constructor and destructor-object oriented programming
Constructor & destructor
Constructor and Destructor
Constructor and destructor in c++
Constructor and destructor
Dynamics allocation
File system implementation
File System Implementation
Inheritance in OOPS
Ashish oot
Constructor in java
Constructor & destructor
Oop concepts
01 introduction to oop and java
Inheritance
Ad

Similar to Constructor and Destructor PPT (20)

PPT
ReviewConstructorDestructorof cplusplus.ppt
PPT
ReviewConstDestr FOR CONSRUCTOR AND DESTRUCTORppt
PPT
Review constdestr
PPT
Review constdestr
PPT
Unit ii
PPT
Constructor and destructor in C++
PPT
Constructors and destructors in C++ part 2
PPTX
Constructors in C++.pptx
PDF
Const-Dest-PPT-5_removedhbfhfhfhdhfdhd.pdf
PPT
Constructors & Destructors in C++ Simplified
PPT
5 Constructors and Destructors
PPT
Friend this-new&delete
PPTX
constructors and destructors
PPTX
Constructor in c++
PDF
Chapter19 constructor-and-destructor
PDF
Constructors and destructors
PPTX
Constructor and Destructor
PPTX
OOP-Lecture-05 (Constructor_Destructor).pptx
PPTX
An introduction to Constructors and destructors in c++ .pptx
PPTX
Constructor and desturctor
ReviewConstructorDestructorof cplusplus.ppt
ReviewConstDestr FOR CONSRUCTOR AND DESTRUCTORppt
Review constdestr
Review constdestr
Unit ii
Constructor and destructor in C++
Constructors and destructors in C++ part 2
Constructors in C++.pptx
Const-Dest-PPT-5_removedhbfhfhfhdhfdhd.pdf
Constructors & Destructors in C++ Simplified
5 Constructors and Destructors
Friend this-new&delete
constructors and destructors
Constructor in c++
Chapter19 constructor-and-destructor
Constructors and destructors
Constructor and Destructor
OOP-Lecture-05 (Constructor_Destructor).pptx
An introduction to Constructors and destructors in c++ .pptx
Constructor and desturctor

Recently uploaded (20)

PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
top salesforce developer skills in 2025.pdf
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
AI in Product Development-omnex systems
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
How to Migrate SBCGlobal Email to Yahoo Easily
Which alternative to Crystal Reports is best for small or large businesses.pdf
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
top salesforce developer skills in 2025.pdf
Upgrade and Innovation Strategies for SAP ERP Customers
Design an Analysis of Algorithms I-SECS-1021-03
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
AI in Product Development-omnex systems
How Creative Agencies Leverage Project Management Software.pdf
Operating system designcfffgfgggggggvggggggggg
Reimagine Home Health with the Power of Agentic AI​
How to Choose the Right IT Partner for Your Business in Malaysia
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Design an Analysis of Algorithms II-SECS-1021-03
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PTS Company Brochure 2025 (1).pdf.......
2025 Textile ERP Trends: SAP, Odoo & Oracle
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx

Constructor and Destructor PPT

  • 1. Constructors & Destructors Review CS 308 – Data Structures
  • 2. What is a constructor? • It is a member function which initializes a class. • A constructor has: (i) the same name as the class itself (ii) no return type
  • 3. class rectangle { private: float height; float width; int xpos; int ypos; public: rectangle(float, float); // constructor void draw(); // draw member function void posn(int, int); // position member function void move(int, int); // move member function }; rectangle::rectangle(float h, float w) { height = h; width = w; xpos = 0; ypos = 0; }
  • 4. Comments on constructors • A constructor is called automatically whenever a new instance of a class is created. • You must supply the arguments to the constructor when a new instance is created. • If you do not specify a constructor, the compiler generates a default constructor for you (expects no parameters and has an empty body).
  • 5. void main() { rectangle rc(3.0, 2.0); rc.posn(100, 100); rc.draw(); rc.move(50, 50); rc.draw(); } • Warning: attempting to initialize a data member of a class explicitly in the class definition is a syntax error. Comments on constructors (cont.)
  • 6. Overloading constructors • You can have more than one constructor in a class, as long as each has a different list of arguments. class rectangle { private: float height; float width; int xpos; int ypos; public: rectangle(float, float); // constructor rectangle(); // another constructor void draw(); // draw member function void posn(int, int); // position member function void move(int, int); // move member function };
  • 7. Overloading constructors (cont.) rectangle::rectangle() { height = 10; width = 10; xpos = 0; ypos = 0; } void main() { rectangle rc1(3.0, 2.0); rectangle rc2(); rc1.draw(); rc2.draw(); }
  • 8. Composition: objects as members of classes • A class may have objects of other classes as members. class properties { private: int color; int line; public: properties(int, int); // constructor }; properties::properties(int c, int l) { color = c; line = l; }
  • 9. class rectangle { private: float height; float width; int xpos; int ypos; properties pr; // another object public: rectangle(float, float, int, int ); // constructor void draw(); // draw member function void posn(int, int); // position member function void move(int, int); // move member function }; Composition: objects as members of classes (cont.)
  • 10. Composition: objects as members of classes (cont.) rectangle::rectangle(float h, float w, int c, int l):pr(c, l) { height = h; width = w; xpos = 0; ypos = 0; }; void main() { rectangle rc(3.0, 2.0, 1, 3); C++ statements; }
  • 11. What is a destructor? • It is a member function which deletes an object. • A destructor function is called automatically when the object goes out of scope: (1) the function ends (2) the program ends (3) a block containing temporary variables ends (4) a delete operator is called • A destructor has: (i) the same name as the class but is preceded by a tilde (~) (ii) no arguments and return no values
  • 12. class string { private: char *s; int size; public: string(char *); // constructor ~string(); // destructor }; string::string(char *c) { size = strlen(c); s = new char[size+1]; strcpy(s,c); } string::~string() { delete []s; }
  • 13. Comments on destructors • If you do not specify a destructor, the compiler generates a default destructor for you. • When a class contains a pointer to memory you allocate, it is your responsibility to release the memory before the class instance is destroyed.
  • 14. What is a copy constructor? • It is a member function which initializes an object using another object of the same class. • A copy constructor has the following general function prototype: class_name (const class_name&);
  • 15. class rectangle { private: float height; float width; int xpos; int ypos; public: rectangle(float, float); // constructor rectangle(const rectangle&); // copy constructor void draw(); // draw member function void posn(int, int); // position member function void move(int, int); // move member function };
  • 16. rectangle::rectangle(const rectangle& old_rc) { height = old_rc.height; width = old_rc.width; xpos = old_rc.xpos; ypos = old_rc.ypos; } void main() { rectangle rc1(3.0, 2.0); // use constructor rectangle rc2(rc1); // use copy constructor rectangle rc3 = rc1; // alternative syntax for // copy constructor C++ statements; }
  • 17. Defining copy constructors is very important • In the absence of a copy constructor, the C+ + compiler builds a default copy constructor for each class which is doing a memberwise copy between objects. • Default copy constructors work fine unless the class contains pointer data members ... why???
  • 18. #include <iostream.h> #include <string.h> class string { private: char *s; int size; public: string(char *); // constructor ~string(); // destructor void print(); void copy(char *); }; void string::print() { cout << s << endl; }
  • 19. void string::copy(char *c) { strcpy(s, c); } void main() { string str1("George"); string str2 = str1; // default copy constructor str1.print(); // what is printed ? str2.print(); str2.copy("Mary"); str1.print(); // what is printed now ? str2.print(); }
  • 20. Defining a copy constructor for the above example: class string { private: char *s; int size; public: string(char *); // constructor ~string(); // destructor string(const string&); // copy constructor void print(); void copy(char *); };
  • 21. string::string(const string& old_str) { size = old_str.size; s = new char[size+1]; strcpy(s,old_str.s); } void main() { string str1("George"); string str2 = str1; str1.print(); // what is printed ? str2.print(); str2.copy("Mary"); str1.print(); // what is printed now ? str2.print(); } Note: same results can be obtained by overloading the assignment operator.