new features of ansi c++ standard

Introduction -: The ANSI C++ standard add several new features to the original c++ specification. Some are added to provide better control in certain situation other are added for providing convenience to c++ programmers. The important features are here as like 1 . New data type Bool wchar_t 2. New Operators const_cast static_cast dynamic_cast reinterpret_cast … Read more

manipulating string in c++

Introduction-:  A string is a sequence of character. we know that the c++ does not  support a build string type. We have use earlier null terminated character array to store and manipulate string. These string are called C string or C style string. ANSI standard c++ now provide a special new class called string. this … Read more

introduction to standard template library in c++

Introduction-: We have seen how template can be used to create generic class and function that could extended support for generic Programming. In order to help the c++ user in generic programming.Alexander Stepanov and Meng Lee of Hewlett packard developed a set of general purpose templatized (algorithms) that could be use as a standard approach … Read more

templates in c++

Template introduction -: Template is one of the features added to c++. It is a new concept which enable us to define generic classes, functions and thus provides support for generic programming. Generic programming is an approach where generic types are used as parameters in algorithm so that they work for a variety of suitable … Read more

managing console input output operations in c++

Managing Console Input Output Operations in C++ :– Every program takes some data as input and generates processed data as output from the familiar input process output cycle. Like this way c++ programming language provide this type facility. We have already use these operator these are the cin  (<<) and cout (<< )operator. C++ support rich … Read more

polymorphism in c++

Polymorphism in c++-: Polymorphism is one of the crucial features of OOP. It simply means ‘one name, multiple form’. Polymorphism is a Greek word it means ” ability to take more than one ” An operation may exhibit difference behavior in different instance. The behavior depends upon the types of data used in the operation. For … Read more

inheritance in c++

Inheritance in C++ -: Inheritance is the process by which object one class acquire the properties of object of another class. Inheritance support the concept of hierarchical classification. For example the bird ‘robin’ is the part of the class ” flying bird” which is again a part of the class “bird”. The principle behind this … Read more

Destructors in c++

Destructors In C++ -: C++ also provide another member function called the destructors that destroyed object when they are no longer required. It is the name implies . It is used to destroy the object that have been created by a constructors. Like the  constructors is a member function whose name is the same as … Read more

constructor in c++

Constructor In C++ -: In the c++ programming language A constructors is a special member function whose task is to initialize the object of its class. It is a special because its name is the same as the class name. The constructors is involved whenever an object of its associated class is created. It is called constructors because it construct the values of data member of the class.

How the constructor declared and defined

//class with a constructor

class integer

{

   int a, b;

   public:

    integer (void);    // constructor declared
    ...........
    ...........
};

integer :: integer (void)  //constructors defined

{

   a=0;
   b=0;

} 

Characteristic for define the constructors -: There are some special character for defining the constructors function these are ..

  • Constructor should be declared in public section.
  • It can not inherited through a derived class can call the base class constructor.
  • These are invoked automatically when the object are created.
  • we can not refers to their address.
  • constructor do not have any return type not even void and therefore and they cannot return values.
  •  Like any other c++ function they can have default arguments.
  • Constructor can not be used as a member of a union.
  • constructor make ‘implicit value’ to the operator new and delete when memory allocation is require.
  • constructor can not be virtual.

 

Types of Constructors in c++ programming language-:  In the  C++ programming language constructor has many types these are

  1. Parameterized  Constructors
  2.  Multiple Constructor in class
  3. Default constructors
  4. Dynamic Initialization constructors
  5.  Copy Constructors
  6. Dynamic Construction
  7. Constructing Two dimension array.

 

Parameterized Constructors

The constructor integer(), defined above , initializes the data members of all the objects to zero. However, in practice it may be necessary to initialize
the various data elements of different objects with different values when they are created. C++ permits us to achieve this objective by passing arguments to the constructor function when the objects are created. The constructors that can take arguments are called Parameterized.

 

 #include <iostream>

 using namespace std;

 class integer
 {
       int  m,  n;
    public:
        integer(int, int);           // constructor declared
        
        void display(void)
        {

             cout << " m = " << m << "\n";
             cout << " n = " << n << "\n";
         }

 };

 integer :: integer(int x, int y)     // constructor defined
 {

        m = x;   n=y;
 }

 int main()
 {

      integer  int1(10,60);   
                            // constructor called implicitly

      integer int2 = integer(30, 90);
                            // constructor called explicitly

      cout << "\nOBJECT1 << "\n";
      int1.display();

      cout << "\nOBJECT2" << "\n";
      int2.display();

      return 0;
 
 }

Output of this program

OBJECT1
M=10
N=60
OBJECT2
M=30
N=90

 

Multiple Constructor in a class-:  We are already using two type constructor. It is called the overloading constructors. these are the

  1. integer();  // there are no arguments
  2. integer(int,int);   // there are two arguments

In the first case the constructor itself supplies the data value and no value are passed by the calling program . in the second case the function call passes the appropriate value from main() c++  permits us to use both these constructors in the same class.

overloading constructors program-:

 #include <iostream>

 using namespace std;

  class complex

{

    float x, y;

       public:

complex (){}        //constructors has no arguments

complex ( float a ) { x=y=a;}  //constructor has one arguments 

complex (float real, float good){ x= real; y=good;}

friend complex  sum (complex, complex);

friend void show(complex);

};

complex sum( complex c1, complex c2)  //friend

{
   complex c3;

   c3.x= c1.x +c2.x;
   c3.y= c1.y +c2.y;
   return(c3);

}
void show(complex c)     //friend

{

   cout << c.x << " +j" << c.y <<"\n";

}

int main()

{

 complex A(2.7, 3.5);    
 complex B(1.6);
 complex C;

 c= sum(A,B);
 cout << "A = "; 
 show (A);
 cout << "B = ";
 show (B);
 cout << "C = ";
 show (C);

return 0;
}


Output of this program .

A = 2.7 + j3.5
B = 1.6 + j1.6
C = 4.3 + j5.1

 

 

Read more