You are on page 1of 8

Introduction to C+(General info, you are not responsible for this) Subset C++ (C+-) we will study this

year include the simpler features of C and C++ languages. It is similar to the F language. C++ is an object oriented language (OOL), it is the OO extension of the C language. In C you can define your own variable types. A student type can have an alphabetic string name, an integer reg_no and a floating number gpa. Such derived types are knowns as structs in C and types in fortran90 and F. C++ brings the extra capability of defining functions within the type. These functions are then applicable only for objects of that particular type. A type with its own functions is called a class, the functions defined within a class are known as methods of that class, entities of this type are known as objects and languages that are based on objects and classes are known as object oriented. There are standart libraries and built in classes; complex is one of these. Example: complex class in C++. complex <float> x, y, z z= x*y the first line defines the variables x, y and z as complex entities and their real and imaginary parts are floating point numbers. The compiler then knows what kind of multiplication z= (Re(x)*Re(y) Im(x)*Im(y)) + i*(Re(x)*Im(y)+Im(x)*Re(y)) is applicable for complex numbers and applies that multiplication. We say that the method is overloaded on the * operator. The real and imag are methods of complex class which return the real and imaginary parts of a complex number. A= z.real(); B= z.imag(); Note that z. means the function applicable for objects of class of z, z is already passed as argument to function real, (it is the default argument) so it is not necessary to list it again as an argument. We will see a similar usage in file handling for .open() and .close() methods. Although you can define your own classes in C++, we will not get into that here. SymNum C+includes only four predefined classes: <iostream>, <fstream>, <complex> and <valarray>. The first two allow unformatted input /output (i/o) and simplified file handling. The last two give functionality similar to the complex data type and arrays in the F language. Fortran90, 95 and F arrays and complex type give these languages limited OOL like features. But they are not OOL. Fortran 2003 and 2008 are fully OOL. Fortran08 also has intrinsic paralellism.

Program name Program ending Comment

Line ending Continuation Default library Types

constant

C+- and F F C++ any_name int main() { end program any_name } ! // /* This form of comment is also accepted but it has undesirable properties and should be avoided */ // C++ returns 0 to the operating system. // Therefore it is of type int. automatic end of line denoted by ; & automatic fairly rich Very basic. Load only the needed features. character char integer int real float real(kind=8) double complex --parameter const integer, parameter ::n=5 const int n=5; // note word order

program hello

Hello World in C+- and F C+#include <iostream> // c++ style i/o using namespace std; // c++ keywords int main() {

print *, Hello World! end program hello

cout << Hello World! \n // \n : newline } #include <iostream> using namespace std; int main() { char nam[10]; cout << Type your name\n; cin >> nam >> endl; // endl == \n cout << hello << nam << endl; } If structure C+int i cout << enter an integer \n; cin >> i; if (i>0) { cout << i << is positive; } else if (i<0) { cout << i << is negative; } else { cout << i << is zero; }

program hello2 character(len=10) :: nam print *, Type your name read *, nam print *, Hello , nam end program hello2

F integer :: i print *, enter an integer read *, i if (i>0) then print *, i, is positive else if (i<0) then print *, i, is negative else print *, i, is zero end if

note that every structure in C is defined by curlies {}.

F do i= 1, 10 x(i)= i ! i & x = 1-10 end do

Do / for loops C+for (i=0; i<10;i++) { x[i]= i+1; }

// i= 0-9, x=1-10

do i= 1, 100 if (x(i) >= 0) then y(i)= sqrt(x(i)) else print *, negative x exit end if end do

for (i=0; i<100;i++) { if (x[i] >=0) { y[i]= x[i]; } else { cout << negative x\n; break; } }

F exit loop stop program

C+break out of loop exit the program

Functions and void functions (subroutines) Functions are used to return ONE variable. C & C++ Void functions and Fortran subroutines are used to return more than one variable or no variable at all. Function calling syntax is the same in both languages. F C+y= myfunc(x) ! y= myfunc(x) // functions are invoked the same y= myfunc(x, a, b) ! y= myfunc(x, a, b) // way in both F and C/C++ function implementations are somewhat different function myfunc(xdum, adum) & float function myfunc(const float xdum, const int result (ydum) adum){ integer, intent(in) :: adum // declarations and typing of dummy real, intent(in) :: xdum // arguments in title line real :: ydum // const makes them intent(in) ydum= xdum**adum return pow(xdum, adum); end function myfunc } call mysub(x, y, z) myfunc(x, y, z) C makes a copy of a variable and sends it to a function. If it is changed, the new value is not copied back to the original. The changes are lost on exit from the function. Fortran does not act this way. Functions and subroutines work directly on the variables they are called with. This code fragment illustrates the difference F C+function double(x) result (x2) float function double(float x){ real, intent(inout) :: x // arguments in title line real :: x2 float x2; // local variable x= 2*x x= 2*x; print *,x in function , x cout <<x in function << x<<endl; x2= x return x; end function myfunc } in main program: x=5 x2=double(x) print *, ,x in main , x, x2in main , x2

x=5; x2=double(x); cout <<x in main<< x<< x2in main<< x2<<endl

In both versions x in the function is 10. Both return this value as x2. However in Fortran the x in main is also changed. In C it IS NOT. The reason is C changed the copy, not the original and returned it as x2. (Note: this IS NOT how functions are supposed to be used. F does not allow intent(inout) in functions to prevent this abuse. Variables declared as intent(in) can NOT be changed within the routine. In C the same effect can be obtained by declaring them as const in the function title line.) To return more than one variable or no variable at all we use subroutines in fortran and void functions in C++. These actually do not return anything at all. However it is possible to make them behave as if they return several variables. Fortran functions and subroutines, as stated above, work on the original copies. So even when they do not return anything, the updated variables are changed in the calling routine as well. In c++ the same effect can be had by declaring variables with an &. The & variables are not copied to the function and it works on the original like fortran. The & is not used in the calling line but only in function definition. In technical terms fortran variables and C++ & variables are not copied to the function but are pointed to. (Note: Do not put an & if you pass an array as an argument to a function! It is already pointed to!) Subroutine and void function calling syntax in the two languages. F C+call mysub(x, y, z) myfunc(x, y, z) subroutine mysum(xdum, ydum, zdum) real, intent(in) :: xdum real, intent(out) :: ydum, zdum ydum= xdum**2 zdum= xdum**3 end subroutine mysum i) ii) iii) void function myfunc(const float xdum, float &ydum, float &zdum){

ydum= pow(xdum, 2); zdum= pow(ydum, 2); }

y and z in the callling routine are updated. If you try changing xdum compiler will not let you. The calling function names DO NOT HAVE TO MATCH function names. Myfunc can be called as myfunc(x,y,z), myfunc(a,b,c) myfunc(ali, veli, hasan) and myfunc(ayse, nese, gulse).

File i/o The file opening, closing, reading and writing commands are similar in F and C++. In fortran the file is assigned a device number and read and write operations are performed using this number. In c++ a stream name is used. Some c++ commands are OO methods and called that way (See examples). In c++ #include <fstream> Must be used to allow c++ style (that is simpler) file i/o. File opening: For input F

C+-

You might also like