CookieMook: close... If you don't provide the -o argument, it'll simply create the binary executable named "a.out".
Code:
g++ -o myprogram myprogram.cpp
What's even better, learn makefiles. They'll take care of compilation easier than manually compiling.
novicew: why do you have the last two lines uncommented. delete those or comment those out. like:
Code:
// rect area: 12
// rectb area: 30
and just some tip for you with your area() method, you might want to make it const like:
Code:
int area() const; // in the class scope
and move the actual implementation as:
Code:
int CRectangle::area() const { return width * height; }
this way you can guarantee the area() method will NOT mutate the width or height variables.
Though I'd suggest you move the CRectangle:: stuff to a .cpp file and the class declaration in a header file. Xcode will automatically compile and link them all together.
A final suggestion, use 'unsigned int' because neither width nor height can be negative. if you explictly try to add a negative integer the compiler will complain, however if you simply pass from input, it won't check, so you'll have to do the checking code if you use the user's input. So:
Code:
#include <iostream>
using namespace std;
class CRectangle {
unsigned int width, height;
public:
CRectangle( unsigned int w, unsigned h );
unsigned int area() const;
};
CRectangle::CRectangle( unsigned int w, unsigned int h ) {
width = w;
height = h;
}
unsigned int CRectangle::area() const {
return width * height;
}
int main( int argc, char* argv[] ) {
CRectangle rectA( 3, 4 );
CRectangle rectB( 5, 6 );
cout << "rectA area: " << rectA.area() << endl;
cout << "rectB area: " << rectB.area() << endl;
return 0;
}
// rectA area: 12
// rectB area: 30
compiles great with g++ -o foo foo.cpp