Unit-1: introduction to C++


“C++ is a statically-typed, free-form, (usually) compiled, multi-paradigm, intermediate-level general-purpose middle-level programming language.”

In simple terms, C++ is a sophisticated, efficient and a general-purpose programming language based on C. It was developed by BjarneStroustrup in 1979.

Many of today’s operating systems, system drivers, browsers and games use C++ as their core language. This makes C++ one of the most popular languages today.

Since it is an enhanced/extended version of C programming language, C and C++ are often denoted together as C/C++.


While BjarneStroustrup was working in AT&T Bell Labs in 1979, he faced difficulties in analyzing UNIX kernel for distributed systems. The current languages were either too slow or too low level. So, he set forward to create a new language.

For building this language, he chose C. Why C? Because it is a general purpose language and is very efficient as well as fast in its operations.

He used his knowledge of object-oriented model from SIMULA and began working on class extensions to C. His aim was to create a language with far higher level of abstraction while retaining the efficiency of C.

This new programming language was named C withClasses, but was later renamed to C++ (++ refers to the increment operator in C).



When C++ was first released in 1985, there were no official standards released. It was only until 1998 that C++ was first standardized which was known as C++98.


In 2003, a new version of C++ standard was published. C++03 wasn’t really a new standard altogether but a bug fix release identified with C++98 “to ensure greater consistency and portability”.

C++11 (C++0x)

The next major standard for C++ was released in 2011 and it was named C++11. Since, C++ committee was sure this update would be released within 2009, they unofficially named it C++0x. Later, when they didn’t, Stroustrup joked that C++0x went hexadecimal – C++0xB (C++11). Nice save.

C++14 (C++1y)

C++14 is the current iteration of C++ released in 2014. Like C++03, it included mainly bug fixes and simple improvements to C++11.

C++17 (C++1z)

The supposedly next iteration to C++ which is planned to be rolled out in 2017. It is expected to have many new features. Most of the features planned for this version are alreadted.


Being a general-purpose language, C++ is undoubtedly feature-rich. Going through all the features will take you some time but, as a beginner, below are the most important features you should know.

1.C++ is fast

Since, C++ is an extended version of C, the C part of it is very low level.

This offers a huge boost in speed that high level languages like Python, Java don’t give you.

2.C++  is statically typed

C++ is a statically typed programming language.

In simple terms, C++ doesn’t allow the compiler to make assumptions about the type of data e.g. 10 is different from “10” and you have to let C++ know which one you are talking about.

This helps the compiler catch errors and bugs before execution of the program.

3 .C++ is a multi-paradigm programming language

C++ supports at least 7 different styles of programming and gives developers the freedom to choose one at their will.

Unlike Java and Python, you don’t need to use objects to solve every task (if it’s not necessary).

You can choose the programming style that fits your use case.

4.Object oriented programming with C++

Object oriented programming helps you solve a complex problem intuitively.

With its use in C++, you are able to divide these complex problems into smaller sets by creating objects.

5.Power of standard library (Standard template library – STL)

The power of C++ extends with the use of standard libraries contained in it.

These libraries contain efficient algorithms that you use extensively while coding.This saves ample amount of programming effort, which otherwise would have been wasted reinventing the wheel.


The prime purpose of C++ programming was to add object orientation to the C programming language, which is in itself one of the most powerful programming languages.

The core of the pure object-oriented programming is to create an object, in code, that has certain properties and methods. While designing C++ modules, we try to see whole world in the form of objects. For example a car is an object which has certain properties such as color, number of doors, and the like. It also has certain methods such as accelerate, brake, and so on.

There are a few principle concepts that form the foundation of object-oriented programming −


This is the basic unit of object oriented programming. That is both data and function that operate on data are bundled as a unit called as object.


When you define a class, you define a blueprint for an object. This doesn’t actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.


Data abstraction refers to, providing only essential information to the outside world and hiding their background details, i.e., to represent the needed information in program without presenting the details.

For example, a database system hides certain details of how data is stored and created and maintained. Similar way, C++ classes provides different methods to the outside world without giving internal detail about those methods and data.


Encapsulation is placing the data and the functions that work on that data in the same place. While working with procedural languages, it is not always clear which functions work on which variables but object-oriented programming provides you framework to place the data and the relevant functions together in the same object.


One of the most useful aspects of object-oriented programming is code reusability. As the name suggests Inheritance is the process of forming a new class from an existing class that is from the existing class called as base class, new class is formed called as derived class.

This is a very important concept of object-oriented programming since this feature helps to reduce the code size.


The ability to use an operator or function in different ways in other words giving different meaning or functions to the operators or functions is called polymorphism. Poly refers to many. That is a single function or an operator functioning in many ways different upon the usage is called polymorphism.


