Introduction to C++ | C++ Basics Tutorial

Hey there! It takes courage to start learning a new programming language and we at TechVidvan will be with you at every step. Let’s first introduce you to the world of C++ and what makes it a world-famous language.

Kudos if you know a thing or two about C, which will make your journey to learn Cpp much easier. But, even if you are not familiar with the C language don’t worry, we will begin with C++ from the very start. 

History of C++

In 1979, a Danish Computer Scientist, Bjarne Stroustrup at Bell Labs began working on “C with Classes” and as the name suggests it is a superset of C. While working on his Ph.D. Thesis, he came across this language Simula, and found out that it had features that were helpful for large software implementations, but it was slow. He then aimed towards making a language as efficient and fast as C but filled with features as useful as Simula.

In 1982, he began developing a successor to “C with Classes” and named it “C++”, “++” being the increment operator. Features like operator and method overloading, virtual functions, single-line comments, references, constants, and many more added alongside.

What is C++?

C++ is a cross-platform and intermediate/mid-level programming language that was developed as an advanced model to C language. Since C was a low-level programming language it didn’t possess many features, which were then rendered in C++ (like OOPS, Error Handling, Operator Overloading, etc.).

Amazing C++ Facts

Here are some amazing facts about C++ that might interest you:

  • The word C++ refers to the nature of the occurrence of changes from C. “++” is a C increment operator.
  • C++ as one of the leading languages is behind the development of all kinds of technical and commercial software.
  • C++ introduces Object-Oriented Programming, which is not in C. Like many other things, C++ supports four main features of OOP: encapsulation, polymorphism, inheritance, and abstraction.
  • C++ received OOP features from Simula67 Programming language.
  • A function is a minimum requirement for the C ++ system to work. (At least the main function ())

Syntax of C++ Programming

Let us look at a simple code that prints “Welcome to TechVidvan!”

#include<iostream>
using namespace std;

int main()
{
    cout <<"Welcome to TechVidvan!";
    return 0;
}

To understand the code snippet, we will have to look at it line by line:

  • The C++ language defines the preprocessors or header files needed in the program. For this program, the header required is <iostream>.
  • The using namespace std; asks the compiler to use the std namespace.
  • The program execution will fail if there’s no main() function. int main() is the main function where program execution begins.
  • The cout << “Welcome to TechVidvan!”; is where we achieve the purpose of this program. The message gets printed in this line.
  • But, we are not done yet without return 0; the main() function won’t terminate and the execution will fail.

Why choose C++ over C?

To date, C is a dominant language and doesn’t seem to be retiring anytime soon. But, given the advances in microcontroller hardware and optimization, it might be time that developers and students start considering C++ for their perusal.

Listed below are some of the reasons: 

1. Modern OOP Techniques: C language invented 50 years ago, lacks everything that newer programming language has, such as OOP concepts like Encapsulation, Inheritance, and Polymorphism.

In C++, we can use these techniques and make the code much more efficient and increase its readability as well as re-usability. 

2. Microcontrollers: Compiler and toolchain support for microcontrollers has been frequently updated by the tool providers. Beyond this, microcontrollers have begun to add hooks to the tools, such that it is easier to develop C++ applications.

3. Exception Handling: In C, error/exception handling has been lacking all the while. Whereas, in C++ exception handling makes the language even superior.

4. Special Methods: The concept of constructors (and their different types) and destructors for objects makes C++ worth switching.

5. Better Macro: In C, we use Macros and in C++ we use Inline Functions, which makes the entire function body act like a Macro.

6. Advanced Features: In C++ there is no restriction for variable declaration i.e. we can declare them anywhere as long as the declaration is before the actual use. Type Checking is stronger in C++.

Features of C++ Language

Features of C++ Programming

C++ is one of the most widely used programming languages for multiple purposes. Thus mentioned below are some of the features which give it the upper hand over the other programming languages: 

1. Mid-level language: Being a mid-level language, C++ comprises advantages of both high-level and low-level languages.

For example, it can be used for drivers and kernels (low-level language features), as well as for games and desktop applications (high-level language features).

2. Cross-Platform: C++ can run on many platforms, hence team members with different machines can code together on a project. We can also compile C++ to numerous platforms, giving it an advantage over other programming languages like C# which is now restricted to Windows.

3. Gives more control: C++ gives users more control over system performance and memory management. Hence, there is no garbage collection like in Java. 

4. Advanced Concepts: Concepts like Object-Oriented Programming, gives us a clear and precise understanding of low-level implementations of polymorphism. 

5. Flexibility: OOP, Functional and Procedural are all supported in C++, making it flexible to switch styles as per need for different projects. Likewise, we can even combine different styles for the same project. 

6. Easy to learn: C++ is really similar to C# and Java, which makes it easier to switch to and from the other programming languages.

