Monday, March 4, 2019

Structure of C++ Program

First program is C++

// This is my first program is C++
/* this program will illustrate different components of
a simple program in C++ */

#include <iostream>
using namespace std;
int main()
   {
          cout << "Hello World!";
          return 0;
    }

When the above program is compiled, linked and executed, the following output is displayed on the VDU screen.

Hello World!

Various components of this program are discussed below:

Comments

First three lines of the above program are comments and are ignored by the compiler. Comments are included in a program to make it more readable. If a comment is short and can be accommodated in a single line, then it is started with double slash sequence in the first line of the program. However, if there are multiple lines in a comment, it is enclosed between the two symbols /* and */

#include <iostream>

The line in the above program that start with # symbol are called directives and are instructions to the compiler. The word include with '#' tells the compiler to include the file iostream into the file of the above program. File iostream is a header file needed for input/ output requirements of the program. Therefore, this file has been included at the top of the program. using namespace std; All the elements of the standard C++ library are declared within std. This line is very frequent in C++ programs that use the standard library. 

int main ( )

The word main is a function name. The brackets ( ) with main tells that main ( ) is a function. The word int before main ( ) indicates that integer value is being returned by the function main (). When program is loaded in the memory, the control is handed over to function main ( ) and it is the first function to be executed.

Curly bracket and body of the function main ( )

A C++ program starts with function called main(). The body of the function is enclosed between curly braces. The program statements are written within the brackets. Each statement must end by a semicolon, without which an error message in generated.

cout<<"Hello World!";

This statement prints our "Hello World!" message on the screen. cout understands that anything sent to it via the << operator should be printed on the screen.

return 0;

This is a new type of statement, called a return statement. When a program finishes running, it sends a value to the operating system. This particular return statement returns the value of 0 to the operating system, which means “everything went okay!”.

Printing Multiple Lines of Text with a Single Statement

/* This program illustrates how to print multiple lines of text
with a single statement */

#include <iostream>
using namespace std;
int main()
      {
           cout << "Welcome\nto\nC++";
           return 0;
       }
Output:

Welcome to C++

The characters print exactly as they appear between the double quotes. However, if we type \n, the characters \n are not printed on the screen. The backslash (\) is called an escape character. It indicates that a "special" character is to be output. When a backslash is encountered in a string of characters, the next character is combined with the backslash to form an escape sequence. The escape sequence \n means newline. It causes the cursor to move to the beginning of the next line on the screen.

The following table gives a listing of common escape sequences.

Escape Sequence 
Description

     \n
Newline
     \t
Horizontal tab
     \a
Bell (beep)
     \\
Backslash
     \'
Single quote
     \''
Double quote

Sunday, March 3, 2019

C++ Compiler


Getting Started

          A computer cannot understand our language that we use in our day to day conversations, and likewise, we cannot understand the binary language that the computer uses to do it’s tasks. It is therefore necessary for us to write instructions in some specially defined language like C++ which is like natural language and after converting with the help of compiler the computer can understand it.

C++ Compiler

         A C++ compiler is itself a computer program which’s only job is to convert the C++ program from our form to a form the computer can read and execute. The original C++ program is called the “source code”, and the resulting compiled code produced by the compiler is usually called an “object file”.

          Before compilation the preprocessor performs preliminary operations on C++ source files. Preprocessed form of the source code is sent to compiler.

          After compilation stage object files are combined with predefined libraries by a linker, sometimes called a binder, to produce the final complete file that can be executed by the computer. A library is a collection of pre-compiled “object code” that provides operations that are done repeatedly by many computer programs.

c++ program diagram

Integrated Development Environment


          Above Figure illustrates the process of translating a C++ source file into an executable file. You can perform entire process of invoking the preprocessor, compiler, and linker with a single action by using Integrated Development environment. These environments consist of a text editor, compiler, debugger, and other utilities integrated into a package with a single set of menus. Preprocessing, compiling, linking, and even executing a program is done by selecting a single item from a menu.

Following Figure shows a screen from the Microsoft Visual C++ IDE.


visual c++ express

Read Creating and running a program using Microsoft Visual C++ Express Edition

The best way to start learning a programming language is by writing a program. In next blog, we'll discuss a program that displays message on monitor.




Saturday, March 2, 2019

Index of C++ Tutorial

C++ Tutorial

This tutorial is for school students who want to learn to program and don't have any knowledge about the programming.
In this course you will learn: semantics of the cpp, what is compiler / IDE, variables, input / output streams, operators conditions like if / else / switch , arrays / multi-dimensional arrays loops - for / while / do-while, functions, pointers,object oriented programming

INDEX

Unit 01. Basics of C++
Unit 02. Flow of Control
  • Conditional Structure
  • Looping Structure
Unit 03. Functions
  • Library Functions
  • User Defined Functions

Unit 04. Compound Data Types
  • Array - Single Dimension
  • Array - Multi Dimension
  • String - Character Array
  • Structures and other Data type
  • Pointers

Unit 05. Object Oriented Programming
  • OOP Concepts
  • Introducing Classes
  • Constructor and Destructor
  • Case Study : Time Class
  • Separate Header and Implementation Files
  • Static Members
  • Friend Functions and Friend Classes
  • Operator Overloading
  • Inheritance - I
  • Inheritance - II
  • Polymorphism and Virtual Function                

Unit 06. File Handling
  • File Handling Basics
  • Text File and Binary File