Resources » Articles/Knowledge Sharing » Computer & Technology

A Basic Theory Of C++ Programming Upto Functions


Posted Date: 08-Aug-2011  Last Updated:   Category: Computer & Technology    
Author: Member Level: Silver    Points: 8


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.


Syntax


#include

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.

Syntax


Main()

{

}

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

#include

Main()

{
cout<<"GOOD MORNING";
}

Result: Display
GOOD MORNING


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.

#include

Main()

{
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
#include

Main()
{
int num;

cout<<"enter a number";

cin>>num;

cout<<"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


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);



Functions


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

{
void display();//declaration

void display()//definition
{

cout<< "Hello World" <
}

display();//call

}

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 Resources:


Read related articles: C language    

Did you like this resource? Share it with your friends and show your love!




Responses to "A Basic Theory Of C++ Programming Upto Functions"
Author: Prem Prakash Gupta    17 Aug 2011Member 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.



Feedbacks      

Post Comment:




  • 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:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Awards & Gifts
    Active Members
    TodayLast 7 Daysmore...

    ISC Technologies, Kochi - India. Copyright © All Rights Reserved.