C++ Programming

C++ Programming

C++ Overview

Introduction

The C++ programming language developed at AT&T Bell Laboratories in the early 1980s by Bjarne Stroustrup. He found C language lacking for simulations and decided to extend the language by adding features from his favorite language, Simula 67. He called it “C with classes” originally. However the name C++ (pronounced C plus plus) was coined by Rick Mascitti.

According to a study, C++ has influenced many other programming languages, some of which include Java, Python, C# and even newer versions of C. If C++ was never created, who knows what these programming languages would look like today. The major reason behind the success and popularity of C++ is, it supports the object-oriented technology, the latest in the software development and the most near to the real world.

Kindly assign a new notebook to take notes so that you could refer to it at any time. Before creating your first program, let’s look at some of the important terms.

Tokens

The smallest individual unit in a program is known as a token or a lexical unit. C++ has the following tokens :

  • Keywords
  • Identifiers
  • Literals
  • Punctuators
  • Operators

Keywords

A word having special meaning reserved by a programming language is known as a keyword. A keyword can be command or parameter. Here are some of the most used keywords in C++ :

Important Keywords

Identifiers

An identifier is basically a name given to different entities such as variables, constants and functions.  Here are the rules for defining an identifier :

  • Alphabets, digits and underscores are allowed
  • Cannot begin with a digit
  • Blank space is not allowed
  • No other special characters
  • A keyword cannot be used as an identifier
  • C++ is case sensitive

We’re going to discuss more about each of these keywords in the upcoming modules…….

C++ Programs 

C++ programs can be created using any text editor. We want all our students to follow up on the programs such that they can create it themselves. So we’ll use a software known as Turbo C++ to do so. Here are the following links to download the software : 

Download Turbo C++ for Windows

Download Turbo C++ for Mac

So once you’re done installing it, then the software will appear in the desired location. Then click on it go to File>>New. For those who can’t see a clear page, keep on clicking New until you get a clear page. 

Now let’s create a basic C++ program. 

#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
char ch;
cout<<"My First C++ Program!!";
cout<<"\n\n\nEnter your Name:";
cin>>ch;
//Yay, I'm learning C++
getch();
}

Now let’s discuss about each of these statements.

#include<iostream.h>

The statements that begin with “#include” are known as header files. These header files contain built-in functions, which are imported or used in any C++ program by using the preprocessor #include statement. This header file includes the following functions :

cout (pronounced see-out) : It is a predefined variable in C++ that stands for common output. It uses an operator known as an output operator (<<). Anything that comes between the double quotes will be displayed in the output of the program.

cin (pronounced see-in) : It is a predefined variable in C++ that stands for common input. It uses an operator known as an extraction operator (>>) to accept text from the user and store it in a certain variable. 

Note : Each header file contains a set of unique functions

#include<conio.h>

This header file includes the following functions :

clrscr( ) : Stands for clear screen. Let’s say you run a program and after a while you run it again but you see the first run-time data again. So to avoid this issue, we use this function to clear the previous run-time data from the screen.

getch( ) : Pronounced as get-ch. The real purpose of getch( ) is to return the keypress. The function waits until the user presses a key. Therefore whenever we use void main( ) we’ve to end the program with getch( ).

void main( )

The void main( ) indicates that the main( ) function will not return any value, hence using getch( ) to end the program. The execution of the program starts with the main function. Every function in C++ has its code included in a pair of curly braces( { } ) refers to the start and the end of the function. This concludes why the start and the end of the main function is indicated by the curly bracket.

char ch;

The keyword char stands for character. The abbreviation char is a data type that holds a single character. The statement ‘char ch’ basically means that we’re initializing a variable ‘ch’ that stores a single character. So if we apply this concept in the above program, you’ll notice that in the output I’ve given my name (comprised of more than one character). Thus the resulting variable ‘ch’ should have the character ‘D’ and the remaining characters will be eliminated.

cout<<“My First C++ Program!!”; 

As discussed earlier, cout is used to display text in the output screen. The text in between the double quotes will be seen in the output.

cout<<“\n\n\nEnter your Name:”;

This statement comprises of a special character known as ‘ \n ‘ (pronounced a slash-n). The new line character ‘ \n ‘ can be used as an alternative to endl. The endl and the ‘\n’ character both have the same function. The syntax for both of these special characters :

cout<<endl; or cout<<“\n”;

We’ve used three ‘ \n ‘ so that we could go three lines below the previous statement in the output screen.

Note : Every statement in C++ inside the main( ) function must end in a semicolon( ; ) or it will be considered as an error by the compiler

cin>>ch;

This statement basically means that we enable the user to input their name, which will be stored in the variable ‘ch’ character.

Note : A character variable can only store a single character unless modified to

//Yay, I’m learning C++

All lines beginning with a backslash are considered as a comment in C++. Comments are portions of the code ignored by the compiler which allows the user to make simple notes in the relevant areas of the program. 

Execution of the Program

Now it’s time to practice the program yourself, without having a look at it. Make sure the code is as precise as mine, or else you may get an error. Now I’m gonna show how to run this program.

Run the Program : Head to the menu bar, and then click Run, then a drop down menu will appear. Hit Run again. Your screen should appear something like this :

My First C++ Program

Now in the output screen, the user’s name will be asked. Input your name and give yourself some credit, because you’re learning remarkable things with us!!

Now it’s time to save your program.

To do that go to File>>Save as and save the file as “MyFirstProgram.cpp”. Make sure there is no space when your typing the name of the file. Use underscore instead of the blank space if required. Don’t forget to type ‘ .cpp ‘ otherwise your program will not be saved.

That’s it for today, see you next week!!!