float square(float x)
{
return x * x;
}
#include <iostream.h> // allows use of >> and << for I/O
int main() // int is the exit status for main
{
cout << "Hello Everyone!\n";
return 0; // 0 means program terminated ok
}
double average( double x, double y ); double toFarenheight( double centegradeTemp ); double toCentegrade( double farenheightTemp );
double average( double x, double y )
{
return ( x + y ) / 2.0;
}
double toFarenheight( double centegradeTemp )
{
return 9.0 * centegradeTemp / 5.0 + 32.0;
}
double toCentegrade( double farenheightTemp )
{
return 5.0 * ( farenheightTemp - 32.0 ) / 9.0;
}
int main()
{
double x = 10.5;
double y = 32.6;
double z = average( x, y );
double centegradeTemp = 22.3;
double farenheightTemp = toFarenheight( centegradeTemp );
cout << toFarenheight( centegradeTemp + 2.0 ) << endl;
cout << average( toFarenheight( 29.7 ),
toFarenheight( centegradeTemp ) );
return 0;
}
double average( double x, double y )
{
return ( x + y ) / 2.0;
}
double x = 5.0; double z = average( 1.0, 2.0*x );
double toCentegrade( double farenheightTemp = 32.0 )
{
return 5.0 * ( farenheightTemp - 32.0 ) / 9.0;
}
double t = toCentegrade(); // gives the Centegrade for 32.0 cout << toCentegrade( 6.0 ); // overrides the default with 6.0
float square(float x)
{
float result = x * x;
cout << "Hello there!\n";
return result;
cout << "Hello there again!\n";
}
int main()
{
cout << square( 2.0 );
cout << square( square( 2.0 ) );
return 0;
}
0 1 -1 -1234567 11 // decimal 11 011 // octal 9 0x11 // hex 17
1.0 -3.000001e-10 30.01E40
0 to 255
'\0' null character 0 '\n' newline (or linefeed) 10 '\r' return 13 '\t' tab 9 ' ' space 32 '0' 48 'A' 65 'a' 97
char *s = "Hello";
char *t = "Hello world!"; cout << "This is another character string.\n"; cout << t << endl; // prints: Hello World!
int numberOfStudentsInECE10 = 160; int automobileVelocity = 0;
numberOfStudentsInECE10 = 175; // added a few students automobileVelocity += 20; // accelerated the auto
const double PI = 3.1415926536; const char newline = '\n'; const int roomCapacity = 250;
PI = 3.0; // compiler will give a warning message
return 3.14159*r*r; return PI*r*r;
automobileVelocity = ( acceleration * time * time ) / 2.0;
#include <iostream.h>
int main()
{
double i = 2 * PI;
cout << i << endl;
return 0;
}
int main()
{
const double PI = 3.14159;
double i = 2.0 * PI;
cout << i << endl;
i = i / 2.0;
square( 2.0 ); // be careful of this mistake
cout << i << endl;
return 0;
}
int f( int a, int j ) // a and j are now in scope
{
int i = 10; // i is now in scope
{
int j = i; // new j is now in scope and hides parameter j
int i = 30; // this new i is in scope and hides outer i
cout << i * j << endl; // refers to inner i and j
} // inner j and i are now out of scope
cout << i << endl;
return a + j + i; // refers to the parameters and outer i
} // a, j, i are now out of scope
int main()
{
int a = 10; // a is in scope
int i = 20; // i is in scope
cout << f( i, a ); // calls f with actual values 20 and 10
return 0;
} // a and i are now out of scope
void increment(int & x)
{
x = x + 1;
}
int main()
{
int z = 10;
increment(z);
increment(z);
cout << z;
return 0;
}
int main()
{
cout << "Hello";
cout << 10 * 10;
cout << 'A';
cout << 3.14159;
...
}
int main()
{
int i;
float f;
char c;
cin >> i; // reads a string of digits as an integer
cin >> f; // reads a string of digits, decimal as a real number
cin >> c; // reads a single character
...
}
#include <iostream.h>
int main()
{
int i = 40;
cout << "I is " << i << endl;
return 0;
}