C plus plus
C++ (pronounced "see plus plus", IPA: /si? pl?s pl?s/) is a general-purpose computer programming language. It is a statically typed free-form multi-paradigm language supporting procedural programming, data abstraction, object-oriented programming, and generic programming. Since the 1990s, C++ has been one of the most popular commercial programming languages.
C++ examples
Example 1
This is an example of a program which does nothing. It begins executing and immediately terminates. It consists of one thing: a main() function. main() is the designated start of a C++ program.
Related Topics:
Function - Program
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
int main()
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
return 0;
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
}
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
The C++ Standard requires that main() returns type int. A program which uses any other return type for main() is not Standard C++.
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
The Standard does not say what the return value of main() actually means. Traditionally, it is interpreted as the return value of the program itself. The Standard guarantees that returning zero from main() indicates successful termination. Note that the Standard does not require the return explicitly; the lack of a return is assumed to mean return 0. However, many compilers do not support this and mark it as an error.
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Indicating unsuccessful termination from a C++ program is done by returning the EXIT_FAILURE constant, which is defined in the cstddef standard header.
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Sometimes the passing of parameters to the main function is desirable. In this case one would write the following code:
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
int main(int argc, char* argv)
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
return 0;
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
}
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Where int argc is the number of parameters and argv is an array of strings that contains the different parameters.
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Example 2
This is an example of a Hello world program, which uses the C++ standard library (not STL) cout facility to display a message and then terminates.
Related Topics:
Hello world program - C++ standard library - Message
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
- include
// Required for std::cout and std::endl
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
int main()
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
std::cout
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
return 0;
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
}
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Example 3
- include
int main()
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
int response;
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
std::cout
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
std::cin >> response;
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
if (response == 1)
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
std::cout
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
}
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
else
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
std::cout
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
}
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
return 0;
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
}
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
This program asks for a user input (with cout), and stores the subsequent information in the 'response' variable. This is then used in the if block to make a decision as to which message should be displayed.
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Example 4
Modern C++ can accomplish advanced tasks in a simple manner. This example demonstrates the use of the C++ Standard Template Library containers map and vector, among other features.
Related Topics:
Standard Template Library - Map - Vector
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
- include
// std::cout - include
// std::vector<> - include
- include
// std::for_each() - include
// std::string - include
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
using namespace std; // import "std" namespace into global namespace
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
void display_item_count(pair< string const, vector > const& person)
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// person is a pair of two objects: person.first is person's name,
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// person.second is a list of person's items (vector of strings)
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
cout
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
}
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
int main()
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// Declare a map with string keys and vectors of strings as data
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
map< string, vector > items;
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// Add some people to the map and let them carry some items
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
items.push_back("scarf");
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
items.push_back("tickets");
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
items.push_back("puppy");
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// Iterate over all the items in the container
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
for_each(items.begin(), items.end(), display_item_count);
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
return 0;
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
}
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
In this example, a "using directive" was used for brevity though in a real-life program, its use should be much more restricted. "Using declarations", which are more specific than directives, are usually recommended :
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
int main()
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
using std::vector;
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
vector my_vector;
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
return 0;
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
}
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Note that the declaration was put at function scope, which reduces chances of clashing (which was the reason for which namespaces were introduced in the language). Dumping whole namespaces in the global namespace, with using directives, defeats the very purpose of namespaces.
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Check out more C++ examples.
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
~ Table of Content ~
| ► | Introduction |
| ► | Technical overview |
| ► | Object-oriented features of C++ |
| ► | Design of C++ |
| ► | History of C++ |
| ► | C++ is not a superset of C |
| ► | C++ examples |
| ► | See also |
| ► | References |
| ► | External links |
~ What's Hot ~
~ Community ~
| ► | History Forum Come and discuss about History, Civilizations, Historical Events and Figures |
| ► | History Web-Ring A community of sites, blogs and forums dedicated to History. Do not hesitate to submit your site. |
and are licensed under the GNU Free Documentation License.
Lexicon - Privacy Policy - Spiritus-Temporis.com ©2005.