C++

Introduction

C++, as we all know is an extension to C language and was developed by Bjarne stroustrup at bell labs. C++ is an intermediate level language, as it comprises a confirmation of both high level and low level language features. C++ is a statically typed, free form, multiparadigm, compiled general-purpose language.

C++ is an Object Oriented Programming language but is not purely Object Oriented. Its features like Friend and Virtual, violate some of the very important OOPS features, rendering this language unworthy of being called completely Object Oriented. Its a middle level language.

Applications of C++

Mainly C++ Language is used for Develop Desktop application and system software. Some application of C++ language are given below.

  • For Develop Graphical related application like computer and mobile games.
  • To evaluate any kind of mathematical equation use C++ language.
  • C++ Language are also used for design OS. Like window xp.
  • Google also use C++ for Indexing
  • Few parts of apple OS X are written in C++ programming language.
  • Internet browser Firefox are written in C++ programming language
  • All major applications of adobe systems are developed in C++ programming language. Like Photoshop, ImageReady, Illustrator and Adobe Premier.
  • Some of the Google applications are also written in C++, including Google file system and Google Chromium.
  • C++ are used for design database like MySQL.

Benefits of C++ over C Language

The major difference being OOPS concept, C++ is an object oriented language whereas C language is a procedural language. Apart form this there are many other features of C++ which gives this language an upper hand on C laguage.

Following features of C++ makes it a stronger language than C,

  1. There is Stronger Type Checking in C++.
  2. All the OOPS features in C++ like Abstraction, Encapsulation, Inheritance etc makes it more worthy and useful for programmers.
  3. C++ supports and allows user defined operators (i.e Operator Overloading) and function overloading is also supported in it.
  4. Exception Handling is there in C++.
  5. The Concept of Virtual functions and also Constructors and Destructors for Objects.
  6. Inline Functions in C++ instead of Macros in C language. Inline functions make complete function body act like Macro, safely.
  7. Variables can be declared anywhere in the program in C++, but must be declared before they are used.

Turbo C++ – Download & Installation

There are many compilers available for C++. You need to download any one. Here, we are going to use Turbo C++. It will work for both C and C++. To install the Turbo C++ software, you need to follow following steps.

  1. Download Turbo C++
  2. Create turboc directory inside c drive and extract the tc3.zip inside c:\turboc
  3. Double click on install.exe file
  4. Click on the tc application file located inside c:\TC\BIN to write the c program
  1. Download Turbo C++ software

You can download turbo C++ from many sites. download Turbo c++

2) Create turboc directory in c drive and extract the tc3.zip

Now, you need to create a new directory turboc inside the c: drive. Now extract the tc3.zip file in c:\turboc directory.

3) Double click on the install.exe file and follow steps

Now, click on the install icon located inside the c:\turboc

Cpp Installation 1

It will ask you to install c or not, press enter to install.

Cpp Installation 2

Change your drive to c, press c

Cpp Installation 3

Press enter, it will look inside the c:\turboc directory for the required files.

Cpp Installation 4

Select Start installation by the down arrow key then press enter.

Cpp Installation 5

Now C is installed, press enter to read documentation or close the software.

Cpp Installation 6

4) Click on the tc application located inside c:\TC\BIN

Now double click on the tc icon located in c:\TC\BIN directory to write the c program.

Cpp Installation 7

In windows 7 or window 8, it will show a dialog block to ignore and close the application because full screen mode is not supported. Click on Ignore button.

Now it will showing following console.

Cpp Installation 8

C++ Program

Before starting the abcd of C++ language, you need to learn how to write, compile and run the first C++ program.

To write the first C++ program, open the C++ console and write the following code:

#include <iostream.h>  
#include<conio.h>  
void main() {  
   clrscr();  
   cout << "Welcome to C++ Programming.";   
   getch();  
}  

#include<iostream.h> includes the standard input output library functions. It provides cin and cout methods for reading from input and writing to output respectively.

#include<iostream.h> includes the standard input output library functions. It provides cin and cout methods for reading from input and writing to output respectively.

#include <conio.h> includes the console input output library functions. The getch() function is defined in conio.h file.

void main() The main() function is the entry point of every program in C++ language. The void keyword specifies that it returns no value.

cout << “Welcome to C++ Programming.” is used to print the data “Welcome to C++ Programming.” on the console.

getch() The getch() function asks for a single character. Until you press any key, it blocks the screen.

Cpp Program 1

How to compile and run the C++ program

There are 2 ways to compile and run the C++ program, by menu and by shortcut.

By menu

Now click on the compile menu then compile sub menu to compile the c++ program.

Then click on the run menu then run sub menu to run the c++ program.

By shortcut

Or, press ctrl+f9 keys compile and run the program directly.

You will see the following output on user screen.

Cpp Program 2

You can view the user screen any time by pressing the alt+f5 keys.

