
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How to Learn C++ Programming?
So you've decided to learn how to program in C++ but don't know where to start. Here's a brief overview of how you can get started.
Get a C++ Compiler
This is the first step you'd want to do before starting learning to program in C++. There are good free C++ compilers available for all major OS platforms. Download one that suits your platform or you can use the tutorialspoint.com's online compiler on www.tutorialspoint.com/compile_cpp_online.php
GCC − GCC is the GNU Compiler chain that is basically a collection of a bunch of different compilers created by GNU. You can download and install this compiler from http://gcc.gnu.org/
Clang − Clang is a compiler collection released by the LLVM community. It is available on all platforms and you can download and find install instructions on http://clang.llvm.org/get_started.html
Visual C++ 2017 Community − This is a free C++ compiler built for windows by Microsoft. You can download and install this compiler from www.visualstudio.com/vs/cplusplus/
Write a C++ program
Now that you have a compiler installed, its time to write a C++ program. Let's start with the epitome of programming example's, it, the Hello world program. We'll print hello world to the screen using C++ in this example. Create a new file called hello.cpp and write the following code to it −
#include<iostream> int main() { std::cout << "Hello World\n"; }
Let's dissect this program.
Line 1 − We start with the #include<iostream> line which essentially tells the compiler to copy the code from the iostream file(used for managing input and output streams) and paste it in our source file. Header iostream, that allows performing standard input and output operations, such as writing the output of this program (Hello World) to the screen. Lines beginning with a hash sign (#) are directives read and interpreted by what is known as the preprocessor.
Line 2 − A blank line: Blank lines have no effect on a program.
Line 3 − We then declare a function called main with the return type of int. main() is the entry point of our program. Whenever we run a C++ program, we start with the main function and begin execution from the first line within this function and keep executing each line till we reach the end. We start a block using the curly brace({) here. This marks the beginning of main's function definition, and the closing brace (}) at line 5, marks its end. All statements between these braces are the function's body that defines what happens when main is called.
Line 4 −
std::cout << "Hello World\n";
This line is a C++ statement. This statement has three parts: First, std::cout, which identifies the standard console output device. Second the insertion operator << which indicates that what follows is inserted into std::cout. Last, we have a sentence within quotes that we'd like printed on the screen. This will become more clear to you as we proceed in learning C++.
In short, we provide a cout object with a string "Hello world\n" to be printed to the standard output device.
Note that the statement ends with a semicolon (;). This character marks the end of the statement
Compile the Program
Now that we've written the program, we need to translate it to a language that the processor understands, ie, in binary machine code. We do this using a compiler we installed in the first step. You need to open your terminal/cmd and navigate to the location of the hello.cpp file using the cd command. Assuming you installed the GCC, you can use the following command to compile the program −
$ g++ -o hello hello.cpp
This command means that you want the g++ compiler to create an output file, hello using the source file hello.cpp.
Run the program
Now that we've written our program and compiled it, time to run it! You can run the program using −
$ ./hello
You will get the output −
Hello world
Now that you've learned how to get started with the C++ programming language, you can start learning it by reading up some material on C++ on websites like C++http://www.cplusplus.com/doc/tutorial/, etc. These websites have excellent guides to get started with learning C++ and can help you get started.
Some other very helpful resources are books by various authors like Bjarne Stroustrup, Scott Meyers, etc. You can start with the book The tour of C++ and then move on to Effective C++ and the like. Here is a definitive book list for c++ you can check out: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list.
You can also check out the C++ standard to learn more about the language itself. It's freely available as a draft on the ISO C++ website: ISO
- Related Articles
- How to learn Python without prior programming knowledge?
- Best book to learn Java programming for beginners
- Why You Should Learn Python Programming?
- 5 Shell Scripts for Linux Newbies to Learn Shell Programming
- Time Required in India to Learn Java Without Any Programming Knowledge?
- Can I Learn Salesforce with no Programming/Coding Knowledge?
- Which is a More Worthwhile Field to Learn, Digital Marketing or Programming?
- Top Reasons to Learn C++
- Learn How to Swim
- How to Get Started with C++ Programming?
- How to start object-oriented programming in C++?
- How to Learn SAP ERP System?
- How to learn the periodic table?
- Learn How to Drive a Car
- What are the good resources to Learn C++?