The concept of overloading is also a branch of polymorphism. When the exiting operator or function is made to operate on new data type, it is said to be overloaded.

Review of C

The C language  exhibits the following characteristics:

  1. There is a small, fixed number of keywords, including a full set of control flow primitives: if/else, for, do/while, while, and switch. User-defined names are not distinguished from keywords by any kind of sigil.
  2. There are a large number of arithmetic, bitwise and logic operators: +, +=, ++, &, ||, etc.
  3. More than one assignment may be performed in a single statement.
  4. Function return values can be ignored when not needed.
  5. Typing is static, but weakly enforced; all data has a type, but implicit conversions are possible.
  6. Declaration syntax mimics usage context. C has no “define” keyword; instead, a statement beginning with the name of a type is taken as a declaration. There is no “function” keyword; instead, a function is indicated by the parentheses of an argument list.
  7. User-defined (typedef) and compound types are possible.
  8. Heterogeneous aggregate data types (struct) allow related data elements to be accessed and assigned as a unit.
  9. Union is a structure with overlapping members; only the last member stored is valid.
  10. Array indexing is a secondary notation, defined in terms of pointer arithmetic. Unlike structs, arrays are not first-class objects: they cannot be assigned or compared using single built-in operators. There is no “array” keyword in use or definition; instead, square brackets indicate arrays syntactically, for example month[11].
  11. Enumerated types are possible with the enum keyword. They are freely interconvertible with integers.
  12. Strings are not a distinct data type, but are conventionally implemented as null-terminated character arrays.

Relations to other languages

Many later languages have borrowed directly or indirectly from C, including C++, C#, Unix’s C shell, D, Go, Java, JavaScript, Limbo, LPC, Objective-C, Perl, PHP, Python, Rust, Swift, Verilog and SystemVerilog (hardware description languages).[5] These languages have drawn many of their control structures and other basic features from C. Most of them (Python being a dramatic exception) also express highly similar syntax to C, and they tend to combine the recognizable expression and statement syntax of C with underlying type systems, data models, and semantics that can be radically different.


C Programming Language

1. C language was developed in 1972 by Dennis Ritchie.

2.  C is a Procedural Oriented Programming language.

3. C has Top Down programming approach.

4. In c programming language, a big problem divides into small pieces known as functions; this approach of programming is known as Modular programming.

5. Structure in C does not have function declaration features i.e. we cannot declare a function as member function of structure in C.

C++ Programming Language

1. C++ language was developed in 1980 by BjarneStrostroup.

2. C++ is an Object Oriented Programming language.

3.  C++ has Bottom Up programming approach.

4. In c++ programming language, a big problem divides into Classes and Objects.

5. Structure in C++ provide the feature of declare a function as member function of structure.

Image result for Difference between c and c++

Cin and Cout

The Standard Output Stream (cout)

The predefined object cout is an instance of ostream class. The cout object is said to be “connected to” the standard output device, which usually is the display screen. The cout is used in conjunction with the stream insertion operator, which is written as << which are two less than signs as shown in the following example.Live Demo

#include <iostream>

using namespace std;

