This article gives you an introduction to C++ Programming from ground level. This article won't teach you all the fundas of C++ programming rather it gives you the base to learn C++ programming, remember that for further learning, the base should be strong and this is what this article tries to do. It would let you know many fundas which will help you in further learning of the the language. C++ was developed in 1980s in the Bell Laboratories by Bjarne Stroustrup as an object oriented programming language. This language is considered by many as an extension of the programming language C. The extension of programming language C to create C++ is obtained by adding classes to C. This is why C++ was initially called "C with Classes". The C++ programming language derives its name from the increment operator used in C, which increments the value of a variable. The symbolic name of C++ rightly indicates that this language is enhanced version of C.
Features of C++ Programming Language:-
- C++ programming language is highly flexible, versatile and very powerful
programming language for developing any software specially the system software
namely operating system, compilers etc.
- C++ is most ideally suited language for development of reusable programs,
which is very important to keep the production cost minimum.
Let us see how C++ compares with other programming languages. All the programming languages can be divided into two categories:-
- Problem oriented languages or High-level languages: These languages have
been designed to give a better programming efficiency, i.e. faster program
development. Examples of languages falling in this category are FORTRAN, BASIC
etc.
- Machine oriented languages or Low-level programming languages. These languages
have been designed to give a better machine efficiency, i.e. faster program
execution. Examples of programming languages falling in this category are
Assembly Language and Machine Language.
Getting Started with C++ Programming
Communicating with a computer involves speaking the language the computer understands, which immediately rules out English as the language of communication with computer. However, there is a close analogy between learning English language and learning C++ language. The classical method of learning English is to first learn the alphabets or characters used in the language, then learn to combine these alphabets to form sentences and sentences are combined to form paragraphs. Learning C++ programming is similar and much easier.
Therefore, instead of straight-away learning how to write programs, we must first know what alphabets, numbers and special symbols are used in C++, then how using these, constants, variables and keywords are constructed, and finally how are all these combined to form an instruction. A group of instruction would be combined later on to form a program. Character SetCharacter set is a set of valid characters that the language can recognize. A character represents any letter, digit or any other sign. C++ has the following character set:
Letters A-Z, a-z
Digits 0-9
Special Symbols space + - * / ' " ( )[ ] etc.
White Spaces blank space, horizontal tab, carriage return, newline etc.
Other Characters, C++ can process any of the 256 ASCII characters as data or as literals.
The alphabets, numbers and special symbols when properly combined form constants, variables and keywords. Let us see what these are:-
- Constants: Constants are data items that never change
their value during a program run. C++ programming language allows several
kinds of constants.
- Variables: Variables are quantities that may vary during
program execution. Variable names are names given to locations in the memory
of computer where the value is stored.
- Keywords: These are the words that convey a special meaning
to the language compiler. Keywords are the words whose meaning has already
been explained to the C++ compiler. The keywords cannot be used as variable
names because if we do so we are trying to assign a new meaning to the keyword,
which is not allowed by the computer. Examples of keywords are if, void, for,
switch etc.
Data types are means to identify the types of data and the associated operations to handle it. In C++ data types are broadly of two types:-
- Fundamental Data Types: These are predefined to the C++
language itself. there are at least five fundamental data types.
- char- represents that the declared variable of this type can store
characters
- int- represents integers
- float- represents floating point numbers
- void- represents valueless data
- char- represents that the declared variable of this type can store
- Derived Data Types: These are constructed from the fundamental
types. I will not give you the details here because this is a bit high-level.
Now that we seen the different types of constants, variables and keywords the next logical step is to learn how they are combined to form instructions.
- Type declaration instructions: to declare the type of
variables used in the program.
Eg:- int num;
Here a variable num is declared of type int(eger).
- Input /Output instructions: to perform the function supplying
input data to a program and obtaining the output results from it.
Eg:-
cin>>a;
cout<<"Hello";
In the first line input is taken from the keyboard by the function cin and
is assigned to a pre-declared variable a. In the second line 'Hello'
is printed using the function cout.
- Arithmetic instructions: to perform arithmetic operation
between constants and variables.
Eg:- c=a+b;
Here c is assigned a value which is the sum of the variables a and b.
- Control instructions: to control the sequence of execution
of various statements in a C++ program.
Eg:- if (a>b) func1();
Here it is checked whether a is greater than b, if it is, then program execution
goes to a user defined function 'func1'.
The first C++ Program
Armed with the knowledge about the types of variables, constants, keywords etc. we would write down our first C++ program.
Each instruction in a C++ program would comprise of a series of statements. These statements must appear in the same order in which we want them to be executed.
The following rules are applicable to all C++ programs no matter ho long or complicated they are
- Blank spaces may be inserted between two words to increase readability
of the statements. However, no blank spaces are allowed within a variable,
constant or keyword.
- Usually all statements are entered in small case letters.
- C++ has no specific rules for the position at which a statement is to be
written. That's why it is often called free-form language.
- Any C++ statement always ends with a semicolon (;).
A few useful tips:-//To calculate the sum of two given numbers
#include
main()
{
int num1; //declares a variable num1 of type int(etger)
int num2; //declares a variable num2 of type int(etger)
int sum; //declares a variable sum of type int(etger)
cin>>num1; //takes input and stores to the var num1
cin>>num2; //takes input and stores to the var num2
sum= num1+num2; //adds vars num1 & num2
cout<
}
- Any C++ program is nothing but a combination of functions, main() is one
such function which is always there in a C++ program in one form or the other.
Empty parentheses are necessary after main.
- The set of statements belonging to a function is enclosed within a pair
of braces
Ex.
main()
{
statement1;
statement2;
statement3;
statement4;
}
- Any variable is declared before using it.
- Any C++ statement should always end with a semicolon.
- iostream.h is the file needed to use the functions cin and cout, which
is included in the program with the include keyword.
After going through the article you have got an introduction to C++ Programming, you now know what C++ is and how it is used. You now know the C++ language and have learnt some of the most fundamental parts of C++. you have learnt how to declare variables and how to use them in arithmetic operations. In one sentence you have got an introduction to C++ programming which will help you in further learning of the language.
Original article by Arvind Gupta. You are free to republish |
No comments:
Post a Comment