Basics
C++ is a compiled language, an upward compatible superset of C and an (incompatible) predecessor to Java. C++ compiles C programs but adds object oriented (OO) features (classes, inheritance, polymorphism), templates (generic functions and classes), function and operator overloading, namespaces (packages), exception handling, a library of standard data structures (string, vector, map, etc.) and formatted text I/O (istream, ostream). Unlike Java, C++ lacks a standard graphical user interface (GUI), network interface, garbage collection, and threads, and allows non-OO programming and unchecked low-level machine operations with pointers. However, C++ executes faster than Java and requires no run-time support.
A C++ program is a collection of function, object, and type declarations. Every program must have a function int main() { ... } where the curly braces enclose a block, a sequence of declarations and statements ending in semicolons which are executed in order. A statement is an expression, block, or control statement that alters the order of execution, such as if, while, for, break, return. Some types (std::string), objects (std::cout), and functions are defined in header files, requiring the line #include <header> before use. Items defined in the standard headers are in the namespace std. The std:: prefix may be dropped after the statement using namespace std;. For instance,
// Comment: prints "Hello world!" and an OS-independent newline
#include <string> // Defines type std::string
#include <iostream> // Defines global object std::cout
using namespace std; // Allow std:: to be dropped
int main() { // Execution starts here
string s="Hello world!\n"; // Declares object s of type string
cout << s; // An expression as a statement, << is the output operator
return 0; // Execution ends here
}
The symbol // denotes a comment to the end of the line. You may also use /* ... */ for multiline comments. Spacing and indentation is used for readability. C++ is mostly free-form, except that the end of line is significant after # and //. C++ is case sensitive.
C++ source code files should be created with a text editor and have the extension .cpp. If the above is called hello.cpp, it may be compiled and run as follows in a UNIX shell window:
g++ hello.cpp -o hello -Wall -O
./hello
The -o option renames the executable file, by default a.out. -Wall turns on all warnings (recommended). -O optimizes (compiles slower but runs faster).
In Windows, the GNU C++ compiler is called DJGPP. To compile and run from an MS-DOS box:
gxx hello.cpp -o hello.exe
hello
The output file must have a .EXE extension (default is A.EXE). There is also a .OBJ file which you can delete.
To use the network or GUI interface in UNIX, you must use the X and socket libraries, which don't work in Windows. In Windows, you must use the Windows API and a compiler that supports them, such as from Microsoft, Borland, or Symantec. GUI/network programming is nonportable and outside the scope of this document.
Links to free and commercial C++ compilers can be found at cplusplus.com.
C++ is a compiled language, an upward compatible superset of C and an (incompatible) predecessor to Java. C++ compiles C programs but adds object oriented (OO) features (classes, inheritance, polymorphism), templates (generic functions and classes), function and operator overloading, namespaces (packages), exception handling, a library of standard data structures (string, vector, map, etc.) and formatted text I/O (istream, ostream). Unlike Java, C++ lacks a standard graphical user interface (GUI), network interface, garbage collection, and threads, and allows non-OO programming and unchecked low-level machine operations with pointers. However, C++ executes faster than Java and requires no run-time support.
A C++ program is a collection of function, object, and type declarations. Every program must have a function int main() { ... } where the curly braces enclose a block, a sequence of declarations and statements ending in semicolons which are executed in order. A statement is an expression, block, or control statement that alters the order of execution, such as if, while, for, break, return. Some types (std::string), objects (std::cout), and functions are defined in header files, requiring the line #include <header> before use. Items defined in the standard headers are in the namespace std. The std:: prefix may be dropped after the statement using namespace std;. For instance,
// Comment: prints "Hello world!" and an OS-independent newline
#include <string> // Defines type std::string
#include <iostream> // Defines global object std::cout
using namespace std; // Allow std:: to be dropped
int main() { // Execution starts here
string s="Hello world!\n"; // Declares object s of type string
cout << s; // An expression as a statement, << is the output operator
return 0; // Execution ends here
}
The symbol // denotes a comment to the end of the line. You may also use /* ... */ for multiline comments. Spacing and indentation is used for readability. C++ is mostly free-form, except that the end of line is significant after # and //. C++ is case sensitive.
C++ source code files should be created with a text editor and have the extension .cpp. If the above is called hello.cpp, it may be compiled and run as follows in a UNIX shell window:
g++ hello.cpp -o hello -Wall -O
./hello
The -o option renames the executable file, by default a.out. -Wall turns on all warnings (recommended). -O optimizes (compiles slower but runs faster).
In Windows, the GNU C++ compiler is called DJGPP. To compile and run from an MS-DOS box:
gxx hello.cpp -o hello.exe
hello
The output file must have a .EXE extension (default is A.EXE). There is also a .OBJ file which you can delete.
To use the network or GUI interface in UNIX, you must use the X and socket libraries, which don't work in Windows. In Windows, you must use the Windows API and a compiler that supports them, such as from Microsoft, Borland, or Symantec. GUI/network programming is nonportable and outside the scope of this document.
Links to free and commercial C++ compilers can be found at cplusplus.com.
No comments:
Post a Comment