int main() {
char str[] = "Hello C++";

cout << "Value of str is : " << str << endl;

When the above code is compiled and executed, it produces the following result −

Value of str is : Hello C++

The C++ compiler also determines the data type of variable to be output and selects the appropriate stream insertion operator to display the value. The << operator is overloaded to output data items of built-in types integer, float, double, strings and pointer values.

The insertion operator << may be used more than once in a single statement as shown above and endl is used to add a new-line at the end of the line.

The Standard Input Stream (cin)

The predefined object cin is an instance of istream class. The cin object is said to be attached to the standard input device, which usually is the keyboard. The cin is used in conjunction with the stream extraction operator, which is written as >> which are two greater than signs as shown in the following example.Live Demo

#include <iostream>

using namespace std;

int main() {
char name[50];

cout << "Please enter your name: ";
cin >> name;
cout << "Your name is: " << name << endl;


When the above code is compiled and executed, it will prompt you to enter a name. You enter a value and then hit enter to see the following result −

Please enter your name: cplusplus
Your name is: cplusplus

The C++ compiler also determines the data type of the entered value and selects the appropriate stream extraction operator to extract the value and store it in the given variables.

The stream extraction operator >> may be used more than once in a single statement. To request more than one datum you can use the following −

cin >> name >> age;

This will be equivalent to the following two statements −

cin >> name;
cin >> age;

New and delete operator

The new operator

The new operator requests for the memory allocation in heap. If the sufficient memory is available, it initializes the memory to the pointer variable and returns its address.

Here is the syntax of new operator in C++ language,

pointer_variable = new datatype;

Here is the syntax to initialize the memory,

pointer_variable = new datatype(value);

Here is the syntax to allocate a block of memory,

pointer_variable = new datatype[size];

Here is an example of new operator in C++ language,

#include <iostream>
using namespace std;
int main () {
   int *ptr1  = NULL; 
   ptr1 = new int; 
   float *ptr2 = new float(223.324);
   int *ptr3 = new int[28];
   *ptr1 = 28; 
   cout << "Value of pointer variable 1 : " << *ptr1 << endl;
   cout << "Value of pointer variable 2 : " << *ptr2 << endl;
   if (!ptr3) 
      cout << "Allocation of memory failed\n"; 
   else { 
      for (int i = 10; i < 15; i++) 
         ptr3[i] = i+1; 
      cout << "Value of store in block of memory: "; 
      for (int i = 10; i < 15; i++) 
         cout << ptr3[i] << " "; 
   return 0;
Value of pointer variable 1 : 28
Value of pointer variable 2 : 223.324
Value of store in block of memory: 11 12 13 14 15

The delete operator

The delete operator is used to deallocate the memory. User has privilege to deallocate the created pointer variable by this delete operator.

Here is the syntax of delete operator in C++ language,

delete pointer_variable;

Here is the syntax to delete the block of allocated memory,

delete[ ] pointer_variable;

Here is an example of delete operator in C++ language,

#include <iostream>
using namespace std;
int main () {
   int *ptr1  = NULL; 
   ptr1 = new int; 
   float *ptr2 = new float(299.121);
   int *ptr3 = new int[28];
   *ptr1 = 28; 
   cout << "Value of pointer variable 1 : " << *ptr1 << endl;
   cout << "Value of pointer variable 2 : " << *ptr2 << endl;
   if (!ptr3) 
      cout << "Allocation of memory failed\n"; 
   else { 
      for (int i = 10; i < 15; i++) 
         ptr3[i] = i+1; 
      cout << "Value of store in block of memory: "; 
      for (int i = 10; i < 15; i++) 
         cout << ptr3[i] << " "; 
   delete ptr1;
   delete ptr2;
   delete[] ptr3;
   return 0;
Value of pointer variable 1 : 28
Value of pointer variable 2 : 299.121
Value of store in block of memory: 11 12 13 14 15


Operators are special type of functions, that takes one or more arguments and produces a new value. For example : addition (+), substraction (-), multiplication (*) etc, are all operators. Operators are used to perform various operations on variables and constants.

Types of operators

1. Assignment Operator

2. Mathematical Operators

3. Relational Operators

4. Logical Operators

5. Bitwise Operators

6. Shift Operators

7. Unary Operators

8. Ternary Operator

9. Comma Operator

Assignment Operator(=)

Operates ‘=’ is used for assignment, it takes the right-hand side (called rvalue) and copy it into the left-hand side (called lvalue). Assignment operator is the only operator which can be overloaded but cannot be inherited.

Mathematical Operators

There are operators used to perform basic mathematical operations. Addition (+) , subtraction (-) , diversion (/) multiplication (*) and modulus (%) are the basic mathematical operators. Modulus operator cannot be used with floating-point numbers.

Relational Operators

These operators establish a relationship between operands. The relational operators are : less than (<) , grater thatn (>) , less than or equal to (<=), greater than equal to (>=), equivalent (==) and not equivalent (!=). You must notice that assignment operator is (=) and there is a relational operator, for equivalent (==). These two are different from each other, the assignment operator assigns the value to any variable, whereas equivalent operator is used to compare values, like in if-else conditions.

Logical Operators

The logical operators are AND (&&) and OR (||). They are used to combine two different expressions together.

If two statement are connected using AND operator, the validity of both statements will be considered, but if they are connected using OR operator, then either one of them must be valid. These operators are mostly used in loops (especially while loop) and in Decision making.

Bitwise Operators

There are used to change individual bits into a number. They work with only integral data types like char, int and long and not with floating point values.

  • Bitwise AND operators &
  • Bitwise OR operator |
  • And bitwise XOR operator ^
  • And, bitwise NOT operator ~

They can be used as shorthand notation too = , |= , ^= , ~= etc.

Shift Operators

Shift Operators are used to shift Bits of any variable. It is of three types,

  1. Left Shift Operator <<
  2. Right Shift Operator >>
  3. Unsigned Right Shift Operator >>>

Unary Operators

These are the operators which work on only one operand. There are many unary operators, but increment ++ and decrement — operators are most used.

Other Unary Operators : address of &, dereference *, new and delete, bitwise not ~, logical not !, unary minus – and unary plus +.

Ternary Operator

The ternary if-else ? : is an operator which has three operands.

Comma Operator

This is used to separate variable names and to separate expressions. In case of expressions, the value of last expression is produced and used.