Category Archives: C++ Tutorial

C++ Program Structure

C++ Program Structure

In C++, like any other programming language we have a specification available for program structure, every C++ program is consist of following building blocks –

cpp-program-structure

Documentation Section :- Documentation section is generally meant include set of comments, that is used to provide the information about the program written like name of a program, utility of program, date of creation, date of last modification ,author name, licensing or copyrights information and any other information that programmer wish to put for the references.

Example:-

Preprocessor Statements:- This is the section where we write all of the preprocessor directive statements, preprocessor statement usually with begins with a pound (#) symbol. Preprocessor statements are processed first when the compilation of the program begins. Preprocessor statements such #include tells compiler to include header files and #define directive is used to define constants prior to compilation of the program. Preprocessor statements are further categories as below –

Link Preprocessor :- These preprocessor directive tell the compiler to link the functions from system library.

Example:

Definition Preprocessor :- This the section where we define all our symbolic constant we gonna use in our program.

Example:

Global Declarations :- This is the section where all the global declaration comes. All of the variables, structures, classed and function defined or declared outside the main function are treated as global.

Class Definition :- The classes are used to map real world entities into programming. The Classes are the key building block of any C++ program. A C++ program may include several class definitions. This is the section where we define all of our classes.

Main Method Definition :- This is the most vital part of each and every C++ program, it is mandatory for C++ program to have a main() method definition. There can be only one main() method in C++ program. The execution of a C++ program starts with the main() method. The C++ program can not be executed without the main() method. The main() method is responsible for the execution of all the user defined statement, functions and library functions. The main() method further structured into – variable declaration, function declaration and user defined executable statements.

User defined functions :- This is the section of where we put all the user defined functions created to perform a specific task. A user defined function must be defined before use it. User defined function can written before or immediately after the main ( ) function and called inside the main ( ) function.

C++ Installation

C++ Installation

Installing C++ In Windows

In order to set up a C++ Programming environment you need to have C++ Compiler installed on your system, so if you don’t have it installed, check the following instruction to get C++ installation. If you’ve already have installed C++ in your system, you can skip this part.

Step 1:- Download the turbo C++ online from any of the safe websites or directly from the link given below –

Download turbo C++

Step 2:- Create a folder “turbo” in c:\ Drive and extract the tc3.zip file in c:\turboc directory.

Step 3:- Now, start the installation by double-clicking the INSTALL Application file in the c:\turboc directory.

Step 4:- Follow the installation wizard to install Turbo C++. Press “Enter” here to start the installation.

cpp-installation-11

Step 5:- Now, change or select installation directory to c:\ and press “Enter”

cpp-installation-2

Step 6:- Now, select the c:\turboc directory that we have created containing required installation files, and press “Enter”

cpp-installation-3

Step 7:- Now, select Start Installation using the down arrow key and then press “Enter”.

cpp-installation-4

Step 8:- Installation is done, press enters to read the documentation and close the installation wizard.

cpp-installation-5

Step 9:- Now you can begin writing a c/c++ program by starting the tc application located in c:\TC\BIN directory. This will application window as below.

cpp-installation-6

Installing C++ In Ubuntu/Linux

Here is how you can install C++ in Linux –

Step 1:- Open a terminal and use the following command to install the C++ compiler and essential tools –

If you are on Ubuntu and Debian use the following command

If you are on CentOS, Fedora and Redhat use the following command

Step 2:- Let’s verify the installation using the following command

Compile and Run C++ Program In Ubuntu/Linux

Step 1:- Create a file using a text editor program of your choice and save it with the .cpp extension. The .cpp file extension is used to specify C++ file.

Example:-

hello.cpp

Step 2:- Now, switch to the directory where the file is located. And, issue the following command to compile the C++ program –

Here, replace program-file-name. cpp with your c++ program file name and program-name with any name of your choice.

Example:-

Step 3:- If there is no compilation error, an executable file is created and you can see the output using the following command

Example:-

Installing C++ In Mac OS

Download and Install Xcode development environment to use GNU C/C++ compiler from Apple’s official website. the link is given below –

C++ Features

C++ Features

C++ (also read as C plus) is a general-purpose object-oriented programming language. C++ is an enhanced version of the C language introduced with many new c++  features and is best known as a super-set of C language. It is a very powerful programming language that helps programmers write a variety of programs. Here we list some of the important features of C++

Simple:-

C++ comes with easy-to-learn simple English language like syntax, which makes it very easy to understand and develop C++ Programs.

High Performance:-

Being a statically typed language, C++ is generally performed more than any dynamically typed language because the code is type-checked before it is executed.

Statically Typed:-

C++ is a statically typed programming language, which means type checking is performed at compile-time instead of run-time. This allows the compiler to catch the bugs before executing the program.

Multi-Paradigm:-

C++ is a multi-paradigm object-oriented programming language that supports procedural, object-oriented (OOPs), and generic programming whereas C is a procedural programming language.

Object-Oriented:-

C++ is a general-purpose, multi-paradigm object-oriented programming language that supports procedural, object-oriented (OOPs), and generic programming whereas C is a procedural programming language. In compliance with four of the major OOPs principles Inheritance, Polymorphism, Encapsulation, and Abstraction.

Standard Template Library(STL):-

C++ comes with a rich inbuilt function library through Standard Template Library (STL).

Pointers:-

C++ supports the use of pointers which are often not available in several programming languages.

Portable:-

C++ is a Portable language, which allows you to write a program irrespective of the operating system as well as Hardware. Programs written in C++ can be executed in many machines with little bit or no change. C++ is a highly portable language that supports multi-device, multi-platform application development.

Reusable:-

C++ comes with an increased level of code re-usability due to the object-oriented programming model and libraries.

Exception handling:-

C++ supports exception handling which was not possible in C.

Memory Management:-

C++ supports dynamic memory allocation, which gives users complete control over memory management. In C++ language, we can be allocated and free up memory dynamically at any time.

Case Sensitive:-

C++ is a case-sensitive programming language. In C++ programming “abc” and “ABC” both are different.

Compiled:-

C++ has compiled language, which means C++ source code needs to be first compiled into low-level code, and then it can be executed, unlike interpreted programming languages where the compilation is not needed.

Platform Dependent:-

C++ is a platform-dependent language. A language is said to be platform-dependent whenever the program is executing in the same operating system where that was developed and compiled but not run and execute on another operating system.

Scalable:-

C++ becomes a preferred choice for applications where high concurrency, multithreading, and load tolerance are required. As C++ is close to the hardware and comes with multithreading libraries, that provide high concurrency and load tolerance and make C++ highly scalable.

Community:-

With C++ you will get a huge community of active members all around the world. C++ is the 6th most used and followed tag on StackOverflow and GitHub.

C++ History

C++ History

Development of C++ programming language started back in 1979 when Bjarne Stroustrup working for his Ph.D. thesis; he had the opportunity to work with a programming language called Simula. Simula is a programming language primarily designed for simulations. Simula was first language to support object-oriented programming paradigm. While working with Simula Bjarne Stroustrup found that this object-oriented programming paradigm can be very helpful in software development.

Soon thereafter, he started to work on a project called “C with Classes”, which was basically aimed to create a superset of C Programming Language with implementation of object-oriented programming features without sacrificing speed or low-level functionality. The new language he is been working implemented some add-on features such as classes, basic inheritance, in-lining, default function arguments, and strong type checking in addition to all the features of the C language.

The first compiler for C with Classes was named as Cfront, which was basically derived from a C compiler called CPre. Cfront was a program designed to translate C with Classes code to ordinary C. Later in 1993, Cfront was discontinued as it became difficult to integrate new features into it.

1983:- In 1983, “C with Classes” was renamed to “C++“, adding new features that included virtual functions, function name and operator overloading, references, constants, type-safe free-store memory allocation (new/delete), improved type checking, and BCPL style single-line comments with two forward slashes (//). Furthermore, it included the development of a standalone compiler for C++

1985:- The first commercial edition of C++ Programming Language was released. This was not yet a standardized version of language.

1989:- The official standard version 2.0 was released.

1990:- Borland’s Turbo C++ was released as a commercial product, this added a many additional libraries that impacted C++’s development considerably.

1998:- The standard committee of C++ released the first international standard for language ISO C++ 98.

2003:- The revised version of C++98 with lots of fixes was dubbed as C++03.

2011:- The C++11 standard was released with new features, extending the standard library further, and providing more facilities to C++ programmers.

2014 :- The revised version of C++11 with bug fixes and some improvements was released as C++14.

2017 :- C++17 is the current iteration of C++ ,released in 2017 with many new features.

Who developed C++?

The C++ Programming Language was developed by Bjarne Stroustrup, he developed C++ at AT&T Bell Labs, he headed the development of language till 2002.

cpp_bjarne-stroustrup-pics

Bjarne Stroustrup (The Creator of C++)

C vs C++

C vs C++

What is the difference between C and C++?

The major difference between C and C++ is that C is a procedural programming language and does not support the concept of classes and objects, while C++ is a combination of both procedural and object-oriented programming languages. C++ is considered a superset of C, which means C++ includes features of C programming language and in addition their own features.

Difference between C and C++

C C++
C is a procedural programming language. C++ is a Object-Oriented Programming language.
C follows Top Down programming approach C++ follows Bottom Up programming approach.
The extension of a C program file is .c The extension of a c+ + program language is .cpp
C is a function driven language. C++ is an object driven language.
In C, structure does not support declaring of function as its member While, in C++ structure provides the feature of declaring a function as a member function.
C does not support inline function. C++ supports inline function.
In C scanf and printf functions are used for standard input and output operation In C++ cin and cout objects are for standard input and output operations.‬ ‬‬‬
C is not compatible with another language. C++ is compatible with the other generic programming languages.
C does not support operator overloading. C++ support operator overloading.
In C, the variable should be defined at the beginning of the program. C++ allows you to declare variables anywhere in the function.
C does not support reference variables C++ support reference variables
C does not allow you to use function overloading. C++ allows you to use function overloading.
C language does not allows you to declare String or Boolean data types. It supports built-in and primitive data types. C++ supports String and Boolean data types.
C does not support Exception Handling. However, it can be performed using some workarounds. C++ supports Exception handling. Moreover, this operation can be performed using try and catch block.
Does not allows functions with default arrangements Allow functions with default arrangements.
C does not have namespace feature. It is present in the C++.
C is a subset of C++. It cannot run C++ code. C++ is a superset of C. C++ can run most of C code.
Does not support encapsulation. As Data and functions are separate and free entities. Supports encapsulation. Data and functions are encapsulated together as an object.
C does not support information hiding. In this language, data are free entities and can be changed outside code. Encapsulation hides the data. So that data structures and operators are used as per intention.
C provide malloc() and calloc() functions for dynamic memory allocation. C++ provides a new operator for this purpose.
C, allows Multiple Declaration of global variables. In C++, Multiple Declaration of global variables are not allowed.
In C, Inheritance is not supported. Inheritance is possible in C++ language.
Virtual Functions are present in C. Virtual Function is not used in C++.
Contain 32 keywords. Contains 52 keywords.
Polymorphism is not possible Polymorphism is one of the most Important Features of OOPS.
C language offers GTK tool for GUI programming C++ supports Qt tools for GUI programming

C++ Introduction

C++ Introduction

The C++ (also read as C plus-plus) is a general-purpose object-oriented programming language developed by Bjarne Stroustrup at Bell Labs in early 1979. C++ is an enhanced version of the C language introduced with many new features and is best known as a super-set of C language. IN C++ introduction, is a multi-paradigm programming language that supports procedural, object-oriented (OOPs), and generic programming whereas C is a procedural programming language. C++ is a statically-typed, free-form, compiled middle-level programming language as it includes a combination of both high-level and low-level language features.

Usage/Applications of C++

C++ is the language which is can be used for a variety of application developments that includes –

  • Operating Systems
  • Client-Server application
  • Device drivers
  • Embedded Systems
  • Database Application/Drivers
  • Robotic
  • Real-Time Simulation and Monitoring Application
  • Animation
  • Games
  • 3D Graphics
  • Distributed System
  • Network Application

Object-Oriented Programming (OOPs)

C++ is based on an object-oriented programming paradigm, it compliance with four of the major OOPs principles –

  • Inheritance
  • Polymorphism
  • Encapsulation
  • Abstraction

Standard Libraries

  • The core library includes the data types, variables, literal, etc.
  • The standard library includes the set of functions manipulating strings, files, etc.
  • The Standard Template Library (STL) includes the set of methods manipulating a data structure.

Who is using C++

C++ is used in many of the large companies belonging to different sectors of the software industry such as Microsoft, Apple, Facebook, Google, Mozilla, Adobe, and Amazon.

For example –

  • Apple OS is written in C++
  • Adobe Products like Photoshop, Illustrator, Flash are developed using C++
  • C++ is used for the development of all of the major web browsers including IE, Google Chrome, and Mozilla Firefox.

Why use C++

C++ is a very powerful, efficient yet very flexible programming language. C++ gives you a lot of control and makes it easy to communicate with system resources such as memory, network, I/O, and Processor. Therefore it is always a preferred choice for the development of high-performance and efficient applications such as – computer games, real-time applications, graphic libraries, system applications.

Benefits of C++

  • C++ is a multi-paradigm programming language that supports procedural, object-oriented (OOPs), and generic programming paradigms.
  • C++ is a highly portable language that supports multi-device, multi-platform application development.
  • C++ allows writing a program irrespective of the operating system as well as Hardware
  • C++ is an object-oriented programming language that supports concepts of classes, inheritance, polymorphism, data abstraction, and encapsulation.
  • C++ comes with a rich function library.
  • C++ supports exception handling which is not possible in C.
  • C++ is a powerful, efficient, and fast language.
  • C++ supports the development of a wide range of applications that includes the development of operating systems, system drivers, browsers, games, 3d graphics, real-time applications.
  • C++ provides performance and memory efficiency.
  • C++ provides a high-level abstraction.
  • C++ is compatible with C.
  • C++ comes with increased reusability of code.

Object Oriented Programming vs Procedural Programming

Object-Oriented Programming vs Procedural Programming

Object-Oriented Programming Procedural Programming
Emphasis on Data; binding of data structures with methods that operate on data Emphasis on algorithms, procedures
Real-world is represented by objects mimicking external entities. Allows modeling of the real-life problems into objects
with state and behavior
Real-world is represented by logical entities and control flow. Tries to fit the real-life problem into procedural language
Data (State) is encapsulated effectively by methods (Behavior) In a given module, data and procedures are separate
Program modules are integrated parts of an overall program. Objects interact with each other by message passing Program modules are linked through a parameter passing mechanism
Uses abstraction at class and object level Uses abstraction at the procedure level
Object-Oriented decomposition focuses on abstracted objects and their interaction Algorithmic decomposition tends to focus on the sequence of events
Active and intelligent data structures (object) encapsulates all passive procedures Passive and dumb data structures used by active methods
Object-Oriented Languages: C++, Small Talk, Java, C#, JavaScript, etc Procedural languages: C, COBOL, PASCAL