C++ is a general-purpose programming language and widely used nowadays for competitive programming. It has imperative, object-oriented and generic programming features. C++ runs on lots of platform like Windows, Linux, Unix, Mac etc.
C++ is an efficient and powerful language and finds wide use in various GUI platforms, 3D graphics and real-time simulations. Because of the inclusion of rich function libraries, working in C++ becomes simpler and convenient than C. Being object-oriented programming like Java, C++ provides the support of inheritance, polymorphism, encapsulation, etc. Unlike C, C++ allows exception handling and function overloading. Bringing in the important topics under one roof, this tutorial to C++ is a very efficient and convenient way to learn C++ from scratch.
C++ Tutorials – A standard hierarchial approach
Below is the complete step-by-step tutorial showing how to get started with C++ and make yourself proficient in it.
- About C++: To begin with let’s lay the foundation by knowing how and why C++ as a programming language is so important and what uses it finds in the field of computer science and programming.
- Setting up the Environment: After gaining a brief introduction on C++, the next step is to know how to get the most out of this language by implementing various programs in it. C++ runs on lots of platform like Windows, Linux, Unix, Mac, etc. Before we start programming with C++. We will need an environment to be set-up on our local computer to compile and run our C++ programs successfully. If you do not want to set up a local environment you can also use online IDEs for compiling your program.
- Basics in C++: So after setting up the language, let’s begin to harness the basics and build a conceptual framework on how to write the programs and various provisions the language has to provide to the geeks. In this article, we would start right from writing our first C++ program to learning Input/Output, Operators, Variables, Loops etc.
- Functions in C++: A function is a set of statements that take inputs, do some specific computation and produces output. The idea is to put some commonly or repeatedly done task together and make a function, so that instead of writing the same code again and again for different inputs, we can call the function.
- Arrays in C++: An array is collection of items stored at contiguous memory locations. We can use normal variables (v1, v2, v3, ..) when we have a small number of objects, but if we want to store a large number of instances, it becomes difficult to manage them with normal variables. The idea of an array is to represent many instances in one variable.
- Strings in C++: Strings are defined as an array of characters. The difference between a character array and a string is that string is terminated with a special character ‘\0’. These are used to work with multiple characters and various special characters. Unlike C, C++ provides users with a rich library to make the operations on string easier and effective to implement.
- Pointers & References in C++: Pointers are symbolic representation of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. The reason we associate data type to a pointer is that it knows how many bytes the data is stored in. When we increment a pointer, we increase the pointer by the size of the data type to which it points.
- Classes and Objects in C++: The building block of C++ that leads to Object-Oriented programming is a Class. It is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object. An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated.
- Object-Oriented Programming in C++: As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.
- Namespaces in C++: Namespaces allow us to group named entities that otherwise would have global scope into narrower scopes, giving them namespace scope. This allows organizing the elements of programs into different logical scopes referred to by names. The namespace is a feature added in C++ and not present in C. A namespace is a declarative region that provides a scope to the identifiers (names of the types, function, variables etc) inside it. Multiple namespace blocks with the same name are allowed. All declarations within those blocks are declared in the named scope.
- Preprocessor in C++: As the name suggests Preprocessors are programs that processes our source code before compilation. There are a number of steps involved between writing a program and executing a program in C / C++. Let us have a look at these steps before we actually start learning about Preprocessors.
- Templates in C++: A template is a simple and yet very powerful tool in C++. The simple idea is to pass data type as a parameter so that we don’t need to write the same code for different data types. For example, a software company may need sort() for different data types. Rather than writing and maintaining the multiple codes, we can write one sort() and pass data type as a parameter. C++ adds two new keywords to support templates: ‘template’ and ‘typename’. The second keyword can always be replaced by keyword ‘class’.
- STL in C++: The Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions such as lists, stacks, arrays, etc. It is a library of container classes, algorithms, and iterators. It is a generalized library and so, its components are parameterized. Working knowledge of template classes is a prerequisite for working with STL.
- Exception Handling in C++: One of the advantages of C++ over C is Exception Handling. Exceptions are run-time anomalies or abnormal conditions that a program encounters during its execution. There are two types of exceptions namely, Synchronous and Asynchronous exceptions. C++ provides try, catch and throw for this purpose.
- Files and Streams in C++: In C++, files are mainly dealt by using three classes fstream, ifstream, ofstream available in fstream headerfile. ofstream class is used while writing on files, ifstream class is used to read from files and fstream class is used to both read and write from/to files.
- Dynamic Memory in C++: Dynamic memory allocation in C/C++ refers to performing memory allocation manually by programmer. Dynamically allocated memory is allocated on Heap and non-static and local variables get memory allocated on Stack (Refer Memory Layout C Programs for details).
- Signal Handling in C++: Signals are the interrupts that force an OS to stop its ongoing task and attend the task for which the interrupt has been sent. These interrupts can pause service in any programs of an OS. Similarly, C++ also offers various signals which it can catch and process in a program.
- Multithreading in C++: Multithreading support was introduced in C+11. Prior to C++11, we had to use POSIX threads or p threads library in C. While this library did the job the lack of any standard language provided feature-set caused serious portability issues. C++ 11 did away with all that and gave us std::thread. The thread classes and related functions are defined in the thread header file.