A Basic Theory Of C++ Programming Upto Functions

This is an introduction to C++ .C ++ is a very powerful programming language.I hope it will be useful to the students .As the name indicates its the advanced level of C programming with Inheritance,object orientations and operator overloading.But here I want to mention only the basic steps of C++ programming.

Header files

When you start a C + + program, you should aware about the header files in C++. The very first header files in C+ + is iostream.h. At first we should include this header file in our program. The keywords of C++ language are defined in this file. That means while including this file only your program can understand the keywords in you program.



Main Function

You can write the body of program inside the main function. The function is indicates with the symbol (). And starting and ending braces are there.





A terminator ';' is required at the end of each line.
for the comment entry in programmincode symbol '// 'is used.

Keyword "cout" is used to display in test in the screen of computer. The test should be inside the quotes .An example for the program to see the test displayed in the screen.

Program 1




Result: Display

The keyword 'endl' is used to go for next line.Do the following program with and without using endl;The you can understand the use of 'endl'. '\n' can be also use.



cout<<"GOOD MORNING"<cout<<"HAVE A NICE DAY"<cout<<"End of the program"<}

Data and Variables

Variables are the small memory space to store the inputs. Basic types of variables are:

char: To store characters
int : To store numbers and integers
float :to store numbers with decimal

Each variable has a specific address in the memory. The declaration of variable is an important step. Variable declaration can be done inside the main () or out side it. The variable may be local and global based on its place of declaration.

Syntax for declaration of variable:

Type of variable space name of variable

int num;

char reply;
float weight;

Accepting Data into a variable

The keyword 'cin' is used to accept the data.

Program 2

int num;

cout<<"enter a number";



The name of a variable should not enclose inside the quotes.

Arrays And Pointers

A character variable allocates a single space for a character .So it requires array for accepting character data just like name, address, designation, etc…

Arrays are the continuous memory allocations for the same type of variables. The size of an array should be declared with its name.

The syntax to declare a character array;

data type space name of array[size of array]

char name[10];

int marks[10];

float weights[10];

Each memory space in an array is called element. For exception to others, the character array has a '/0' (back slash /zero) in the last element. So it is required to give an extra element for the back '/0'.

Values can be assigned to the variables, at the time of declaration .In the case of character variable or array the syntax is;

data type variable name =' ';
data type variable name[size] =" ";

char answer='y';
char name="apple";

Pointers are also a type of variable, but they store the addresses of other variables.
Pointer declaration

char name[10];
char* ptr=&name;

Types of Operators in C++

Arithmetic Operators: +,-,*,/

Logical Operators :>,<,>=,< =

Relational Operators :&&( and), != (not Equal to), ||(or)

Assignment Operator: =

Comparison Operator: ==

Constraints Loops

Constraints are used to check logics .

1 if ….else

2 switch …. case


Loops are used for repeated execution of required lines with out write them repeatedly in the program. So they help to reduce size of program

1 for ( ; ; )
2 while (condition)

3 do {
} while (condition);


Function includes function declaration, function definition and function call.
void main()

void display();//declaration

void display()//definition

cout<< "Hello World" <



Function can have return values. So while declaring functions we can mention the data type of the return value.If the declaration of function start with void there is no return value.

int add();

Function polymorphism

Parameters can be passed into the function at the time of execution.These parameters can be of same or different data types.And also there can be different numbers of parameter.

void add(int num1,int num2);
void add(int num1,float num2);
void add (int num1,int num2,int num3);

Here all functions have same name but they perform differently.This is called function polymorphism.

Execution of c++ program

The execution of programs begins with main function. All the other functions are called inside the main function. These functions are executed as per the order of function calls inside the main.

Advance to C, the C++ is an object oriented program. Its other features are Inheritance, class, object to class, pointers to class, etc

Related Articles

Using The C Preprocesser To Make Great Programs

The C-preprocesser is a very distinctive feature of C which enables it to provide flexibility to programs. It can be used to do some great things and make programs more portable and flexible. The preprocesser can be given certain commands called "directives" that can do a lot of things explained in the article below.

Calculations in C programming

Your computer is actually a big calculator. Whether you're listening to music, watching a movie or playing a video game, your computer only do calculations. This article will teach you to make the most of the calculations a computer can do. We will reuse what we have just learned, namely variables. The idea is just to make your calculations with variables: these variables add, multiply, store the result in another variable, etc.. Even if you're not a fan of math, this article will be absolutely essential.

A Path to "C" Language

Here we will be in a new journey to C Language. Here you will now how to learn, use and get mastery in C. There were many languages evoulve after c, But it exist in the history of languages as a golden milesotne du to the roboustness of this language. Here part by part we will cover all the topics of C. We will know how to use it. So in each part you will now something new.

Basic Fundamental of C# Object Oriented Programming language

Now days software companies want to develop project or web-application or web-site in short times and also in low price. C# language is growing day by day due to its support with .NET and provides same features like java. C# is more user friendly than java. C# has world largest class library.

More articles: C language


Author: Prem Prakash Gupta17 Aug 2011 Member Level: Bronze   Points : 1

Deepa this is very nice demo of C++ programming usage. Unfortunately using chars like < or > are html special characters which have been removed from your posting.
I request you all to use this to format C++ code snippets : www.prettyprinter.de/module.php?name=PrettyPrinter

before pasting here otherwise it's discouraging to see a incorrect C++ syntax due to usage of html special characters.

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: