What is C++?

C++ is an object-oriented programming language. It was developed by Bjarne Stroustrup at  AT&T belt laboratory in murry hill, New Jersey, USA, in the early 1980s. Stroustrup, an admirer of simula67 and a strong supporter of C, wanted to combine the best of both the language and create a more powerful language that could support object-oriented programming features and still retain the power and elegance of c . The result was c++ therefore c++ is an extension of c with a major addition of  the class construct features of simula67. Since the class was a major addition to the original c language. Important features add in c++ are -> Inheritance ,classes,function overloading and operator overloading.

 

Applications of C++

  1. C++ is a versatile language for handling very large programs.
  2. C++ allows us to create hierarchy-related  objects, we can build special object-oriented libraries which can be used later by many programmers.
  3. While C++ is able o map the real-world problem properly, the C part of C++ gives the language the ability to get close to the machine-level details
  4. C++ programs are easily maintainable and expandable. when a new feature needs to be  implemented it is very easy to add to the existing structure of an object
  5. It is expected that C++ will replace  C as a general-purpose language in the near future.

 

BASIC CONCEPT OF OBJECT-ORIENTED PROGRAMMING

The concept used extensively in object-oriented programming

These include:

  • ·        Objects
  • ·        Classes
  • ·        Data abstraction and encapsulation
  • ·        Inheritance
  • ·        Polymorphism
  • ·        Dynamic binding
  • ·        Message passing

Program features:

Like c, the c++ program is a collection of functions. The above example contains only one function, main().                                                                                                                                                                                                          As usual ,execution begins at main(). Every C++ program must have a main().c++ is a free-form language. With few exceptions, the compiler ignores carriage returns and white spaces. Like c, the c++  statements terminate with semicolons.

Comments

C++ introduces a new comment symbol // (double slash ). Comment start with a double slash, the double slash makes a single line as a comment in the program.  The comment ensures the compiler that the  comment is not a part of the program

Example : // This is not a part of the program.

 

A simple C++ program

C++ is a programming language, but what exactly is a program? A program is a set of instructions to execute in a sequence way to give the desired output.

Let's do our first program: print  ”hello word”

#include <iostream>

// Program prints out "Hello, World" to screen

 int main()

{                                                                                                                                                   std::cout<< "Hello, World."<<std::endl;

 return 0;      }

  

The hash (#) include is used when we want to include the header files from the library in the compiler. The Turbo C compiler is used to execute code. In this case, we are including a standard C++ library in our program. The file we want to include is then specified inside the angle brackets <>. Here, we are including a file called iostream.h. This file handles the input and output of data to the console/screen.

After   #include<iostream> , the double slash // is called a comment. Comments in code are not executed by the program. They are mainly to tell the person about the code or any block of code to easily understand by the coder. It is good practice to comment your code so that when you look at code you wrote a year ago, you will know what the code was doing then.

main() is a function that tells the compiler from where the main program is started. We will cover functions shortly, but the main function is the first function that is executed in a program, also called the entry point. A function is used to perform a certain task. Here, the printing of Hello, World is tasked to the main function. The contents that need to be executed must be enclosed in the curly brackets of the function. The int is a keyword from the set of 32 keywords and main is a function, int main() , suggests that the function will return an integer. This is why we have returned 0 at the end of the main function, suggesting that the program executed and the program can terminate without errors.

When we want to print out something to the console/screen, we use the std::cout (console out) C++ command to send something to the screen. Whatever we want to send out should precede and end with the output operator, <<. <<std::endl is another C++ command, which specifies that it is the end of the line and nothing else should be printed on this line afterward. We have to use the prefix before the std:: code to tell C++ that we are using the standard namespace with the namespace std. But why are namespaces necessary? We need namespaces because anyone can declare a variable name with std. How would the compiler differentiate between the two types of std? For this, we have namespaces to differentiate between the two.

Note that the two lines of code we have written in the main function have a semicolon (;) at the end of each line. The semicolon tells the compiler that this is the end of the instructions for that line of code so that the program can stop reading when it gets to the semicolon and go to the next line of instruction. Consequently, it is important to add a semicolon at the end of each line of instruction as it is mandatory.

The two lines of code we wrote before can be written in one line as follows:

std::cout<< "Hello, World."<<std::endl;                                                                                                                   return 0;

Even though it is written in a single line, for the compiler, there are two instructions with both instructions ending with a semicolon. The first instruction is to print out Hello, World to the console and the second instruction is to terminate the program without any errors. It is a very common mistake to forget semicolons and it happens to beginners as well as experienced programmers every now and then. So it's good to keep this in mind, in case you encounter your first compiler errors.

Some important terms in the  C++

  • ·       Program basics
  • ·       Variables
  • ·       Operators
  • ·       Statements
  • ·       Iteration
  • ·       Functions
  • ·       Arrays and pointers
  • ·       Struct and Enum
  • ·       Classes and inheritance

Post a Comment

Previous Post Next Post