Now press Esc to return to the turbo c++ console.

C++ Basic Input/Output

C++ I/O operation is using the stream concept. Stream is the sequence of bytes or flow of data. It makes the performance fast.

If bytes flow from main memory to device like printer, display screen, or a network connection, etc, this is called as output operation.

If bytes flow from device like printer, display screen, or a network connection, etc to main memory, this is called as input operation.

I/O Library Header Files

Let us see the common header files used in C++ programming are:

Header FileFunction and Description
<iostream>It is used to define the cout, cin and cerr objects, which correspond to standard output stream, standard input stream and standard error stream, respectively.
<iomanip>It is used to declare services useful for performing formatted I/O, such as setprecision and setw.
<fstream>It is used to declare services for user-controlled file processing.

Standard output stream (cout

The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console

Let’s see the simple example of standard output stream (cout):

#include <iostream>  
using namespace std;  
int main( ) {  
   char ary[] = "Welcome to new techie language";  
   cout << "Value of ary is: " << ary << endl;  
}  

Output:

Value of ary is: Welcome to new techie language

Standard input stream (cin)

The cin is a predefined object of istream class. It is connected with the standard input device, which is usually a keyboard. The cin is used in conjunction with stream extraction operator (>>) to read the input from a console.

Let’s see the simple example of standard input stream (cin):

#include <iostream>  
using namespace std;  
int main( ) {  
  int age;  
   cout << "Enter your age: ";  
   cin >> age;  
   cout << "Your age is: " << age << endl;  
}  

Output

Enter your age: 22
Your age is: 22

Standard end line (endl)

The endl is a predefined object of ostream class. It is used to insert a new line characters and flushes the stream.

Let’s see the simple example of standard end line (endl):

#include <iostream>  
using namespace std;  
int main( ) {  
cout << "new techie language";     
cout << " Javatpoint"<<endl;   
cout << "End of line"<<endl;   
}   

Output

New techie language Javatpoint 
End of line

C++ Variable

A variable is a name of memory location. It is used to store data. Its value can be changed and it can be reused many times.

It is a way to represent memory location through symbol so that it can be easily identified.

Let’s see the syntax to declare a variable

type variable_list; 

The example of declaring variable is given below:

int x;    
float y;    
char z;    

Here, x, y, z are variables and int, float, char are data types.

We can also provide values while declaring the variables as given below:

int x=5,b=10;  //declaring 2 variable of integer type    
float f=30.8;    
char c='A';    

Rules for defining variables

A variable can have alphabets, digits and underscore.

A variable name can start with alphabet and underscore only. It can’t start with digit.

No white space is allowed within variable name.

A variable name must not be any reserved word or keyword e.g. char, float etc.

Valid variable names:

int a;    
int _ab;    
int a30

Invalid variable names:

int 4;    
int x y;    
int double;

C++ Data Types

A data type specifies the type of data that a variable can store such as integer, floating, character etc.

Cpp Data typews 1

There are 4 types of data types in C++ language.

TypesData Types
Basic Data Typeint, char, float, double, etc
Derived Data Typearray, pointer, etc
Enumeration Data Typeenum
User Defined Data Typestructure

Basic Data Types

The basic data types are integer-based and floating-point based. C++ language supports both signed and unsigned literals.

The memory size of basic data types may change according to 32 or 64 bit operating system.

Let’s see the basic data types. It size is given according to 32 bit OS.

Data TypesMemory SizeRange
char1 byte-128 to 127
signed char1 byte-128 to 127
unsigned char1 byte0 to 127
short2 byte-32,768 to 32,767
signed short2 byte-32,768 to 32,767
unsigned short2 byte0 to 32,767
int2 byte-32,768 to 32,767
signed int2 byte-32,768 to 32,767
unsigned int2 byte0 to 32,767
short int2 byte-32,768 to 32,767
signed short int2 byte-32,768 to 32,767
unsigned short int2 byte0 to 32,767
long int4 byte
signed long int4 byte
unsigned long int4 byte
float4 byte
double8 byte
long double10 byte

C++ Keywords

A keyword is a reserved word. You cannot use it as a variable name, constant name etc. A list of 32 Keywords in C++ Language which are also available in C language are given below.

autobreakcasecharconstcontinuedefaultdo
doubleelseenumexternfloatforgotoif
intlongregisterreturnshortsignedsizeofstatic
structswitchtypedefunionunsignedvoidvolatilewhile

A list of 30 Keywords in C++ Language which are not available in C language are given below.

asmdynamic_castnamespacereinterpret_castbool
explicitnewstatic_castfalsecatch
operatortemplatefriendprivateclass
thisinlinepublicthrowconst_cast
deletemutableprotectedtruetry
typeidtypenameusingvirtualwchar_t
Design a site like this with WordPress.com
Get started