Home | | Computer Science 11th std | C++: Significance of Default constructors

Example Programs in C++ - C++: Significance of Default constructors | 11th Computer Science : Chapter 14 : Classes and objects

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

C++: Significance of Default constructors

Default constructors are very useful to crate objects without having specific initial value. It is also used to create array of objects.

Significance of Default constructors

 

Default constructors are very useful to crate objects without having specific initial value. It is also used to create array of objects.


Illustration 14.24 to illustrate the significance of default constructor

#include<iostream>

using namespace std;

class simple

{

private:

int a, b;

public:

simple()      //default constructor

{

a=0 ;

b= 0;

cout<< "\n default constructor"<<endl;

}

int getdata();

};

int simple :: getdata()

{

int tot;

cout<<"\nEnter two values ";

cin>>a>>b;

tot=a+b;

return tot;

}

int main()

{

int sum=0;

simple s1[3];

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

for (int i=0;i<3;i++)

sum+=s1[i].getdata();

cout<<"\nsum of all object values is"<<sum;

return 0;

}

Output:

default constructor

default constructor

default constructor

      Object 1 with both values

Enter two values 10 20

Enter two values 30 40

Enter two values 50 50

sum of all object values is200

 

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++: Significance of Default constructors | 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-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.