+ 2
What is the error in this .... Related to constructor overloading plz help and solve my query
#include <iostream> using namespace std; class rel { float c; public: rel(int a) { cout<<"a is"<<a; } rel(int a,int b) { c=a+b; } rel(float a,int b) { c=a+b; } rel(float a,float b) { c=a+b; } void display() { cout<<"sum is"<<c<<endl; } }; int main() { rel obj(3); rel obj2(5,6); rel obj4(5.6,8); rel obj3(6.8,7.3); obj.display(); obj2.display(); obj3.display(); obj4.display();
10 Antworten
+ 1
ohh.. so in constructor overloading we must use f with float values..and in double ! did we use d also with double values?
0
@immortal but when we use this using only single float, then it is taking it float without uski f
0
#include <iostream>
using namespace std;
class rel
{
float c;
public:
/* rel(int a)
{
cout<<"a is"<<a;
}
rel(int a,int b)
{
c=a+b;
}*/
rel(float a,int b)
{
c=a+b;
}
/* rel(float a,float b)
{
c=a+b;
}*/
void display()
{
cout<<"sum is"<<c<<endl;
}
};
int main() {
/*rel obj(3);
rel obj2(5,6);
rel obj4(5.6,8);*/
rel obj3(6.8,7);
obj3.display();
/*obj2.display();
obj3.display();
obj4.display();*/
return 0;
}
this is working
sorry not justyfied with your last reply
0
when it taking a decimal value as float nd when double?
0
Here is ur complete program
#include <iostream>
using namespace std;
class rel
{
float c;
public:
rel(int a)
{
cout<<"a is"<<a<<endl;
}
rel(int a,int b)
{
c=a+b;
}
rel(double a,int b)
{
c=a+b;
}
rel(double a,double b)
{
c=a+b;
}
void display()
{
cout<<"sum is"<<c<<endl;
}
};
int main() {
rel obj(3);
rel obj2(5,6);
rel obj3(5.6f,8);
rel obj4(6.8f,7.3f);
obj2.display();
obj3.display();
obj4.display();
return 0;
}
0
thanxx @kishan Gupta and@immortal