C plus plus examples

The title of this article is incorrect because of technical limitations. The correct title is C++ examples.

These are some examples of code from the C++ Programming Language.

Conditionals

if (tester == true)
{
    std::cout << "true" << std::endl;
}
else if (tester == false)
{
    std::cout << "false" << std::endl;
}

This code checks whether the boolean variable is true or false and prints out the corresponding value. Note that if tester is of bool type, it is the same as the following code: (If tester is not a boolean variable, and not either 0 or 1, then the code above and code below mean something completely different)

if (tester == true)
{
    std::cout << "true" << std::endl;
}
else
{
    std::cout << "false" << std::endl;
}

This is a more conventional way of doing it:

if (tester)
{
    std::cout << "true" << std::endl;
} 
else
{
    std::cout << "false" << std::endl;
}

You can also combine ifs, else ifs, and elses all in the same statement, such as this code that prints out a grade letter based on percentages:

if (grade >= 90)
{
    std::cout << "A" << std::endl;
}
else if (grade >= 80)
{
    std::cout << "B" << std::endl;
}
else if (grade >= 70)
{
    std::cout << "C" << std::endl;
}
else if (grade >= 60)
{
    std::cout << "D" << std::endl;
}
else
{
    std::cout << "F" << std::endl;
}

Note that the braces are not actually part of the syntax of the if statement. The syntax requires only that the clauses be statements, not necessarily compound statements. Thus, the following is also correct:

if (tester)
    std::cout << "true" << std::endl;
else
    std::cout << "false" << std::endl;

Of course, a compound statement, if needed, will carry with it a pair of braces:

if (tester)
{
    std::cout << "true" << std::endl;
    n = 1;
}
else
{
    std::cout << "false" << std::endl;
    n = 0;
}

One case requiring a compound statement is the use of nested if statements. The C++ rules state that the 'else' clause matches to the closest 'if'. If this is not what is intended, the inner if must be enclosed in a compound statement.

// NB misleading indentation obscuring an error
if (tester1)
    if (tester2)
        std::cout << "tester1 and tester2 are both true" << std::endl;
else
    std::cout << "tester1 is false" << std::endl; 

In this example, the logic implied by the indentation does not match the logic implied by the rules of C++, as the else will actually match to the inner if. To correct this, braces are required:

if (tester1)
{
    if (tester2)
        std::cout << "tester1 and tester2 are both true" << std::endl;
}
else
    std::cout << "tester1 is false" << std::endl;

STL

The Standard Template Library provides a variety of useful containers for you to use. Here is an example involving the STL vector:

#include <vector>
#include <iostream>
void main() {
    std::vector<int> my_vector; // Create a vector that will hold integers
    int input = 1; // Create a temporary variable to hold the user's input
    std::cout << "Enter some numbers, enter zero to quit!" << std::endl;
    while(input) { // While input is nonzero
        std::cin >> input;
        my_vector.push_back(input); // This function adds input to the back of the vector
    }
    std::cout << "You entered the following numbers:" << std::endl;
    // Now, iterate through the contents of the vector
    for(std::vector<int>::iterator i =
    my_vector.begin(); // Vector.begin() returns an iterator that points to the beginning of the list
    i != my_vector.end(); ++i) {
        // Use (*my_iterator) to access the contents of the iterator
        std::cout << (*i) << std::endl;
    }
    std::cout << "Goodbye!" << std::endl;
}
Navigation

  • Art and Cultures
    • Art (https://academickids.com/encyclopedia/index.php/Art)
    • Architecture (https://academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (https://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (https://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools