Online Classes

Results can only be achieved if you have the focused direction and crystal clear knowledge. To achieve this, you need a mentor. We will help you out by connecting with an expert mentor in the field.

Learn More

Article

Introduction to C++

4 min 56 sec read

C++ is middle-level programming language which means it supports both high level and low-level programming features. It is not purely an object-oriented programming language as some of the features in C++ like friend functions violates the philosophy behind the object-oriented feature.

C++ is used in various system drivers, most antivirus software is written in C++. It was developed by Bjarne Stroustrup in 1979. C++ is considered as static type programming language. A programming language is said to use statically typed when type checking is performed during compile-time as opposed to run-time.

Features of Object Oriented Programming

 

  1. Encapsulation
  2. Polymorphism
  3. Inheritance
  4. Data Hiding

 

We will explore each of these topics in upcoming articles in detail. First we cover the basics of simple C++ program.

Basic Syntax

Open any text editor like notepad,notepad++ or sublime or any other editor you are comfortable with, write this program and save it as abc.cpp at your destination like on Desktop.

#include 
using namespace std;

int main() {
 // your code goes here
  cout<< "Welcome to C++";
  return 0;
}

How to run a program in C++?

Open terminal in Mac, Linux or Command Prompt in Windows. Access the directory where your C++ file is stored. In our case, it is saved as "abc.cpp" file on desktop. Type the commands one by one and hit Enter.

cd Desktop
g++ abc.cpp
./a.out

Welcome to C++

Various parts of program in c++ are explained below :-

1) #include :- is used to include something in a program.

2) :- is used to include all the functions and libraries written in this file for the use of programmers like cout,cin etc.

3) using namespace std :- says use a namespace named std.

4) int main :- is starting point of eecution . That means Function is called first whenever you executes a program.

5) cout :- is used to print the statements . It is like scanf() and printf() in C.

6) return 0; :- is used to return an int value because specified returntype before main() function is int .

Comments in C++

C++ Comment is written as follows:

1) /* This is a multi-line 
        comment syntax */

2) // This is single line comment syntax

Identifiers:

An Identifiers in C++ are simply the names used to identify a variable, function, or any other user-defined item. An identifier starts with a letter A to Z, a to z, or an underscore '_' followed by zero or more letters, underscores, and digits (0 to 9). C++ does not allow punctuation characters such as @, $, and % within identifiers. C++ is a case-sensitive programming language. Thus, Programming and programming are two different identifiers in C++.

abc , _abc , abc_123 are valid tokens
whereas,
$abc,#abc,abc$ are not valid tokens . 

Tokens in C++:

Every C++ program consists of various tokens. Tokens in C++ can either be a keyword, an identifier, a constant, a string literal, or a symbol. There are 4 tokens in the given example below if you noticed,7

cout                        --> 1st token
<<                      --> 2nd token
"Welcome to C++"      --> 3rd token                               
;                                --> 4th token

Basic Introduction of Classes & Objects

Consider the class as a Home. Home can be of various types like Bungalow, Palace, Flats. So consider class as a blueprint at high level which consists of various properties & methods.

Open text editor notepad,notepad++ or sublime or any other editor , write this program and save it as PQR.cpp

#include 
using namespace std;
 class PQR
{
public:
 int i;                   //data variable
  void  display()         //Member Function
  { 
   cout<<"Always remember by default
 methods are private in C++ :)";
  }
}; // Class ends here

int main()
{
 PQR pqrObject;         // Creatig PQR class's object
 pqrObject.display();   //Calling member function using class object
}

Output

Open terminal ,

g++ PQR.cpp
./a.out

Always remember by default methods are private in C++ :)

This was just the basic introduction to C++. We will explore each and every topic in detail we discussed here. Just go with the flow. Have a look on next article of Datatypes in C++ you should know.

Try to execute what you have learnt

Easy to use online data structure compiler where you can execute the programs in your favourite programming language.
(C, C++, Python)

Open Compiler

HTML, CSS and Javascript Real time Web Editor

Execute your HTML, CSS and javascript code in real time with the web editor
(HTML, CSS, Bootstrap, Javascript)

Open Web Editor