marq

Dr. Charles Simonyi is the Father of Modern Microsoft Excel                                           JavaScript was originally developed by Brendan Eich of Netscape under the name Mocha, later LiveScript, and finally renamed to JavaScript.                                           The word "Biology" is firstly used by Lamarck and Treviranus                                           Hippocrates (460-370 bc) is known as father of medicine.                                           Galene, 130-200 is known as father of Experimental Physology                                           Aristotle (384-322 BC) is known as Father of Zoology because he wrote the construction and behavior of different animals in his book "Historia animalium"                                           Theophrastus(370-285 BC) is known as father of Botany because he wrote about 500 different plants in his book "Historia Plantarum".                                           John Resig is known as Father of Jquery -                                          HTML is a markup language which is use to design web pages. It was invented in 1990 by Tim Berners-Lee.                                                                The Google was founded by Larry Page and Sergey Brin.                                                                Rasmus Lerdorf was the original creator of PHP. It was first released in 1995.                                                               Facebook was founded by Mark Zuckerberg                                                               Bjarne Stroustrup, creator of C++.                                                                Dennis Ritchie creator of C                                                                                                                              James Gosling, also known as the "Father of Java"                                          At 11.44%, Bihar is India's fastest growing state                                          Father of HTML -Tim Berners Lee                                          orkut was created by Orkut Büyükkökten, a Turkish software engineer                    Photoshop: It came about after Thomas Knoll, a PhD student at the University of Michigan created a program to display grayscale images on a monochrome monitor which at the time was called 'Display'.

constractor


C++ Class Constructors and destructors

Author: Sripriya R     Published on: 17th Aug 2006    |   Last Updated on: 25th Jul 2011

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:

Sample Code
  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. }
Copyright exforsys.com

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:

Sample Code
  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. }
Copyright exforsys.com
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:

Sample Code
  1. class Exforsys
  2. {
  3.     private:
  4.         ...
  5.     public:
  6.         Exforsys()
  7.         { }
  8.         ~ Exforsys()
  9.         { } 
  10. }

No comments:

Post a Comment