Introduction to C++ Programming Language

Introduction to C++ Programming Language

C++ is a language that supports object-oriented programming paradigm. C++ emphasis on data rather than procedure. 

You can easily learn the C++ programming language by understanding C and some advanced features of the OOP concept. C++ programs are divided into objects and classes.


Why Learn C++?

1. It is one of the most popular languages in the programming world.
2. It is hard to be replaced by others because it has so many features.
3. It is portable which means any programs written in it can easily be moved.
4. It is a high-speed programming language in terms of execution.
5. It is used in the graphics field due to its high speed.
6. Different web browsers are developed using C++ like Google Chrome, Firefox, Safari, etc as it gives speed that is required for rendering.
7. As it supports low-level language, it is used in compilers.
8. It is used in developing OS (Operating Systems) like Windows, Linux, Ubuntu, iOS, Android, etc.
9. It is used in developing databases like MySQL, MongoDB, etc.


History of C++ Language

1. C++ is an object-oriented extension of C language.
2. C++ was designed by Bjarne Stroustrup at Bell Lab.
3. Initially, it was called "C with classes."
4. Later in 1983 by adding features of OOP's with C language named it as C++.
5. This OOP's concept was influenced by the contemporary language called Simula 67.
6. There are several features available which are added gradually such as Classes, templates, exceptions, multiple inheritance, etc.
7. Conventional Programming, using high-level programming such as COBOL, Fortran which are known as Procedure Oriented Programming (POP).



Difference Between C and C++

1. C is a subset of C++ when compared to C++ while C++ is a superset of C. C++ can run most of the C code while C cannot run C++ code.
2. C supports the procedural programming paradigm for code development while C++ supports both procedural programming and object-oriented programming paradigm. That's why C++ also called a hybrid language.
3. In C data and functions are separate and are free entities but In C++ data and functions are encapsulated together in the form of an object.
4. C uses functions for input/output e.g., scanf and printf while C++ uses objects for input/output e.g., cin and cout.
5. C provides malloc and calloc function for Dynamic Memory Allocation(DMA) and free for memory deallocation while C++ provides a new operator for memory allocation and delete operation for memory deallocation.
6. C does not provide direct support for error handling (also called exception handling) but C++ provides support for exception handling. Exceptions are practiced for "hard" errors that cause the code wrong.

Features of C++

1. It is a general-purpose language used in Games, GUI based applications, image editing applications, web browsers, compilers, OS, etc.
2. It is a middle-level language.
3. It is a compiled language which makes it efficient and fast.
4. It is an object-oriented programming language.
5. C++ supports Object-Oriented Programming (OOP). OOP treats data as a critical element in the program and does not allow free. 
6. It ties data more closely to the functions that operate on it and also protects it from modification from outside function. 
7. OOP approach provides a way of programming by creating partitioned memory for both data and functions.



Characteristics of OOP

1. OOP emphasis on data rather than procedure.
2. Programs are divided into objects and classes.
3. Data and Functions are tied together in the data structure.
4. Data is invisible and cannot be accessed by outsider functions.
5. Objects may interact with each other by making use of functions.
6. New data and functions can be easily added whenever needed.
7. It follows a bottom-up approach.

Features of Object-Oriented Programming

  • Objects.
  • Classes.
  • Data abstraction and Encapsulation.
  • Inheritance.
  • Polymorphism.
  • Dynamic Binding.
  • Message Passing.
Structure of C++ Program

  1. C programming extension is .C
  2. C++ programming extension is .CPP

Inputs and Outputs in C++

Input in C++

cin: cin object in C++ is an object of class istream. It is used to accept the input from the standard input device (like keyboard). It is linked with the standard C input stream stdin.


The "c" in cin means "character" and "in" means "input" and therefore cin means character input. the cin object is used along with the extraction operator ( >> ) in order to get a stream of characters.

The general syntax is:
cin>>varName1>>varName2>>varName3;

Example: inputs and outputs in c++



#include<iostream.h>
#include<conio.h>
int main()
 {
  int a,b;

  cout<<"Enter a number \n";
  cin>>a;

  cout<<"Enter another number \n";
  cin>>b;

  cout<<"Sum is:"<<a+b;

  return 1;
 }

Output:

Enter a number
12
Enter another number
20
Sum is: 32

Output in C++

cout: The cout object in C++ is an object of class ostream. It is used to display the output to the standard output device i.e., monitor.

The "c" in cout refers to "character" and "out" refers to "character output". The cout object is used along with the insertion operator ( << ) in order to display a stream of characters.

The general syntax is:
cout<<"string"<<varName<<"string"<<varName;



Example: inputs and outputs in c++

#include<iostream.h>
#include<conio.h>
int main()
 {
  cout<<"Learning Mania: Learn New Things Everyday";

  return 1;
 }


Output:

Learning Mania: Learn New Things Everyday

Compilation Process in C++

Compilation-Process-in-C++


















Keywords in C++

  1.  auto
  2.  break
  3.  case
  4.  char
  5.  const
  6.  continue
  7.  default
  8.  do
  9.  double
  10.  else
  11.  num
  12.  extern
  13.  float
  14.  for
  15.  goto
  16.  if
  17.  int
  18.  long
  19.  register
  20.  return
  21.  short
  22.  signed
  23.  sizeof
  24.  static
  25.  struct
  26.  switch
  27.  typedef
  28.  union
  29.  unsigned
  30.  void
  31.  volatile
  32.  while

Identifiers in C++

  • Identifiers are nothing but the name of functions, variables, classes, arrays, etc which you make while writing your programs so that you can recognize it later by the same name given to it.
  • Identifiers are just like names in any language.
  • There are some rules which need to follow while naming identifiers in C++.
  • C++ is case-sensitive means that Uppercase Letters and Lower Case letters are different from each other.
  • The name of an identifier never starts with a digit. However, underscore ( _ ) can be used at the starting of any identifier.
  • No reserved words (Keywords) are used as an identifier. Only alphabetic letters, numbers (digits) and underscore (_) are allowed in C++ language for naming identifiers.

Scope of Variable in C++ 

The scope of variables may be classified as:

  1. Local Variables.
  2. Global Variables.

Local variables are declared inside a function or block and can only be used by statements specified inside the same function or block of code. They are not known to outsider functions. for example:


#include<iostream.h>
int gone(int,int);
void main()
 {
  int b;                 // Local variable
  int s = 6, u = 8;      // Local variable
  b = gone (s, u);

  cout<<"\n The Output is: "<<b;

  int gone(int x, int y)
  {
   int z;
   z = x+y;
   return z;
  }
 }


Global variables can be used by the functions or by any other external functions. for example:


#include<iostream.h>
int x, y, z;               // Global Variable
float m, n, f;             // Global Variable
void main()
 {
  int s, u;               // Local Variable
  float w, j;             // Local Variable
  ...
  ..
  .
 }

Previous
Next Post »

Comment Below: ConversionConversion EmoticonEmoticon