I have a doubt regarding constructor in this program--
include<iostream.h>
class A
{
private:
int a;
float b;
A(int j)
{
a=j;
cout<<"\n a is"<<a;
}
public:
A()
{
cout<<"\n Default constructor invoked\n";
a++;
b=0;
}
A(int j,float i)
{
a=j;
b=i;
cout<<"\n value"<<a<<"and"<<b<<endl;
}
public:
void show()
{
int j;
cout<<"\n Enter value of j";
cin>>j;
A(j);
cout<<"\n new values are"<<a<<"and"<<b;
}
};
void main()
{
A a1;
A a3(8,4.01);
a1.show();
a3.show();
}
I tried this program in Visual c++ and turbo c++.This program compiled well in the turbo c++ while in visual it's giving an error " 'j' : redefinition; different basic types" for this --
void show()
{
int j;
cout<<"\n Enter value of j";
cin>>j;
A(j);
cout<<"\n values are"<<a<<"and"<<b;
}
but if instead of calling this A(j) constructor..I do this
void show()
{
int j;
float i;
cout<<"\n Enter value of j";
cout<<"\n Enter value of i";
cin>>i;
cin>>j;
A(j,i);
cout<<"\n values are"<<a<<"and"<<b;
}
the program compiles well..
Why does it do this way in visual c++? :?:
My second doubt is that when this program is executed , the values after calling the constructor A(j) do not change.. :confused: Is it because that the constructors are just used to initialise the objects and their second call is not taken into consideration or there is some other reason for it?? :?:
Thanks!