Home | | Computer Science 11th std | C++: Introduction to Constructors

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

C++: Introduction to Constructors

The definition of a class only creates a new user defined data type.

Introduction to Constructors

 

The definition of a class only creates a new user defined data type. The instances of the class type should be instantiated (created and initialized) . Instantiating object is done using constructor.

 

Need for Constructors

 

An array or a structure in c++ can be initialized during the time of their declaration.

For example

struct sum

{

      int n1,n2;

};

class add

{

      int num1,num2;

};

int main()

{

      int arr[]={1,2,3}; //declaration and initialization of array

      sum s1={1,1}; //declaration and initialization of structure object

      add a1={0,0}; // class object declaration and initialization throws compilation error

}

Member function of a class can accessall the members irrespective of their associated access specifier.

 

The initialization of class type object at the time of declaration similar to a structure or an array is not possible because the class members have their associated access specifiers (private or protected or public). Therefore Classes include special member functions called as constructors. The constructor function initializes the class object.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 14 : Classes and objects : C++: Introduction to Constructors |

Related Topics

11th Computer Science : Chapter 14 : Classes and objects


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

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