C++
1.Definitions:
function takes two intiger arguments and returns an integer:
int func1(int,int);
If "a" is another intiger, the function above can be used this way:
a=func1(2,3);
compiler will check the use of func1() to make sure that a can accept the return
value.
The compiler ignores the names:
int func1(int lenght, int width);
A function with no arguments:
int func2( );
A body is a collection of statements enclosed in braces. Braces denote the
beginning and ending of a block of code. Function definition with an empty body:
int func1(int length, int width) {}
The braces replace the semicolon.
Variable a is an integer:
int a;
Declaring a variable without defining it means:
extern int a;
Extern can also apply to function declarations:
extern int func1(int length, int width);
Examples of declarations:
//: C03:Declare.cpp
// Declaration & definition examples
extern int i; // Declaration without definition
extern float f(float); // Function declaration
float b; // Declaration & definition
float f(float a) { // Definition
return a + 1.0;
}
int i; // Definition
int h(int x) { // Declaration & definition
return x + 1;
}
int main() {
b = 1.0;
i = 2;
f(b);
h(i);
} ///:~
Many libraries contain significant numbers of functions and variables. To save
work and ensure consistency when making the external declarations for these
items, C++ uses a device called the header file. A header file is a file
containing the external declarations for a library. To include a header file:
#include <header>
The linker collects object modules (.o or .obj), generated by the compiler, into
an executable program the operating system can load and run. It is the last
phase of the compilation process.
To use a library:
1.Include the library's header file
2. Use the functions and variables in the library
3. Link the library into the executable program
2.Program
To declare the functions and external data in the iostrams class:
#include <iostream>
The iostream package defines a variable(an object) called cout that accepts all
data bound for standard output. To send data to standard output, you use the
operator <<:
cout<<"howdy";
If you want to expose all the elements from the namespace called "std":
using namespace std;
After this, you don't have to worry that your particular library component is
inside a namespace, since teh using directive makes that namespace available
throughout the file where the using directive was written.
#include <iostream.h>
means:
#include <iosream>
using namespace std;
When the program starts, it calls a special function "main()". main() always has
return type of int.
Program1:
//Saying Hello
#include <iostream.h> //Stream declarations
int main(){
cout<< "Hello, I am " << 8 << "Today!" << endl;
}
The special iostream function endl outputs the line and a newline.
\n means a newline, \t means tab,\\ backslash,\b backspace.
Program2:
#include <iostream.h>
int main() {
// Specifying formats with manipulators:
cout << "a number in decimal: "
<< dec << 15 << endl;
cout << "in octal: " << oct << 15 << endl;
cout << "in hex: " << hex << 15 << endl;
cout << "a floating-point number: "
<< 3.14159 << endl;
cout << "non-printing char (escape): "
<< char(27) << endl;
}
Any character can be sent to a stream object using a cast to a char(data type
which holds single characters). This cast looks like a function call:char(). In
the above program char(27) sends an "escape" to cout.