I have been studying classes from a few weeks and now I want to apply the theory of what I studied so far.I made this program .CAn someone tell me what is wrong with it and how can I make it workable.
#include<iostream>
usingnamespace std;
class Reverse
{
int c;
int b;
int result=0,k;
int a=0,i;
public :
int reverse(unsignedint b);
}
int Reverse::reverse(unsignedint b)
{
b=a;
for(;c/10!=0;c/=10)
a++;
for(;b/10!=0;b/=10,a--)
{
k=b%10;
for(i=0;i<a;i++)
{
k*=10;
}
result+=k;
}
result = result+b%10;
return result;
}
int main()
{
int input;
char ch;
do
{
Reverse funct;
cout<<"enter numner";
cin>>input;
cout<<"the reverse is "<<funct.reverse(input);
cout<<endl;
cout<<"enter c to continue and q to quit the program";
cin>>ch;
} while (ch=='c');
return 0;
}
I recently ran into some code that had this problem as well. You can declare members that are made when you have a class and then you can assign member values when you construct the class itself. This is very easily done through Initializer Lists.
1 2 3 4 5 6 7 8 9 10
class Reverse
{
int c;
int b;
int result, k;
int a ,i;
public :
Reverse(unsignedint) : result(0), a(0) {} //NOTICE: int reverse isn't valid for a constructor
}
Also notice that I change the function int reverse. The reason being is because this is your constructor which 1) Does not return a value 2) Is required.
@ kevinchkin and compterquip !!!
So you mean I should just declare the variables in private section of the class not initialize it .
what else should I do ? Tactful criticism is very welcome.
#include<iostream>
usingnamespace std;
class Reverse
{
int c;
int b;
int result, k;
int a ,i;
public :
Reverse(unsignedint) : result(0), a(0);
}
int Reverse::reverse(unsignedint b)
{
b=a;
for(;c/10!=0;c/=10)
a++;
for(;b/10!=0;b/=10,a--)
{
k=b%10;
for(i=0;i<a;i++)
{
k*=10;
}
result+=k;
}
result = result+b%10;
return result;
}
int main()
{
int input;
char ch;
do
{
Reverse funct;
cout<<"enter numner";
cin>>input;
cout<<"the reverse is "<<funct.reverse(input);
cout<<endl;
cout<<"enter c to continue and q to quit the program";
cin>>ch;
} while (ch=='c');
return 0;
}
Yes if you want to initialize a data member then use initializer list as mentioned by computerquip.
Right now there are few problems in your code.
Invalid constructor int Reverse(unsignedint b); // This is constructor and constructor does not have a return type.
No semicolon after class declaration.
And initializer list can be used while providing definition of constructor and not while declaring it. Unless ofcourse, you are defining it at the same place you declared it.
When line 45 will be executed it will throw an error because you have not declared a default constructor. There is only one constructor in class right now which is Reverse (unsignedint); . So technically line 45 should be Reverse funct(1); // since parameterized constructor
However, if you want to declare new object like you did right now then add the default constructor too i.e. constructor without any parameters Reverse() : result(0), a(0){}
Other than that, I don't know how you want to initialize 'result' and 'a'. Right now I don't see any use of the parameterized constructor in your code, well, unless you want to initialize 'result' and 'a' with the same value like this Reverse (unsignedint v) : result(v), a(v) { } This will assign result and a both to v, (this v you will supply while creating the object)
Or other use could be Reverse (unsignedint a, unsignedint b) : result(a), a(b) { }
This will initialize result to a and a to b.
#include<iostream>
usingnamespace std;
class Reverse
{
int c;
int b;
int result, k;
int a ,i;
public :
reverse(){};
Reverse(unsignedint) : result(0), a(0)
}
Reverse::reverse(unsignedint b)
{
b=a;
for(;c/10!=0;c/=10)
a++;
for(;b/10!=0;b/=10,a--)
{
k=b%10;
for(i=0;i<a;i++)
{
k*=10;
}
result+=k;
}
result = result+b%10;
}
int main()
{
int input;
char ch;
do
{
Reverse funct;
cout<<"enter numner";
cin>>input;
cout<<"the reverse is "<<funct.reverse(input);
cout<<endl;
cout<<"enter c to continue and q to quit the program";
cin>>ch;
} while (ch=='c');
return 0;
}
I don't know what to tell you kevin - I think you said it pretty clear. His emailed me about this program X amount of times. I've given him the solution and he still has no idea about it. So I'd gather he is still unsure of classes.
You should go read up about them on the tutorials on this site or cprogramming.com.
#include<iostream>
usingnamespace std;
class Reverse
{
int c;
int b;
int result /* =0 */,k; // result=0 can not asign values to non static
int a /* =0 */,i; // members in the declaration
public :
int reverse(unsignedint b);
}; // was missing an ;
int Reverse::reverse(unsignedint b) //this function just does not work...
{
b=a; // what b is this? the member var or the function input?
// a is never asigned a value
for(; c/10 != 0; c/= 10) // c is not asigned a value
a++;
for(; b/10!=0; b/=10, a--) // what b is this?
{
k=b%10;
for(i=0; i<a; i++)
{
k*=10;
}
result+=k;
}
result = result + b % 10;
return result;
}
int main()
{
int input;
char ch;
do
{
Reverse funct;
cout<<"enter numner";
cin>>input;
cout << "the reverse is " << funct.reverse(input) << endl;
cout<<"enter c to continue and q to quit the program";
cin>>ch;
} while (ch=='c');
return 0;
}
Here is some code to do the sort of thing you are attempting, hope you find it useful:
Thank you writetonsharma ! for what you said about me .
Can you give any comments on my original program ?
@grey wolf !
you made my program a little weired .I dont think you need two classes for this program and your output is very very strange.
@ kevinchkin !! thanks for your comments,
I tried exactly what you told me to do.I dont know how to do some of the things that you wrote in the comments.
#include<iostream>
usingnamespace std;
reverse(unsignedint b)
{
//Code for reversing.
}
int main()
{
int input;
char ch;
do
{
cout << "Please enter a number.";
cin >> input;
cout << "The reverse of " << input << " is " << reverse(input);
cout << endl;
cout << "Enter c to continue: ";
cin >> ch;
}
while (ch=='c');
return 0;
}
C-style but seriously, you don't needed classes for everything.
no but the OP has"...been studying classes from a few weeks and now I want to apply the theory", so giving a non class based example is pointless. Admittedly the example is not best suited to a class, but such is life...
Well this is more of a function of a class or just a global function. Try creating something more object like. A number wrapper or a string wrapper or something similar. What I'm trying to explain is that basing an ENTIRE CLASS on one function isn't something that should be done. All of the members of the class used above should actually be declared inside of the Reverse function itself since they aren't needed to be kept except maybe result. But then again result can be the return value of a global function. It's not class specific so in the end you'll just be left with a constructor.
#include<iostream>
usingnamespace std;
class UserNumber //Use one class for simplicity.
{
private:
unsignedint m_Number;
public:
staticint reverse(unsignedint);
UserNumber(unsignedint number = 0);
inlineunsignedint number() const { return m_Number; }
inlineunsignedint &number() { return m_Number; }
int reverse();
};
int UserNumber::reverse(unsignedint input)
{
int digitCount = 1;
int remainder = 0;
int result = 0;
for (unsignedint c = input; c/10 != 0; c/= 10)
{
++digitCount;
}
for (int i = 0; i < digitCount; ++i)
{
result *= 10;
remainder = input % 10;
result += remainder;
input /= 10;
}
return result;
}
UserNumber::UserNumber(unsignedint number)
: m_Number(number)
{
}
int main() // make the program easy to understand.
{
int input;
char ch;
do
{
cout << "Please enter a numner: ";
cin >> input;
cout << "The reverse is " << UserNumber::reverse(input) << endl; //Don't confuse the user.
cout << "enter c to continue and q to quit the program ";
cin >> ch;
} while(ch == 'c');
return 0;
}