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,
- There is Stronger Type Checking in C++.
- All the OOPS features in C++ like Abstraction, Encapsulation, Inheritance etc makes it more worthy and useful for programmers.
- C++ supports and allows user defined operators (i.e Operator Overloading) and function overloading is also supported in it.
- Exception Handling is there in C++.
- The Concept of Virtual functions and also Constructors and Destructors for Objects.
- Inline Functions in C++ instead of Macros in C language. Inline functions make complete function body act like Macro, safely.
- 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.
- Download Turbo C++
- Create turboc directory inside c drive and extract the tc3.zip inside c:\turboc
- Double click on install.exe file
- Click on the tc application file located inside c:\TC\BIN to write the c program
- 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
It will ask you to install c or not, press enter to install.
Change your drive to c, press c
Press enter, it will look inside the c:\turboc directory for the required files.
Select Start installation by the down arrow key then press enter.
Now C is installed, press enter to read documentation or close the software.
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.
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.
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.
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.
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 File | Function 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.
There are 4 types of data types in C++ language.
Types | Data Types |
---|---|
Basic Data Type | int, char, float, double, etc |
Derived Data Type | array, pointer, etc |
Enumeration Data Type | enum |
User Defined Data Type | structure |
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 Types | Memory Size | Range |
---|---|---|
char | 1 byte | -128 to 127 |
signed char | 1 byte | -128 to 127 |
unsigned char | 1 byte | 0 to 127 |
short | 2 byte | -32,768 to 32,767 |
signed short | 2 byte | -32,768 to 32,767 |
unsigned short | 2 byte | 0 to 32,767 |
int | 2 byte | -32,768 to 32,767 |
signed int | 2 byte | -32,768 to 32,767 |
unsigned int | 2 byte | 0 to 32,767 |
short int | 2 byte | -32,768 to 32,767 |
signed short int | 2 byte | -32,768 to 32,767 |
unsigned short int | 2 byte | 0 to 32,767 |
long int | 4 byte | |
signed long int | 4 byte | |
unsigned long int | 4 byte | |
float | 4 byte | |
double | 8 byte | |
long double | 10 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.
auto | break | case | char | const | continue | default | do |
double | else | enum | extern | float | for | goto | if |
int | long | register | return | short | signed | sizeof | static |
struct | switch | typedef | union | unsigned | void | volatile | while |
A list of 30 Keywords in C++ Language which are not available in C language are given below.
asm | dynamic_cast | namespace | reinterpret_cast | bool |
explicit | new | static_cast | false | catch |
operator | template | friend | private | class |
this | inline | public | throw | const_cast |
delete | mutable | protected | true | try |
typeid | typename | using | virtual | wchar_t |