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.
#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;
}
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
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.