C++ tutorial, C++ programs, C++ Exercises, C++ Example Programs

Monday, July 18, 2011

Basic Structure of a C++ Program

So, what is the format of a C++ program? We discussed in previous C++ lessons about some basic programming concepts like program, programming language, program development process, algorithms, flowcharts and so. Here we will discuss basic  structure of a C++ program. The basic structure of a C++ program consists of the following two main parts:

1. Preprocessor Directives

2. main Function

Basic C++ Program Strucrure

Preprocessor Directive number 1
Preprocessor Directive number 2
.
.
.
Preprocessor Directive number n
void main()
{
    C++ statement number 1;
    C++ statement number 2;
      .
      .
      .
    C++ statement number n;

1. Preprocessor Directives

Preprocessor directives are C++ commands given to Compiler. Preprocessor is a part of C++ compiler. Preprocessor acts upon these commands. Preprocessor directives modify source program before compilation.Preprocessor directives start with hash sign #. Preprocessor directives does not end with a semi colon. For example #include is a preprocessor directive. It is used to include header files into our source program. #include<iostream.h> means to include a header file iostream.h  into our source program. If we use cout object or cin object in our program, then we have to include iostream.h header file in our source program, because it contains necessary information about cin and cout object.

2. main Function

A C++ program is divided into small and manageable modules of code called functions. The main function is the starting and compulsory function. The control starts execution of C++ program from the main function. The basic structure of a main function is as follows:
basic-structure-of-C-plus-Plus-Program-in-detail-Explanation-examples
basic-structure-of-C-plus-Plus-Program-in-detail-Explanation-examples

void main()
{
    C++ statement number 1;
    C++ statement number 2;
      .
      .
      .
    C++ statement number n;
}
First line void main() indicates the start of main function. It is called declaration or header of main function. void means this main function will not return any value.
Second line { indicates start of body of main function. Body of main function consists of a set of one or more C++ statements according to the logic and requirement of the program.
C++ statements are also called instructions or commands. Each C++ statment is used to perform a specific task. For example, the follwing statement is used to display "Hello C++" on screen:
cout<<"Hello C++";
Next Lines indicates the presense of a set of one or more C++ statements.  
Here is a basic first program of C++.
Last Line }  shows the end of main function as well as the end of the program.
Let us see an example program according to the basic structure discussed above:

1. #include<iostream.h>

2. void main()

3. {

4. cout<<"Hello C++ Programming!";

5. }
OUTPUT of this program
Hello C++ Programming!

How does this program work?

How does this program work?
  1. [#include<iostream.h>] is used to include header file named iostream.h in source code (First.cpp).
  2. [void main()] indicates Start of main function
void means the main function will not return any value.
  1. { Opening Brace indicates the start of definition of main function.
  2. [cout<<"Hello C++ Programming!";] is used to display “Hello C++ Programming!” on monitor screen.
  3. } Closing Brace indicates the end of main function definition

Share:

C++ Program Development Cycle

The C++ development cycle describes the process to create a program. This process is divided in to four steps as shown in the figure:
Generally, to create a program, you follow these steps:
1. Type the code in a source code editor (Turbo C++, Quincy C++ Compiler, CodeWarrior or some other editor). Here you will write C++ instructions to perform the task, in a predefined format
2. Compile the code.  Compiling means translating source code into object code. If there are errors, you must go back to the code to fix them.
3. Link the code. Linking the code is the process of checking to see whether the code works with all the files that you included in the program. If you get an error, you must return to Step 1 to fix the error.
4. Test the program. You test the program to be sure that it functions properly. Here you will perform the follwing steps:
1. Execute / run the program.
2. Enter sample data
3. Check the output results. Are they accurate?
If your program makes it through all of these steps successfully, then the development cycle is now complete.
Share:

Friday, July 15, 2011

How To Write, Compile and Run Your First C++ Program in Turbo C++

There are three main steps in developing a C++ program:
  1. Type your Program in Turbo C++ Editor
  2. Compile this program, and remove errors, if any.
  3. Execute the program and get output results.
Here I am teaching you all this through a set of pictures with proper titles to explain each step.
First of all, open Turbo C++ IDE to write, compile and run programs in C / C++ easily. you can double click on the short cut of Turbo C++ on desktop of your PC.
Turbo C++ IDE to write, compile and run programs in C / C++
Easyway open Turbo C++ IDE to write, compile and run programs in C / C++

 Now click on the File menu and select New option to create a new file for writing a C / C++ program
create a new file for writing a C / C++ program
Easyway C++ -click on the File menu and select New option to create a new file for writing a C / C++ program
Start typing the C++ program in new file.
typing the C++ program in new file.
Easyway C++ Programs - Start typing the C++ program in new file.
Type your C Plus Plus program, as shown in the figure below:
Type your C Plus Plus program
Easyway C++ Programs Type your C Plus Plus program
Click on File menu and select Save option or press key board shortcut F2 to save the C++ program file .
Save the C++ program file
Save the C++ program file - Easyway C++ Programs.
Save C++ file

type name and press enter

press enter to save C++ file

C++ program saved

Compile C++ program

Compiled successfully C++ program with no errors

Run or execute C++ program

output of C++ program
Share:

How To Start Turbo C++ IDE

Today, I am going to tell you about starting Turbo C++ 3.0 IDE, so that you will be able to write C++ programs. First of all, go to your My Computer icon and double click on it. Open C drive, double click on TC folder and Open BIN folder by double clicking. Here you will see a file named TC.EXE. Double click on it to start Turbo C++ 3.0 IDE. The following pictures will help you to perform all this.






TC.EXE file is in BIN folder. Open this folder.
When you double click TC.EXE, Turbo C++ environment will be before your eyes, as shown below:
Share:

Thursday, July 14, 2011

How To Install Turbo C++ 3.0 - IDE

Note: This tutorial was written by me to install Turbo C++ version 3.0 IDE from an Exe file. Now I have made available for you a pre-installed, ready to download and work TURBO C++ 3.0 file. Please Read This New and Updated Tutorial. Anyhow, you can continue to read this tutorial for information.
First of all, you need to install a C++ IDE- Integrated Development Environment to Start Computer Programming using C++. Turbo C++ 3.0 IDE is one of the best environments to create, edit, compile and run C++ programs. First of all download Turbo C++ 3.0 Installation file. Then perform the following steps according to the pictures:

Double click the installation file.

C

Click Install button to install Turbo C++ 3.0 into C drive.
Turbo C++ 3.0 installer is extracting and copying files....
Normally, the directory paths for Turbo C++ 3.0 require that the main folder name should be TC, so if you see a different name like in the figure shown above, please rename it to C. It is necessary to properly compile and run C++ programs.



Installation process is completed!
Share:

Wednesday, July 13, 2011

Features of C++ Programming Language

Let us discuss important features of C++ Programming Language, in detail. C++ is a powerful and general purpose programming language. It can be used to create not only numerous application programs, but also suitable for system programming. By the passage of time, C++ has become very popular and now it is the preferred programming language to develop professional applications on all plate forms. Following are some important features of C++ programming language:

1. Easy To Learn



C++ is very easy to understand and learn. It uses English like statements that are easy to remember and use. Due to this property of C++, now a days, it is the part of Computer Classes at Bachelor and Master level. C++ is taught to students as Introduction To Programming.
Features of C++ Programming Language

2. Case Sensitive

C++ is case sensitive, it means that it will differentiate between lower case and upper case words. A variable Marks will be considered different from marks, due to lower and upper case. All keywords are written in lower case.

3. Hardware Control

It is used to write programs to control hardware components of computer. It provides the convenience of a high level programming language such as BASIC, COBOL or Pascal, but at the same time allows much closer control of a computer's hardware. C++ is used for system programming.

4. A Small But Powerful language

C++ is a small language with a small number of keywords and programming constructs. But still it is very powerful to develop application as well as system software.

5. Machine Independent

C++ supports machine Independence. It means that a C++ program written on a computer can be run on different computers

6. Faster Code

The latest C++ compilers can generate amazingly faster code. This code is so efficient that its execution speed can be compared with the Assembly language code for the same task.

7. Well-Structured language

Syntax of C++ makes it easy to write programs through modular programming(dividing larger program into smaller and more easily manageable modules. Therefore C++ programs are  easy to understand and maintain.

8. C++ is a superset of C

C++ is a super set to C language. It uses all syntax of C and adds more new features. Major addition is the features that support Object oriented programming.

9. Compatibility With C

As you know C++ is an extension to C language. So it is backward compatible with C. It supports all C language syntax and features. So any C statements can be included in a C++ program.

10. Object Oriented Programming

Important features of C++ Programming language


C++ is an Object oriented language. It supports programming on the basis of objects. An object is a collection of data and related functions. Object may represent a person, thing or place in real world.
OOP is a programming technique in which programs are written on the basis of OBJECTS. Object oriented programs are easier to write, debug and maintain. OOP becomes increasingly helpful, as the programs grow larger. OOP has the greatest potential for simplifying program conceptualization, coding, debugging and maintenance, when we have to develop an application with hundreds of functions. 

Share:

Brief History of C++


C++ - pronounced “See Plus Plus” - is a powerful computer programming language. It was developed by Bjarne Stroustrup at Bell Laboratories in 1985. It is an enhancement to the popular C Language. It fully supports the latest programming technique Object Oriented Programming – OOP.

Brief History of C++

1. During 1960s, many programming languages were developed.
2. In 1963, a language called CPL – Combined Programming Language was developed. It was better to meet the requirements of the market at that time, but very difficult to learn.
short history of c++ Picture of Bjarne stroustrup developer of C++
      
3. In 1967, Martin Richards Developed a new, simplified and modern language called BCPL – Basic Combined Programming Language. BCPL was simplification of CPL.
4. In 1970, Ken Thompson developed B language. 
5. In 1972, Dennis Ritchie developed C language.
6. In 1978 Brian Kernighan and Dennis Ritchie published a book – The C Programming Language. The earlier version of C was known as K & R (Kernighan and Ritchie) C.
7. In 1989 ANSI – The American National Standards Institute produced a newer standardized version of C language called ANSI C. ANSI C introduced new features that were not present in traditional C that is K & R C.
8. In early 1980s, Bjarne Stroustrup at Bell Laboratories started the work to develop C++.
9. In 1985, the first commercial version of C++ was released.
10. In 1990 ANSI Committee started the work on standardization of C++.
11. In 1998 ANSI / ISO C++ standard was approved. ANSI /ISO C++ standards are used by most of the C++ compilers available today. C++ is a superset of C language: that is it incorporates all the operators, functions, statements and syntax of the ordinary C, but adds additional features. C++ enabled the programmers to improve the quality of code. The main reason for these additional features is to support Object Oriented Programming. In addition there are many other new features, too. Today, C++ is widely used to develop Application software as well as system software. There are several versions of the C++ language. The main versions of C++ include Turbo C++,  Borland C++, Code Warrior–Mac and above all Microsoft Visual C++. 

Object Oriented Programming:

OOP is a programming technique in which programs are written on the basis of OBJECTS. Object oriented programs are easier to write, debug and maintain. OOP becomes increasingly helpful, as the programs grow larger. OOP has the greatest potential for simplifying program conceptualization, coding, debugging and maintenance, when we have to develop an application with hundreds of functions. 

Share:

Tuesday, July 12, 2011

Flowcharts in Computer Programming

Definition of Flowchart

Today we will discuss The Role of Flowcharts in Computer Programming. Flowchart is the graphical representation of an algorithm. It is used to show all the steps of an algorithm in a sequence pictorially. An algorithm may be converted to a flowchart. The flowchart is then converted into a program written in any programming language.

Use of Flowchart in Computer Programming with Symbols
Importance and Symbols of Flowchart in Computer Programming

Why to use Flowcharts in Computer Programming?

Advantages of flow charts in computer programming

Advantages of Flowcharts

So what are the main advantages of using Flowcharts in Computer Programming?
Flowchart is used for the following reasons:
 1. Flowchart represents an algorithm in graphical symbols.
 2. It shows the steps of an algorithm in an easy way.

3.
 3. It is used to understand the flow of the program graphically.
 4. Flowchart can be easier and more effective to understand the logic of the given program instantly.
 5. A flowchart will be helpful to the programmer during coding / program development phase.   

Commonly Used Symbols To Draw a Flowchart

So after reading the benefits of using Flowcharts in Computer programming, we will learn how to draw a flow chart. Following are the common symbols used in Flowchart:

1. Start/End

Oval symbol is used to represent the start or end of the flowchart. Every program has a Start and End. So we will add an Oval symbol with caption "Start" in the beginning of each flow chart. Similarly, we will place an Oval symbol with the tag "End" or "Stop" at the end of every flow chart. Flowcharts in Computer Programming Oval Symbol

2. Input/Output

Parallelogram symbol is used to represent an input or output step. So if we wish to input the value of a variable N in our program, we will put a parallelogram symbol there with a caption of "Input N" or "Read N" etc. Similarly, we will use this symbol to represent output statements in program too. For Example, if we encounter an output statement in our program algorithm to show a message "Enter a Number:", we will add a parallelogram symbol with the caption "Enter a Number". If we wish to show the value of a variable, we may add parallelogram symbol with the text "The result =", result. Where "result" is a variable name.
 

3. Process

Rectangle symbol is used to represent a process step. A process may be a calculation or assignment etc. The algorithmic statements like Calculate Sum = Num1 + Num2 can be represented by the rectangle symbol in flowcharts. Similarly, the assignment statements like Width = 10 may also be represented by rectangle symbol.
Flowcharts in Computer Programming Rectangle or Process Symbol
 

4. Selection

Diamond symbol is used to represent a selection step. A condition is given in the diamond. If condition is true then flow of control will go in one direction. If condition is false then control will go in other direction.
Flowcharts in Computer Programming diamond or decision Symbol
For example, we wish to represent an IF statement in a flowchart, say IF Number N is divisible by 2 Then Display "It is an Even Number" Else Display "It is an Odd Number" End IF In this case the diamond symbol will be used along with parallelogram symbols as shown below: Flowcharts in Computer Programming Example of diamond or decision Symbol

5. Flow Lines

Arrow symbols are used to represent the direction of flow in the flowchart. There are four types of flow lines.
Flowcharts in Computer Programming Flow lines arrows Symbol

6. Connector

Circle symbol is used to combine different flow lines. It is used as a connector symbol in flow charts.
Flowcharts in Computer Programming connector Symbol

 

Solved Flow Chart Examples:

Flowchart Example 1 Develop a flowchart to input two numbers from the user, calculate its sum and then display the result.
Example Add Two Numbers Flowcharts in Computer Programming
Example Add Two Numbers Flowcharts in Computer Programming
  Flowchart Example 2 Develop a flowchart to input 5 numbers from the user, calculate its average and then display the result.
Example of Average program Flowcharts in Computer Programming
Example of Average program Flowcharts in Computer Programming

Dear Readers, you just read the role of Flowcharts in Computer Programming in detail with Flow chart definition, symbols, advantages of flow charts and examples of flow charts.


Further Suggested Readings: After Download Free Turbo C++ Ver 3.0 IDE!

Basic Structure of a C++ Program

C++ Program Development Cycle

How To Write, Compile and Run Your First C++ Program

How To Start Turbo C++ IDE

How To Install Turbo C++ 3.0 - IDE

Features of C++ Programming Language

Brief History of C++

Flowcharts in Computer Programming

The Role of Algorithms in Programming

How To Start Computer Programming

What is a Computer Program

- See more at: http://easycppprogramming.blogspot.com
Share:

The Role of Algorithms in Programming

Today, we will discuss The Role of Algorithms in Programming. Dear C++ beginner programmers, before writing a program design an algorithm first. This will make the whole programming process easy and less prone to errors. So, first of all we define an algorithm and learn its basic concepts.
The Role of Algorithms in Programming

Algorithm

An algorithm is a step-by-step procedure to solve a problem. It is better to write algorithm before writing the actual computer program. Writing algorithm first, will help to write a better program with less errors. Why algorithm is so important in program development process? We answer this question by discussing the advantages of algorithms in program development, as follows:
The Role of Algorithms in Programming Example

Advantages of Algorithms

Following are the advantages of  writing algorithm before writing program:

1. Reducing Complexity

Programming is one of the most complex jobs. Writing algorithm before program makes the job simple, because we divide big task into smaller sub tasks. Each sub task is solved separately

2. Flexibility

Algorithm is written so that the program may be written in any language e.g. Visual Basic, Java or C++ etc.

3. Easy to understand

Algorithm is written in English like statements. So it is easy to understand.

Example 1:  The Role of Algorithms in Programming

Write an algorithm to input two numbers from the user, calculate its sum (total) and then display the result.
1.    Input X, Y
2.    Total = X+Y
3.    Display Total
4.    End

Example 2: The Role of Algorithms in Programming

Write an algorithm to input 5 numbers from the user, calculate its average and then display the result.
1.    Input A, B, C, D, E
2.    Total = A+B+C+D+E
3.    Average=Total / 5
4.    Display Average
5.    End

While studying The Role of Algorithms in Programming we also discuss the basic properties of an algorithm:

Properties of Algorithm

Following are some properties of an algorithm:
1.    Algorithm must provide the complete solution of the problem
2.    Algorithm must finish in finite time period
3.    Each instruction in algorithm must be executed in finite time period
4.    The given problem should be broken down into simple and meaningful steps.
5.    The steps should be numbered sequentially.
6.    The steps should be descriptive and written in simple English.
Dear Readers, we have covered today The Role of Algorithms in Programming. 


Further Suggested Readings: After The Role of Algorithms in Programming

Basic Structure of a C++ Program
C++ Program Development Cycle
How To Write, Compile and Run Your First C++ Program
How To Start Turbo C++ IDE
How To Install Turbo C++ 3.0 - IDE
Features of C++ Programming Language
Brief History of C++
Flowcharts in Computer Programming
The Role of Algorithms in Programming
How To Start Computer Programming
What is a Computer Program
- See more at: http://easycppprogramming.blogspot.com
Share:

How To Start Computer Programming

Computer programming can be started by learning a high level computer programming language like C or C++. Since C++ is the latest and extended version of C, so in this blog: Easy Cpp Programming  we will start learning Computer Programming through C++ . We have already discussed some basic programming concepts like What is a Computer Program, Programming Languages and Computer Programmer. Before we start, here are some basic concepts that are useful in better programming approach.  First of all, we will learn The Program Development Process. Later, some problem solving techniques like ALGORITHM and FLOWCHARTS will be discussed. Lets start with Program Development Process.  
How to start Computer Programming - Program development process

Program Development Process

A programmer must go through a set of organized stages to develop a good program. These stages of program development process are discussed below:
1.    Defining and Analyzing the Problem
2.    Designing the Algorithm
3.    Coding                                                                                   k
4.    Testing and Debugging
5.    Documentation

1. Defining and Analyzing the Problem

In this step, a programmer studies the problem. He decides the best way to solve this problem. He selects the programming language. He finds desired inputs and outputs and processing details.

2. Designing the Algorithm

The programmer designs an algorithm for the program. Algorithm is a step by step procedure to solve a problem. It simplifies writing of program. He may convert this algorithm into a flowchart. Flowchart is the pictorial representation of an algorithm.

3. Coding  (Writing the Program)

The next step is coding. Here Programmer writes the program in a high-level language. He selects suitable program instructions from the chosen programming language to perform the requires tasks.

4. Testing and Debugging

The process of checking correctness of a program by executing the program with sample data is called Testing.  In testing, the program is executed to check whether it is according to our requirements or not.  If the output is not according to our requirements then we find faults correct them. Debugging is a process of finding and correcting the errors in a program.

5.  Documentation

When the program is finalized, its documentation is prepared. Documentation guides the user so that he may use the program efficiently. It helps the user to execute the program and perform different tasks.
Share:

Monday, July 11, 2011

What is a Computer Program


A computer program is a set of instructions given to computer to perform a specific task. For example,  a program to calculate employees pay, a program to calculate factorial of a number or a program to display multiplication table of a given number. Programs consist of instructions to tell the computer how to process given data and get information. Computer programs are used to perform different tasks on computer, easily and quickly. The following pictures shows a program written in C++ programming language:
What is a Program in Computer Programming

/*
 This C++ program is used
 to show Hello C++ Programming!
 CopyRights 2011-2013
 EasyCppProgramming.BlogSpot.Com
*/
#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  cout<<"Hello C++ Programming!";
  getch();
}
The above program consists of different instruction in C++ programming language. The main purpose of this little program is to display a message on monitor screen ' Hello C++ Programming!'. So when this program is compiled and executed, the following output will be displayed on computer screen:

Output of First Program in C++ Hello C++ Programming

What is Computer Programming?

Computer programming is the process of developing computer programs. Computer programs are a medium to instruct the computer. They tell the computer what to do.

Define a Computer Programmer:

  A Computer programmer is a person who develops computer programs. Commonly, a computer programmer has a bachelor or master level degree in Computer Science.

What is a programming Language?

A programming language is a set of words, symbols and rules to write computer programs.  There are many programming languages like BASIC, COBOL, Pascal, Java, Visual Basic, C language and C++ etc.
Share:

EasyCPPprogramming.blogspotcom

Labels