-1

I want to write an object oriented program. I do not know much about it. It looks for me that it is somewhat similar to subroutine in Fortran. I have created a sample program below. May you help me out translating it into C++ codes please? c Program in Fortran to calculate area and perimeter of a rectangle

implicit double precision(a-h,o-z), integer(i-n)
dimension a(10),p(10)
xl = 0.0
xb = 0.0
do 10 ix = 1,10
   call area(xl,xb,a)
   call perimeter(xl,xb,p)
   write(*,*) ix,a(ix),p(ix)
   xl = xl + 1.0
   xb = xb + 1.0
  10    continue
end

subroutine area(xx,yy,ara)
implicit double precision (a-h,o-z),integer(i-n)
dimension ara(10)
do 40 j = 1,10
     ara(j) = xl*xb
    40     continue
return
end

subroutine perimeter(xl,xb,per)
implicit double precision (a-h,o-z),integer(i-n)
dimension per(10)
do 50 i=1,10
   per(i) = 2*(xl+xb)
    50  continue
return
end

Thank you.

Yann Ramin
  • 32,895
  • 3
  • 59
  • 82
nagendra
  • 245
  • 6
  • 13
  • 4
    Voted to close, not a real question. Please make an attempt at the C++ code, or if you've run into some problem in some C++, please post that. – user7116 Apr 07 '11 at 17:21
  • it is not (somewhat similar to subroutine). How do you expect to "write an object-oriented program" when you don't have a clue what it is? There are lots of intros to get started – davka Apr 07 '11 at 20:17
  • Dear Davka, thank you, I got the point. May you suggest the good source that I can go over to learn it well please? – nagendra Apr 08 '11 at 13:28

1 Answers1

1

I'm not into Fortran, but this looks like what you are getting at (If it's not what you wanted, please clarify):

#include<vector> // allows you to use the C++ STL std::vector (think of it as a better array)

// calculates the area of a rectangle
double rectangle_area( const double height, const double width )
{
    return height*width;
}
// calculates the perimeter of a rectangle
double rectangle_perimeter( const double height, const double width )
{
    return 2*height+2*width;

int main()
{
    const int N = 10; // defined a constant integer
    std::vector<double> area( N ); // creates a vector of double precision floats of size N
    std::vector<double> perimeter( N ); // idem

    double width = 0.;
    double height = 0.;

    for( int i = 0; i < 10; ++i ) // loop over i from 1 to 10, incrementing (++i) after each iteration
    {
        area[i] = rectangle_area( width, height );
        perimeter[i] = rectangle_perimeter( with, height );
        width += 1.; // same thing as 'width = width + 1' or width++;
        height += 1.; // idem
    }        

    return 0;
}

Note that this does not output anything to the screen. To output a number or string in C++, you'll need to #include <iostream> and use the following syntax:

std::cout << variable_you_want_to_output << std::endl;

The std::endl inserts a newline and flushes the output stream.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • It is perfect. This is what I was looking for. Thank you. May you tell me the good reference for this please? – nagendra Apr 08 '11 at 13:41
  • Dear Rubenvb, I got the point from you. May you tell my how will the codes look like if I use 'using namespace std' please? And what is the difference between the two cases? Thank you. – nagendra Apr 08 '11 at 14:13
  • If you do `using namespace std;` you can leave out every occurrence of `std::`. Nothing better as a reference as a book. Take your pick: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – rubenvb Apr 08 '11 at 16:50