I've been writing C++11 or better code, well since 2010, as I started doing so with the TR1 as was. We're nearing eight years since then, and it starts to show.
So, where would I recommend starting to learn modern C++? Well, if you've never programmed before, don't start by learning C or C++, go learn Pascal, or Python, or something else which is more friendly. I started out in Pascal, for a good three years, before I started to work in C and later moved into C++ (in 1998) so if 20 years of C++ teach me anything, it's don't try to learn if as your first language.
Once you have a concept of how to program, then start to learn C++, and I would recommend finding someone - hopefully like me - and asking them. An hours chat with them, to help swap what you know with what they know, is a good start.
Saving any such friends, YouTube, watch CPPCon, BoostCon, watch talks about programming C++ but most importantly get a development environment and cut some code, if you must go for a community edition of Visual Studio 2017, but otherwise get a Linux machine up and running and have a play.
If you work in an office where there's a C++ guru, ask them, if they're worth their salt they'll be more than happy to take you through a few things.
And failing that, I include a set of programs below, one through three, these are the most simplistic C++ programs I would recommend you start off working with, and if you want to know more comment below.
So, open an editor, and write this C++ code, then save the file as "main.cpp":
#include <iostream>
int main ()
{
std::cout << "Hello World";
}
If you are in Visual Studio, you run that file, if you're in linux close the editor after saving and lets use the gnu g++ compiler (install it in Ubuntu say with "sudo apt install g++"), and you compile this into a program like so:
g++ main.cpp -o example1
Your program output will be called "example1", and you can run that program, and it'll say "Hello World".
The code itself I want to only explain the first line... the include, this tells the compiler to include some code for you to use, and "iostream" is the input/output streaming functions for you.
int main is the first function the program starts to run from, int is the return type, meaning integer, but in C++ we don't need to return a value here - if anyone argues with you about this, they're wrong. The name "main" is the a special name and the compiler makes sure your program starts with this function entry point. The empty brackets show we're not passing anything into the function, there are no parameters.
The braces mark out the body of code for the main function, so it starts with an open brace, contains lines of code and then ends with a close brace, and yes I call them "braces" not "curly brackets" :)
The one and only line of code we've got left is the output call, this is streaming the value on the right of the "<<" chevrons to the standard character output stream... "std::cout"... I'll reiterate 'standard character output stream". And the value we're streaming is a string (note the quotes in the code) "Hello World".
This is the only line of code with the all important semicolon ending, this is used to tell the compiler that we're done with out line of code.
Go, try this...
The next most basic program for C++ noobies to learn, is to maybe output some more kinds of data.... After say, asking the user something...We've seen cout, how about "standard character input"?...
#include <iostream>
#include <string>
int main ()
{
std::cout << "What is your name? ";
std::string name;
std::cin >> name;
std::cout << "\r\n";
std::cout << "Oh Hi " << name;
}
We're introducing a new header, the "string" header helps us store strings of characters in the type "standard string"... "std::string"... Did you spot that?... This is our first variable, and it has the name "name" so we can refer to it later.
As ask the user a question by sending characters out "<<" to the output stream, and then we read them back in ">>" from the character input stream, and we read them into the "name" variable we just created.
Next we output a carriage return "\r" to move our carat back to the left of our screen, and we move to the next line "\n".
And finally we output a piece of text AND our input variable!
You can play with this code with other variable types, other than string... Try "int" to read in a whole number, of "float" to read in a floating point number.
But our third program, will involve some actual processing.... Lets average the age of a set of people we ask the user to input...
#include <iostream>
int main ()
{
int TotalAge (0);
int age;
std::cout << "Enter the name of person 1: ";
std::cout >> age;
TotalAge = TotalAge + age;
std::cout << "\r\n";
std::cout << "Enter the name of person 2: ";
std::cout >> age;
TotalAge = TotalAge + age;
std::cout << "\r\n";
std::cout << "Enter the name of person 3: ";
std::cout >> age;
TotalAge = TotalAge + age;
std::cout << "\r\n";
float Result = TotalAge / 3;
std::cout << "The average Age is: " << Result
}
Here we have much more code, we ask for each persons age, adding it to the total, then we calculate the resulting average age and print it out. Take a moment to look at this....
What do you see? If the first thing you see is that there are three sets of the same code in there, then you have the seed of a programming within. If however you just see a mess, then maybe C++ isn't for you.
No comments:
Post a Comment