Showing posts with label explicit. Show all posts
Showing posts with label explicit. Show all posts

20121002

Why to use explicit keyword in C++

//explicit keyword is used only with constructor
// which  avoids conversion only it will allow to create objects
 #include<iostream.h>
class sample
{
private:
int i;
public:
sample(){}
// explicit sample(int ii)
 sample(int ii)
{
i=ii;
}

sample operator+(sample s)
{
sample temp;
temp.i=i+s.i;
return temp ;
}
void display()
{
cout<<i<<endl;
}
};
int main()
{
sample s1(25), s2;
s1.display();
s2=s1+25;
s2.display();

return 0;
}
//How to use explicit keyword in C++ with exaple
//How to use explicit keyword in C++  with exaple
//How to use explicit keyword in C++  with exaple
//Why to use explicit keyword in C++  with exaple
//Why to use explicit keyword in C++  with exaple

//overloading S2=S1+25;


Previous                             Home                               Next