Home | | Computer Science 11th std | C++: Declaration and Definition of constructor

Example Programs in C++ - C++: Declaration and Definition of constructor | 11th Computer Science : Chapter 14 : Classes and objects

Chapter: 11th Computer Science : Chapter 14 : Classes and objects

C++: Declaration and Definition of constructor

When an instance of a class comes into scope, a special function called the constructor gets executed.

Declaration and Definition

 

When an instance of a class comes into scope, a special function called the constructor gets executed. The constructor function name has the same name as the class name. The constructors return nothing. They are not associated with any data type. It can be defined either inside class definition or outside the class definition.

 

Example 1:

Illustration 14.14 A constructor defined inside the class specification.

#include<iostream>

using namespace std;

class Sample

{

int i,j;

      public :

int k;

      Sample()

      {

      i=j=k=0;//constructor defined inside the class

      }

};

 

Illustration 14.15 A constructor defined inside the class specification.

#include<iostream>

using namespace std;

class Sample

{

int i,j;

      public :

int k;

      Sample()

      {

      i=j=k=0;//constructor defined inside the class

      }

};

int main()

{

Samples1;

return 0;

}


 

Example 2:

Illustration 14.16 A constructor defined outside the class specification.

#include<iostream>

using namespace std;

class Data

      {

int p,q;

      public :

int r;

      Data();   //only prototype to be specified here to intimate its access specifier

      };

Data ::Data()

{

      p=q=r=0;                      // constructor defined outside the class

}

int main()

{

      Data d1;

return 0;

}

 

Illustration 14.17 illustrate a constructor defined inside the private visibility.

#include<iostream>

using namespace std;

class X

{

      int num;

      X()

{

      num=k=0;

}

public:

int k;

};

int main()

{

X x;    // The constructor of X cannot accessed by main() because main() is a

      //non member function

      //and the compiler throws error message             [Error] 'X::X()' is private

return 0;

}

 

Functions of constructor

 

As we know now that the constructor is a special initialization member function of a class that is called automatically whenever an instance of a class is declared or created. The main function of the constructor is

1) To allocate memory space to the object and

2) To initialize the data member of the class object

There is an alternate way to initialize the class objects but in that case we have to explicitly call the member function.

 

Illustration 14.18 illustrate a member function initializes the data member.

After creating the object the getvalue() should be explicitly called to initialize the object.

#include<iostream>

using namespace std;

class Sample

{

      int i, j;

      public :

      int k;

      void getvalue()

      {

      i=j=k=0; //member function

      }

int main()

{

Sample s1;

      s1.getvalue();                        //member function initializes the class object

return 0;

}

 

Default Constructors

 

A constructor that accepts no parameter is called default constructor. For example in the class data program Data ::Data() is the default constructor . Using this constructor Objects are created similar to the way the variables of other data types are created.

Example

int num;      //ordinary variable declaration

Data d1;      // object declaration

If a class does not contain an explicit constructor (user defined constructor) the compiler automatically generate a default constructor implicitly as an inline public member.

 

Illustration 14.19 illustrate the compiler generated constructor

#include<iostream>

using namespace std;

class Sample

{

      int i, j;

      public:

      int k; //no user defined constructor in this program

      void getvalue()//member function

      {

           i=j=k=0;

      }

};

int main()

{

      Sample s1; //uses the default constructor generated by the compiler

      s1.getvalue();

      return 0;

}

 

Illustration 14.20 to illustrate the constructor and other member function in a class

#include<iostream>

using namespace std;

class simple

{

private:

      int a,b;

public:

simple()

{

      a= 0 ;

      b= 0;

      cout<< "\n Constructor of class-simple ";

}

      void getdata()

      {

      cout<<"\n Enter values for a and b (sample data 6 and 7)... ";

      cin>>a>>b;

      }

      void putdata()

      {

      cout<<"\nThe two integers are... "<<a<<'\t'<< b<<endl;

      cout<<"\n The sum of the variables "<<a<<" + "<<b<<" = "<<a+b;

      }

};

int main()

{

      simple s;

      s.getdata();

      s.putdata();

      return 0;

}

Output:

Constructor of class-simple

Enter values for a and b (sample data 6 and 7)... 6 7

The two integers are... 6         7

The sum of the variables 6 + 7 = 13

 

Parameterized Constructors

 

A constructor which can take arguments is called parameterized constructor .This type of constructor helps to create objects with different initial values. This is achieved by passing parameters to the function.

 

Illustration 14.21 to illustrate the Parameterized constructor used for creating objects

#include<iostream>

using namespace std;

class simple

{

private:

      int a,b;

public:

simple(int m, int n)

{

a= m ;

b= n;

cout<< "\n Parameterized Constructor of class-simple "<<endl;

}

void putdata()

{

cout<<"\nThe two integers are... "<<a<<'\t'<< b<<endl;

cout<<"\n The sum of the variables "<<a<<" + "<<b<<" = "<< a+b; }

};

int main()

{

simple s1(10,20),s2(30,45); //Created two objects with different values created

cout<<"\n\t\tObject 1\n";

s1.putdata();

cout<<"\n\t\tObject 2\n";

s2.putdata();

return 0;

}

Output:

Parameterized Constructor of class-simple

Parameterized Constructor of class-simple

Object 1

The two integers are .. 10      20

The sum of the variables 10 + 20 = 30

Object 2

The two integers are... 30      45

The sum of the variables 30 + 45 = 75

 

Illustration 14.22 to illustrate the creation of object with no argument after defining parameterized constructor throws error

Note:- Just like normal function parameterized constructor can also have default arguments.

#include<iostream>

using namespace std;

class simple

{

private:

      int a,b;

public:

simple(int m, int n)

{

a= m ;

b= n;

cout<< "\n Parameterized Constructor of class-simple "<<endl;

}

void putdata()

{

cout<<"\nThe two integers are .. "<<a<<'\t'<< b<<endl;

cout<<"\n The sum of the variables "<<a<<" + "<<b<<" = "<< a+b; }

};

int main()

{

simple s,s1(10,20)      // [Error] no matching function for call to 'simple::simple()'

s1.putdata();

s2.putdata();

return 0;

}

 

Illustration 14.23 to illustrate the default argument in parameterized constructor

#include<iostream>

using namespace std;

class simple

{

private:

int a,b;

public:

simple(int m, int n=100)   //default argument

{

a= m ;

b= n;

cout<< "\n Parameterized Constructor with default argument"<<endl;

}

void putdata()

{

cout<<"\nThe two integers are... "<<a<<'\t'<< b<<endl;

cout<<"\n The sum of the variables "<<a<<" + "<<b<<" = "<< a+b; }

};

int main()

{

simple s1(10,20),s2(50);

cout<<"\n\t\tObject 1 with both values \n";

s1.putdata();

cout<<"\n\t\tObject 2 with one value and one deafult value\n";

s2.putdata();

return 0;

}

Output:

Parameterized Constructor with default argument

Parameterized Constructor with default argument

      Object 1 with both values

The two integers are... 10        20

The sum of the variables 10    + 20 = 30

      Object 2 with one value and one deafult value

The two integers are... 50        100

The sum of the variables 50    + 100 = 150


Tags : Example Programs in C++ , 11th Computer Science : Chapter 14 : Classes and objects
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 14 : Classes and objects : C++: Declaration and Definition of constructor | Example Programs in C++

Related Topics

11th Computer Science : Chapter 14 : Classes and objects


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.