Text
Hello, World!
Welcome to part 2 of the C++ Tutorial Series
This post will cover one of the first programs you will write in any language: the Hello, World! program.
Major points I'll be touching on:
Comments.
Including code from other sources.
A brief note on the Standard Library
The "main" function.
Outputting text to the command line.
An important thing to note is that your code files will have the extension .cpp, for example you may call your main file in a large project main.cpp or projectName.cpp. Certain files called "header files" will have a different extension: .h or .hpp but we'll be touching on those later. Deciding between the two header file extensions is simply a matter of opinion.
Comments
Comments in C++ (and other programming languages) are used to leave a message in the code that will be ignored by the compiler. This is important so other people (or yourself) can understand what the code is supposed to do when they're reading it later on.
To write a comment on a single line, we use //. This means that everything after the // won't affect your code until the start of the next line. Example:
//This is a comment. This is not a comment, and your code will not compile.
We can also have comments spanning multiple lines. These are opened with /* and closed with */. This is useful for longer explanations of what a program is expected to do. Example:
/* This is a comment. * So is this. * The * on each line isn't necessary. * Just makes it easier to see when the comment ends. */ This is not a comment.
For clarity, especially in later lessons when multiple files may be referenced, files will be labeled at the top with their filename. Example:
//examplefile.cpp /* * Code continues below */
Code From Other Sources
A lot of programming tasks are incredibly repetitive, difficult, or frequently used. In these situations, programmers will often group them together into convenient files for re-use in multiple projects at a later date. These files are called Libraries.
Most C++ compilers come with a collection of libraries called the Standard Template Library (STL). The STL contains a wide variety of incredibly useful functions and data structures, saving developers a lot of time and effort. Console output (printing text to the command line) is one of the many useful features.
To do this, we need to add #include <iostream> to the top of the .cpp file. IOStream stands for Input and Output Stream. This means that content in this part of the data is able to send and receive streams of data which can be of any size.
The Main Function
The main function is the core of your program. This is where execution starts and stops. Every other sub-component of the software branches out from here. The function is declared as int main(int argc, char **argv)
That's a bit complicated, so let's break it down piece-by-piece. The first word, int is the "return type". This is the type of information that a function will give back to whatever triggered it. That means that when int main is finished, it will return an int value. int means an integer (positive or negative whole number), but we'll get into that in a later post.
The next part of the function is main. This is the name of the function.The main important thing is that you can't have 2 things with the same name. For the most part, function naming is done using camel case, meaning the first word starts with a lowercase letter, but the first letter of every other word is capitalized. Examples: main, moreCode, and moreWordsThanThisNameReallyNeeds. The goal is to make your names easy to read, and accurately describe the function without being too long to reasonably type.
Next up in the function are the arguments. Functions can receive outside data if they are declared with arguments between the parentheses, such as (int argc, char **argv). We won't be passing along any data from the command line quite yet, so these aren't particularly useful for us. For now you can declare your main function as:
//main.cpp int main( ) { }
Normally you can't just leave out the arguments, but main is an exception.
Outputting To The Command Line
Now we get into our first bit of actual functional code.
The first thing we'll want to do is include the iostream library.
//main.cpp #include <iostream>
Next, we declare our main function:
//main.cpp #include <iostream> int main() { }
Now, we want to write the phrase "Hello, World!" to the screen. Most tutorials on the internet will have you use the line using namespace std; but we won't be doing this. What this does is bring everything from the "std" namespace into the global namespace. This effectively shortens their names by 5 characters. Example:
//Without using namespace std; std::string name = "John;" //with using namespace std; string name = "John";
This may seem like a great way to make your life easier, but can actually cause conflicts in large codebases, or when using multiple different libraries. You may encounter a situation where there are multiple different "string" data types in different namespaces. The compiler will not accept multiple definitions using the same name in the same namespace, so it's best to just leave things where they were designed to be.
To actually output your text, we will use something called std::cout. You can pass data into this object, and it will print the data out to your command line. To do this, we use << to "push" the data into cout. Example:
//main.cpp #include <iostream> int main() { std::cout << "Hello, World!"; }
In its current state, anything sent to the command line after our message will be on the same line. We want our information to be separate from anything sent after it. To do this we add std::endl which is the STanDard library's way to END a Line. So our final product is
//main.cpp #include <iostream> int main() { std::cout << "Hello, World!" << std::endl; }
This will print our message and return the cursor to the beginning of the next line.
Compilation
In order for our code to actually do something, we need to convert it into a format that your computer can "read". We do this by using Clang++, which is a part of the LLVM compiler package included in the requirements in the first post (link at bottom). To do this, you must first open your command line and navigate to the directory where you placed your .cpp file. Next, you type clang++ main.cpp -o helloWorld This executes a program called "clang++", gives it main.cpp to work with (you can replace this with any file name), and the -o tells it that you want to specify an output file (helloWorld). On Windows you will want to add .exe to the end of your output file name. If there are any errors, check with the examples above to ensure your code is typed correctly. C++ is case-sensitive, meaning capitalizing a letter is enough to completely change the meaning. On Windows you may still receive a warning if your code is typed correctly. Your code will still work, but it can be annoying, and can be resolved by using clang-cl instead of clang++. Once the code is compiled, you can run it by typing ./helloWorld on bash, helloWorld.exe on CMD, or .\helloWorld.exe on Windows PowerShell.
Feel free to change the code, and pass other strings through std::cout. Experimentation is the best way to learn what your code can and can't do.
Previous Post: Getting Started Next Post: Coming Soon! Main Blog
#C++#C++ language#C++ programming#C++ tutorial#tutorial#programming#code#compiler#clang#clang++#series#part 2
0 notes
Text
Getting Started With C++
Welcome to part 1 of my new C++ Programming Tutorial!
This post will cover the basic setup required to begin making simple software using the C++ programming language on both Windows and Linux. While I assume Mac will be similar, I don't have access to one for testing.
Requirements
A compiler. I recommend LLVM (I will be using version 7.0.1)
A text editor or Integrated Development Environment (IDE)
Certain IDEs (such as Eclipse CDT) will require the JDK
Basic knowledge of your operating system's command line (moving between directories).
LLVM Install
Windows
From the link above, select the appropriate compiler binary. Any windows version from 7 up should be 64 bit. During the install it will ask if you want to add LLVM to the system PATH. In order for the command line tools to work, you will need LLVM and its related binaries to be added to the system PATH. The middle option should do this for all users. You can test your install by typing clang++ in the command line. If it says the command is not recognized, something is wrong. If it says no input file given, you're ready to go.
Linux
Check your distribution's package manager to see if the available llvm or clang version is up-to-date. The current version as of this writing is 7.0.1. If not, binaries may be available for your distribution from the link above, or you can use any C++ compiler to compile the source yourself following the instructions on the LLVM website. Typing clang++ --version should show the current installed version if installation was successful.
Selecting an IDE
There are a lot of options out there for text editors and IDEs. The only real rule is not to use something like Microsoft Word, because it adds additional formatting, which makes your code unreadable to the compiler.
Some of the options I've used are:
Notepad++, a nice text editor without the bells and whistles. Color coding support for a wide variety of languages.
Eclipse CDT
Cevelop (Based on the Eclipse CDT, with some nice changes)
CLion (Quite expensive, but free for students. I use this most.)
Many more options out there. Your choice doesn't affect much, except possibly color coding more modern features.
One important note is that with the exception of Notepad++, the examples I've listed will all require the JDK (link at top)
This is it for setup. In my next post, I'll cover basic command line output, and how to compile your code.
Next Post: Coming Soon. Main Blog
#first post#c++#c++ language#programming#getting started#tutorial#programming tutorial#eclipse#ide#CLion#Cevelop#Notepad++
0 notes