-
first C++ programm..
Hi i have just started with C++ and did this small app...just wanted u guys to take a look at it and give me some point .And one more thing instead always typing using std:: etc etc...isnt there another way ..thanks
Code:
#include <iostream>
#include <cstdlib>
#include <cstdio>
using std::cout;
using std::endl;
class Rectangle{
public:
Rectangle();
int perimeter();
int area();
// set functions
void setlenght(int);
void setwidth(int);
// get functions
int getlenght();
int getwidth();
//draw function
void drawRec();
private:
int lenght;
int width;
};
Rectangle::Rectangle(){ lenght = width = 1; }
void Rectangle::setlenght( int l ){ lenght = ( l > 0 ) ? l : 0; }
void Rectangle::setwidth( int w ){ width = ( w > 20 ) ? 20 : w; }
int Rectangle::getlenght(){ return lenght; }
int Rectangle::getwidth(){ return width; }
int Rectangle::area(){ return (lenght*width); }
int Rectangle::perimeter(){ return 0; }
void Rectangle::drawRec()
{
int array[25][25]={0} ;
array[0][0] = 201;
array[0][lenght-1] = 187;
for ( int cnt_main = 1; cnt_main <= lenght-2; cnt_main++ )
array[0][cnt_main] = 205;
array[width-3][0] = 200;
for ( int cnt_main = 1; cnt_main <= width-4; cnt_main++ )
array[cnt_main][0]= 186;
array[width-3][lenght-1] = 188;
for ( int cnt_main = 1; cnt_main <= lenght-2; cnt_main++ )
array[(width-3)][cnt_main] = 205;
for ( int cnt_main = 1; cnt_main <= width-4; cnt_main++ )
array[cnt_main][(lenght-1)] = 186;
cout << "\n\n";
for ( int cnt_main = 0; cnt_main <= width-1; cnt_main++, cout << "\n" ){
for ( int cnt_sub = 0; cnt_sub <= lenght-1; cnt_sub++ ){
if(array[cnt_main][cnt_sub] == 201)
putchar(201);
else if ( array[cnt_main][cnt_sub] == 187 )
putchar(187);
else if ( array[cnt_main][cnt_sub] == 205 )
putchar(205);
else if ( array[cnt_main][cnt_sub] == 200 )
putchar(200);
else if ( array[cnt_main][cnt_sub] == 186 )
putchar(186);
else if ( array[cnt_main][cnt_sub] == 188 )
putchar(188);
else
cout << " ";
}
}
}
int main(void)
{
Rectangle t;
int len, wid;
cout << "Enter you length and width: ";
cin >> len >> wid;
t.setlenght(len);
t.setwidth(wid);
cout << "\nThe Area of rect with lenght "<< t.getlenght() << " and width is "
<< t.getwidth() << " is " << t.area() << endl;
t.drawRec();
system("PAUSE");
return 0;
}
-
as for the std:: stuff, you could do, "using namespace std;", without the quotes
-
Hey thanks alot man .I needed that ..;)
-
Re: first C++ programm..
You spelt length wrong, but other than that you code looked good.