Skip to main content

Introduction to C++ Programming


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. Comparison of C++ Programming Language

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.

C++ stands in between these two categories. That’s why it is often called a Middle level language, since it was designed to have both: a relatively good programming efficiency (as compared to Machine oriented languages) and relatively good machine efficiency (as compared to Problem oriented languages).

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 Set: Character set is a set of valid characters that the language can recognize. Acharacter 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 in C++

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
  • 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.

Instructions in C++ Programming Language

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 (;). Now, let us have a look at a program which calculates the sum of two numbers given by the user.

Now, let us have a look at a program which calculates the sum of two numbers given by the user.

//To calculate the sum of two given numbers
#include<iostream.h>
main()
{ 
int num1; //declares a variable num1 of type int(etger)
int num2; //declares a variable num2 of type int(etger) 
int total; //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 
total= num1+num2; //adds vars num1 & num2 
cout<<total; //prints the value of the variable
}

A few useful tips:-

  • 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.

Summary

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

Popular posts from this blog

Fix For Toshiba Satellite "RTC Battery is Low" Error (with Pictures)

RTC Battery is Low Error on a Toshiba Satellite laptop "RTC Battery is Low..." An error message flashing while you try to boot your laptop is enough to panic many people. But worry not! "RTC Battery" stands for Real-Time Clock battery which almost all laptops and PCs have on their motherboard to power the clock and sometimes to also keep the CMOS settings from getting erased while the system is switched off.  It is not uncommon for these batteries to last for years before requiring a replacement as the clock consumes very less power. And contrary to what some people tell you - they are not rechargeable or getting charged while your computer or laptop is running. In this article, we'll learn everything about RTC batteries and how to fix the error on your Toshiba Satellite laptop. What is an RTC Battery? RTC or CMOS batteries are small coin-shaped lithium batteries with a 3-volts output. Most laptops use

The Best Way(s) to Comment out PHP/HTML Code

PHP supports various styles of comments. Please check the following example: <?php // Single line comment code (); # Single line Comment code2 (); /* Multi Line comment code(); The code inside doesn't run */ // /* This doesn NOT start a multi-line comment block /* Multi line comment block The following line still ends the multi-line comment block //*/ The " # " comment style, though, is rarely used. Do note, in the example, that anything (even a multi-block comment /* ) after a " // " or " # " is a comment, and /* */ around any single-line comment overrides it. This information will come in handy when we learn about some neat tricks next. Comment out PHP Code Blocks Check the following code <?php //* Toggle line if ( 1 ) {      // } else {      // } //*/ //* Toggle line if ( 2 ) {      // } else {      // } //*/ Now see how easy it is to toggle a part of PHP code by just removing or adding a single " / " from th

Designing a Simple Order Form Application in PHP

Ok guys, for this post we’re going to create a simple yet complete order form page. Order forms are used on many sites to take customers order online. Order forms should have the capability to take orders from visitors regarding what items they want to purchase and store the information for further processing. For this post’s example, we are going to create an order form for a Book Seller. The form will be designed to take order of five different items (books). Our order form application should be able to take order of five different items in any separate quantities tht user wants, it should also ask for shipping address and name of the customer. It should then store the information provided in a file along with the date and time order was placed. The application should also be able to take any number of orders and store them all linearly for further human processing. For this, we need a front end of a HTML form to which the user would interact a