Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

C++ Class Constructors and destructors

By Sripriya R | on August 17, 2006 |
C++ Tutorials

Class Constructors and destructors in C++

In this C++ tutorial you will learn about Class Constructors and destructors in C++ viz., Constructors, What is the use of Constructor, General Syntax of Constructor, Destructors, What is the use of Destructors and General Syntax of Destructors.

Constructors:

What is the use of Constructor

The main use of constructors is to initialize objects. The function of initialization is automatically carried out by the use of a special member function called a constructor.

General Syntax of Constructor

A constructor is a special member function that takes the same name as the class name. The syntax generally is as given below:

{ arguments};

The default constructor for a class X has the form

X::X()

In the above example, the arguments are optional.

The constructor is automatically named when an object is created. A constructor is named whenever an object is defined or dynamically allocated using the “new” operator.

There are several forms in which a constructor can take its shape namely:

Default Constructor:

This constructor has no arguments in it. The default Constructor is also called as the no argument constructor.

For example:

  1. class Exforsys 
  2. {
  3.     private:
  4.         int a,b; 
  5.     public:
  6.         Exforsys(); 
  7.         ...
  8. };
  9.  
  10. Exforsys :: Exforsys()
  11. {
  12.     a=0;
  13.     b=0;
  14. }

Copy constructor:

This constructor takes one argument, also called one argument constructor. The main use of copy constructor is to initialize the objects while in creation, also used to copy an object. The copy constructor allows the programmer to create a new object from an existing one by initialization.

For example to invoke a copy constructor the programmer writes:

Exforsys e3(e2);

or

Exforsys e3=e2;

Both the above formats can be sued to invoke a copy constructor.

For Example:

  1.  #include <iostream> 
  2. using namespace std;
  3. class Exforsys
  4. {
  5.     private:
  6.         int a;
  7.     public:
  8.         Exforsys()
  9.         { }
  10.         Exforsys(int w)
  11.     {
  12.         a=w;
  13.     } 
  14.     Exforsys(Exforsys& e)
  15.     {
  16.         a=e.a;
  17.         cout << " Example of Copy Constructor";
  18.     }
  19.     void result()
  20.     {
  21.         cout<< a;
  22.     } 
  23. };
  24. void main()
  25. {
  26.     Exforsys e1(50); 
  27.     Exforsys e3(e1);
  28.     cout<< "ne3=";e3.result();
  29. }

In the above the copy constructor takes one argument an object of type Exforsys which is passed by reference. The output of the above program is

Some important points about constructors:

  • A constructor takes the same name as the class name.
  • The programmer cannot declare a constructor as virtual or static, nor can the programmer declare a constructor as const, volatile, or const volatile.
  • No return type is specified for a constructor.
  • The constructor must be defined in the public. The constructor must be a public member.
  • Overloading of constructors is possible. This will be explained in later sections of this tutorial.

Destructors

What is the use of Destructors

Destructors are also special member functions used in C++ programming language. Destructors have the opposite function of a constructor. The main use of destructors is to release dynamic allocated memory. Destructors are used to free memory, release resources and to perform other clean up. Destructors are automatically named when an object is destroyed. Like constructors, destructors also take the same name as that of the class name.

General Syntax of Destructors

~ classname();

The above is the general syntax of a destructor. In the above, the symbol tilda ~ represents a destructor which precedes the name of the class.

Some important points about destructors:

  • Destructors take the same name as the class name.
  • Like the constructor, the destructor must also be defined in the public. The destructor must be a public member.
  • The Destructor does not take any argument which means that destructors cannot be overloaded.
  • No return type is specified for destructors.

For example:

  1. class Exforsys
  2. {
  3.     private:
  4.         ...
  5.     public:
  6.         Exforsys()
  7.         { }
  8.         ~ Exforsys()
  9.         { } 
  10. }
« « HR Interview – HR Interview Etiquettes
How Consistent Practice Can Help You Improve Your Memory » »

Author Description

Avatar

Free Training

RSSSubscribe 0 Followers
  • Popular
  • Recent
  • How to Access C++ Class Members

    August 21, 2007 - 0 Comment
  • C++ Memory Management Operators

    October 12, 2007 - 0 Comment
  • C++ String Representation and Handling

    September 8, 2007 - 0 Comment
  • C++ Static Functions

    February 12, 2007 - 0 Comment
  • C++ Standard Input Output Stream

    August 24, 2007 - 0 Comment
  • C++ Dereference Operator

    October 14, 2007 - 0 Comment
  • C++ Structure Part II

    August 19, 2007 - 0 Comment
  • C++ Abstraction

    April 26, 2007 - 0 Comment
  • C++ Operators Part I

    August 24, 2007 - 0 Comment
  • C++ Operator Overloading Part 1

    September 7, 2006 - 0 Comment
  • C++ Dereference Operator

    October 14, 2007 - 0 Comment
  • C++ Memory Management Operators

    October 12, 2007 - 0 Comment
  • C++ Void Pointer and Null Pointer

    October 12, 2007 - 0 Comment
  • C++ Pointers

    October 1, 2007 - 0 Comment
  • C++ Static Functions

    October 1, 2007 - 0 Comment
  • C++ Friend Functions

    September 28, 2007 - 0 Comment
  • C++ Pure Virtual Function and Base Class

    September 28, 2007 - 0 Comment
  • C++ Virtual Functions

    September 25, 2007 - 0 Comment
  • C++ Inline Functions

    September 25, 2007 - 0 Comment
  • C++ Function Passing Types

    September 12, 2007 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • C++ Dereference Operator
  • C++ Memory Management Operators
  • C++ Void Pointer and Null Pointer
  • C++ Pointers
  • C++ Static Functions

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT