Computer Hope
Software => Computer programming => Topic started by: timtim41 on February 11, 2010, 12:49:47 PM
-
Hey guys, I have been trying to learn how to do OOP by using classes in C++, and I made a program that compiles, but the output is "256" and i don't know why and I was just wondering what I did wrong. All comments are appreciated. Thanks :)
#include <iostream>
using namespace std;
class CRectangle
{
int x, y;
public:
-
Sorry, the program is:
#include <iostream>
using namespace std;
class CRectangle
{
int x, y;
public:
void values(int, int);
int area() {return (x*y);}
void CRectangle::shapeformat() {
x = 3;
y = 4;
}
};
int main()
{
CRectangle rect_shape;
cout<<endl<<rect_shape.area();
cout<<endl;
return 0;
}
-
I don't know c++
But I was wondering,
Is it normal to have..
{{{
and
}}}}
Why aren't they paired?
-
{{{, }}}}? Where do you see that at, cuz I wouldn't consider that normal in C++ unless if you have parameters within parameters, you know what i mean. but i dont see that anywhere in the source code.
-
Is the output an error?
I found this:
256 error? (http://www.cplusplus.com/forum/general/13692/)
What is 'shapeformat'?
-
Hello,
The C++ Compiler built into my brain says that it might be a case of uninitialized variables.
#include <iostream>
using namespace std;
class CRectangle
{
int x, y;
public:
void values(int, int);
int area() {return (x*y);}
void CRectangle::shapeformat() {
x = 3;
y = 4;
}
};
int main()
{
CRectangle rect_shape;
cout<<endl<<rect_shape.area();
cout<<endl;
return 0;
}
In Line No. 10, qualifier `CRectangle::` for the function `shapeformat` isn't necessary. Delete it.
Make a call to `shapeformat` from main().
Make it look something like:
#include <iostream>
using namespace std;
class CRectangle
{
int x, y;
public:
void values(int, int);
int area()
{
return (x*y);
}
void shapeformat()
{
x = 3;
y = 4;
}
};
int main()
{
CRectangle rect_shape;
rect_shape.shapeformat();
cout<<endl<<rect_shape.area();
cout<<endl;
return 0;
}
NOTE: I have modified the code and did some cleanup to avoid confusions. Try to use proper and uniform indentation, alignment and linespacing so that the code is more `readable`.
Secondly, you've called `endl` before and after call to the class member `area()`. The former causes an empty line which may not be desired. It could be something like:
cout <<rect_shape.area() << endl;
-
Geek-9pm,
Maybe you missed this one on Line No. 8:
int area() {return (x*y);}
-
Ok, thanks for the help and everything, but it still prints out "256." What I wanted it to do is to print out the product of 3 and 4 which is '12'. I know other ways to do it, but I want to know why this code does what it does for educational purposes.
-
Hello,
prints out "256."
No chance! Did you call `shapeformat`from `main()`? What compiler are you using? Try a Clean Build. Here on my Linux with G++ (The GNU C++ Compiler):
$ g++ -o newfile newfile.cpp
$ ./newfile
12
$
I saved your code as `newfile.cpp`(after making the modifications shown in my Relply No. 5) and then compiled it to `newfile`.
-
TheUnixGuy's revised program works perfectly fine for me as well with Visual Studio 2008.
-
Hello,
The C++ Compiler built into my brain says that it might be a case of uninitialized variables.
#include <iostream>
using namespace std;
class CRectangle
{
int x, y;
public:
void values(int, int);
int area() {return (x*y);}
void CRectangle::shapeformat() {
x = 3;
y = 4;
}
};
int main()
{
CRectangle rect_shape;
cout<<endl<<rect_shape.area();
cout<<endl;
return 0;
}
In Line No. 10, qualifier `CRectangle::` for the function `shapeformat` isn't necessary. Delete it.
Make a call to `shapeformat` from main().
Make it look something like:
#include <iostream>
using namespace std;
class CRectangle
{
int x, y;
public:
void values(int, int);
int area()
{
return (x*y);
}
void shapeformat()
{
x = 3;
y = 4;
}
};
int main()
{
CRectangle rect_shape;
rect_shape.shapeformat();
cout<<endl<<rect_shape.area();
cout<<endl;
return 0;
}
NOTE: I have modified the code and did some cleanup to avoid confusions. Try to use proper and uniform indentation, alignment and linespacing so that the code is more `readable`.
Secondly, you've called `endl` before and after call to the class member `area()`. The former causes an empty line which may not be desired. It could be something like:
cout <<rect_shape.area() << endl;
If you don't mind me asking.. How do you run this code? I've got MS Visual C++ 2008 Express ED.
I've only practiced running simple Win32 Console programs..
-
Hello there,
I've never used that IDE but try copying that into the textbox where you type Win32 programs and try compiling it. It should work. BC_Programmer uses that IDE though, maybe he can help.
-
ahh, I see..
If I use "start without debugging" it displays "12...press any key to continue"
-
lollll.. I always use debugging and I have that IDE.
I will have a look at it.
Usually main() will tell VS compiler to run it.
Edit: EEVIAC what the lol?
It outputs 12 for me too. =)
Isn't that normal?..
Since 3*4 is 12.. lol...
-
I just replaced the entire "stub" program it created with TheUnixGuy's code. ran fine- but the command window dissapeared instantly, so I cheated and added a call to gets() at the end so I could see the result. There's an option somewhere in the project or debugging options to keep it open- at least, I think there is.