7. Ample library support: Besides 3rd party libraries for fast and swift development, C++ also supports libraries for data structures and algorithms.

8. Faster Execution: Being a compiled and procedural language, source code written in C++ shine in execution. Garbage collection and dynamic typing along with many features slow down the execution in modern languages.  

Applications of C++ Programming Language

C++ is an extensively used programming language also used for building and developing real-life applications, while some of them are:

1. Operating Systems: C++ language is often used in writing many major Operating Systems including Windows, Linux, and macOS. 

2. Newer programming languages: Despite being an extension of C, C++ is also used to develop many programming languages like C#, Java, JavaScript, PHP, Python, and many more. Thus, helped to bloom the software development industry.

3. Games: As discussed earlier, we know that C++ is faster and it allows speedy execution which helps developers to utilize the CPU for thorough functions. Effectively, C++ helps developing gaming engines as it gives better control over hardware.

4. Graphics User Interface Applications: C++ is also used to build most of the GUI Based applications which we use almost daily. Adobe Systems, Windows Media Player, and Blender being a few of them.

5. Database Software: In the world of Database Management, engines like MySQL, Redis, Postgres, and MongoDB are the most popular. It would be interesting to know that they were written in C++.

Besides, all this C++ has also helped us with the widely used browsers (Mozilla and Chrome), banking applications (Infosys Finacle), and Cloud/Distributed System (Bloomberg).

Keywords in C++

Just like any other programming language, C++ has keywords or “reserved words” whose meanings are already explained to the compiler. Keywords are those words that have a predefined meaning, hence we can not use them as identifiers.

C++ language has 75 keywords:

and continue if public try
and_eq default inline register typedef
asm delete int reinterpret_cast typeid
auto do typename long return
bitand double mutable short uchar_t
bitor dynamiccast namespace signed union
bool else new sizeof unsigned
break enum not state_cast using
case explicit not_eq static virtual
catch extern operator struct void
char false Or switch volatile
class float or_eq template wchar_t
compl for overload this while
const friend throw private xor
constcast goto protected true xor_eq

Comments in C++

Students, developers, and even teachers sometimes need to explain the code snippets for that C++ supports comments.

There are 2 types of comments :

1. Single-line comments (//)

A single-line comment starts with two front slashes and continues to the end of the line.

2. Multi-line comments (/* ….. */)

Multi-line comments or block comments are used when the explanation isn’t short and writing it on one continued line makes it unreadable. They begin with “ /* ” and end with “ */ “. 

Just to be clear, comments are not a part of the program and the compiler ignores them, so even if the comments have errors they won’t be detected.

Escape Sequences in C++

Escape sequences are non-printing characters used to control the printing behavior of the display stream objects, hence are also known as control characters. These characters are not printed in the output. They are prefixed with a backslash and a character to control the printing behavior. They can be inserted in any position of the string.

Some of the escape sequences along with their purpose are mentioned below:

  • \a – Bell
  • \n – New line
  • \r – Carriage return
  • \b – Backspace
  • \f – Formfeed
  • \t – Horizontal tab
  • \ – Quotation mark
  • \v – Vertical tab
  • \’ – Apostrophe
  • \\ – Backslash
  • \? – Question mark
  • \0 – Null

Let’s learn the concept of escape sequence by implementing them:

For example, take the previous code 

#include<iostream>
using namespace std;

int main()
{
    cout <<"Welcome to TechVidvan!";
    return 0;
}

Now, if you want to print “Welcome to” and “TechVidvan!” in separate lines, just simply insert the escape sequence “\n” in the middle of the string.

#include<iostream>
using namespace std;

int main()
{
    cout <<"Welcome to \nTechVidvan!";
    return 0;
}

Output:

Welcome toTechVidvan

Advantages of Programming in C++

  • C++ is the fastest and most efficient language.
  • C++ has a variety of uses and that is why learning a language makes it easier to understand the concept of OOP which is very important for the IT industry.
  • Learning C++ gives a good foundation and makes it easier to learn other programming languages.

Disadvantages of C++ programming

  • C ++ is often difficult to identify.
  • Error messages in C ++ can be extended and it is often difficult to correct the error.
  • Accessing libraries in C++ can be a very tedious job.
  • C++ source code is always prone to errors as type-checking is not present.

Libraries in C++

Standard C ++ programs are divided into three main sections:

1. The primary library includes data types, variables and text, etc.

2. A typical library includes a set of tasks that control cables, files, etc.

3. The Standard Template Library (STL) includes a variety of methods that use data structure.

Summary

In this C++ tutorial, we learned the basics of C++ including the syntax of a C++ program, the features and applications along with advantages and disadvantages. We even got to know about the escape sequence, standard libraries, and the keywords in C++.

This C++ tutorial from TechVidvan gets followed by various other topics in C++. Hopefully, this article helped you build a foundation that will make it easier to understand the following topics.