<NOSCRIPT><A href="http://about.com/" target=_blank><IMG src="C++ Tutorial - Lesson 18 Constructors and Destructors_files/0.gif" border=0></A></NOSCRIPT>   About > Computing & Technology > C/C++ 
Search     
 Hi, I'm John Kopp, your guide to C, C++ and C# programming. This site provides what you need to know to learn C, C++ or C# programming. Browse the subjects on the left, try one of the tutorials or use the search box to get started.

C/C++

with John Kopp
Your Guide to one of hundreds of sites
 Home · Articles · Forums · Chat · Newsletters · Help    
Subjects

  ESSENTIALS
· C Tutorial
· C++ Tutorial
· C/C++ Glossary
· Tips
· More Tutorials
  BUYER'S GUIDE

Product Reviews
Top Picks
C Tutorials
C++ Tutorials
C#
Advanced C
Advanced C++
Beginning C
Beginning C++
Books
C++ Builder
Careers
CGI
CM
Dictionaries
CompilingDebugging
Freeware/Shareware
Humor
Magazines
OOAD
Polls
SoftwareEngineer'n
Style
STL
UNIX/GNU
Visual C++

Subject Library

All articles on this topic

 

Stay up-to-date!
Subscribe to our newsletter.

Web Hosting
Global Servers

 
 
 
Advertisement
> Free Credit Report
 
C++ Tutorial - Lesson 18: Constructors and Destructors
 More of this Feature
• Constructors and Destructors, Page 1
• Constructors and Destructors, Page 2
• Constructors and Destructors, Page 3
• Member Initialization Lists
• Member Initialization Lists, cont.
• Solution to Practice Problem
 
 Related Resources
• C++ Tutorial - Introduction to Classes
• C++ Tutorial - Classes, Members and Methods
• C++ Tutorial - Function Overloading
• Advanced C++ Tutorials
• Simple Solutions in C++
• C++ Programming Tips
• All C++ Lessons
 

by John Kopp

Welcome to About.com's free tutorial on C++ programming. This lesson covers constructors and destructors. Constructors and destructors are special class methods. A constructor is called whenever an object is defined or dynamically allocated using the "new" operator. The purpose of a constructor is to initialize data members and sometimes to obtain resources such as memory, or a mutex or lock on a shared resource such as a hardware device. An object's destructor is called whenever an object goes out of scope or when the "delete" operator is called on a pointer to the object. The purpose of the destructor is clean up. It is used to free memory and to release any locks or mutexes on system resources.

The definition and use of constructors and destructors is fairly simple. First, I'll outline some basic rules on constructors and destructors, and then provide the details using some simple examples.

  • A constructor is a method that has the same name as its class.
  • A destructor is a method that has as its name the class name prefixed by a tilde, ~.
  • Neither constructors nor destructors return values. They have no return type specified.
  • Constructors can have arguments.
  • Constructors can be overloaded.
  • If any constructor is written for the class, the compiler will not generate a default constructor.
  • The default constructor is a constructor with no arguments, or a constructor that provides defaults for all arguments.
  • The container classes such as vector require default constructors to be available for the classes they hold. Dynamically allocated class arrays also require a default constructor. If any constructors are defined, you should always define a default constructor as well.
  • Destructors have no arguments and thus cannot be overloaded.

Next page > Constructors, Continued. > Page 1, 2, 3, 4, 5


Subscribe to the C/C++ Newsletter
Name
Email






Email this page | Sign up for a Newsletter |
Explore More
Most Popular Articles
• C++ Programming Tutorial
• C Programming Tutorial
• C++ Tutorial - Lesson 12: File Input and Output
• C++ Tutorial - Lesson 4: Input and Output - cin, cout, iostr...
• C++ Tutorial - Lesson 10: Arrays and Vectors
• C++ Tutorial - Lesson 1: Writing and Compiling A First Progr...
• Tutorials
What's Hot Now
• Teach Yourself C++ in 21 Days by Jesse Liberty
• C Programming Tutorial
• C++ Tutorial - Lesson 25: Operator Overloading
• The C Programming Language, 2nd Edition by Kernighan and Rit...
• Topics in C++: Function Template Specialization
• C Programming Tips: Using Long Constants
• C Tutorial - Lesson 5: Conditional Processing, Part 1 - Rela...
About Us | Advertise on This Site | User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2004 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.