C++ is generally compiled and compilers exist for many platforms, however due to the complexity of the C++ standard, compilers don't always behave the same way.
Why would I learn this language?
C++ is in some ways a super-set of C. It was originally called "C with Objects". It adds many advanced language features such as classes, namespaces, exceptions and meta-programming.
C++ has many of the same issues as C, but it also adds many new issues (complex syntax and semantic behavior). On the whole, however, it is a useful language which is used in many different industries.
C++ provides a useful level of abstraction and performance, and is used in both application development. The majority of computer games are written using C++.
Starting Points
- C++ Language Tutorial
- A beginner guide to programming in C++.
- An Introduction to GCC
- A manual that provides an introduction to the GNU C and C++ compilers.
Example Code
The following is an example of the Fizz Buzz problem. You can run and edit this program here.
#include <iostream>
int main(){
for(int i = 1; i <=100; i++){
if(i % 3 == 0 && i % 5 == 0){
std::cout << "FizzBuzz\n";
}
else if(i % 3 == 0){
std::cout << "Fizz\n";
}
else if(i % 5 == 0){
std::cout << "Buzz\n";
}
else{
std::cout << i << "\n";
}
}
return 0 ;
}
The following is an example of the 100 doors problem. You can run and edit this program here.
#include <iostream>
#include <vector>
void doors (int n)
{
// Initialize a vector of n boolean values
std::vector<bool> is_open(n);
// Process the doors
for (int pass = 0; pass < n; ++pass)
for (int door = pass; door < n; door += pass+1)
is_open[door].flip();
// Print out the results
for (int door = 0; door < n; ++door)
std::cout << "Door #" << door+1 << (is_open[door] ? " is open." : " is closed.") << std::endl;
}
int main(int argc, char ** argv)
{
// Call the doors function with n = 100
doors(100);
return 0;
}