Introduction to C++
C++, in Computer Science, an object-oriented version of the C programming language, developed by Bjarne Stroustrup in the early 1980s at Bell Laboratories
official Website of Creater of C++ Mr Bjarne Stroustrup http://www.stroustrup.com/
What is C++
- C++ is an object oriented programming language.
- C++ is an extension of C with a major addition of the class construct feature.
- C++ is superset of C.
- Features like classes, inheritance, function overloading, and operator overloading make C++ a truly object-oriented language.
- OO features in C++ allow programmers to build large programs with clarity, extensibility and ease of maintenance, incorporating the spirit and efficiency of C.
A C++ program is a collection of commands, which tell the computer to do “something”. This collection of commands is usually called C++ source
code, source code or just code.
C++ compiler which will be used to compile your source code into final executable program.
Most C++ compilers don’t care what extension you give your source code, but if you don’t specify otherwise, many will use .cpp by default
Most frequently used and free available compiler is GNU C/C++ compiler, otherwise you can have compilers either from HP or Solaris if you have respective Operating Systems
Before we can write and execute our first program, we need to understand in more detail how programs get developed. Here is a graphic outlining a simplistic approach:
Step 1: Define the problem that you would like to solve.
This is the “what” step, where you figure out what you are going to solve. Coming up with the initial idea for what you would like to program can be the easiest step, or the hardest. But conceptually, it is the simplest. All you need is a an idea that can be well defined, and you’re ready for the next step.
Step 2: Determine how you are going to solve the problem.
This is the “how” step, where you determine how you are going to solve the problem you came up with in step 1. It is also the step that is most neglected in software development. The crux of the issue is that there are many ways to solve a problem — however, some of these solutions are good and some of them are bad. Too often, a programmer will get an idea, sit down, and immediately start coding a solution. This almost always generates a solution that falls into the bad category.
Typically, good solutions have the following characteristics: * They are straightforward * They are well documented * They can be easily extended (to add new features that were not originally anticipated) * They are modularized
The problem is largely with the third and fourth bullets — while it’s possible to generate programs that are straightforward and well documented without using a lot of forethought, designing software that is extensible and sufficiently modularized can be a much tougher challenge.
As far as extensibility goes, when you sit down and start coding right away, you’re typically thinking “I want to do _this_”, and you never consider that tomorrow you might want to do _that_. Studies have shown that only 20% of a programmers time is actually spent writing the initial program. The other 80% is spent debugging (fixing errors) or maintaining (adding features to) a program. Consequently, it’s worth your time to spend a little extra time up front before you start coding thinking about the best way to tackle a problem, and how you might plan for the future, in order to save yourself a lot of time and trouble down the road.
Modularization helps keep code understandable and reusable. Code that is not properly modularized is much harder to debug and maintain, and also harder to extend later. We will talk more about modularization in the future.
Step 3: Write the program
In order the write the program, we need two things: First we need knowledge of a programming language — that’s what these tutorials are for! Second, we need an editor. It’s possible to write a program using any editor you want, be it Window’s notepad or Linux’s gedit. However, we strongly urge you to use an editor that is designed for coding.
A typical editor designed for coding has a few features that make programming much easier, including:
1) Line numbering. Line numbering is useful when the compiler gives us an error. A typical compiler error will state “error, line 64?. Without an editor that shows line numbers, finding line 64 can be a real hassle.
2) Syntax highlighting and coloring. Syntax highlighting and coloring changes the color of various parts of your program to make it easier to see the overall structure of your program.
3) An unambiguous font. Non-programming fonts often make it hard to distinguish between the number 0 and the letter O, or between the number 1, the letter l (lower case L), and the letter I (upper case i). A good programming font will differentiate these symbols in order to ensure one isn’t accidentally used in place of the other.
Your C++ programs should be called name.cpp, where name is replaced with the name of your program. The .cpp extension tells the compiler (and you) that this is a C++ source code file that contains C++ instructions. Note that some people use the extension .cc instead of .cpp, but we recommend you use .cpp.
Also note that many complex C++ programs have multiple .cpp files. Although most of the programs you will be creating initially will only have a single .cpp file, it is possible to write single programs that have tens if not hundreds of individual .cpp files.
Step 4: Compiling
In order to compile a program, we need a compiler. The job of the compiler is twofold: 1) To check your program and make sure it follows the syntactical rules of the C++ language: 2) To take your source code as input and produce a machine language object file as output. Object files are typically named name.o or name.obj, where name is the same name as the .cpp file it was produced from. If your program had 5 .cpp files, the compiler would generate 5 object files.
For illustrative purposes only, most Linux and Mac OS X systems come with a C++ compiler called g++. To use g++ to compile a file from the command line, we would do this:
"g++" -c file1.cpp file2.cpp file3.cpp *
This would create file1.o, file2.o, and file3.o. The -c means “compile only”, which tells g++ to just produce .o files.
Other compilers are available for Linux, Windows, and just about every other system. We will discuss installing a compiler in the next section, so there is no need to do so now.
For complex projects, some development environments use a makefile, which is a file that tells the compiler which files to compile. Makefiles are an advanced topic, and entire books have been written about them. We will not discuss them here.
Step 5: Linking
Linking is the process of taking all the object files for a program and combining them into a single executable.
In addition to the object files for a program, the linker includes files from the runtime support library. The C++ language itself is fairly small and simple. However, it comes with a large library of optional components that may be utilized by your program, and these components live in the runtime support library. For example, if you wanted to output something to the screen, your program would include a special command to tell the compiler that you wanted to use the I/O (input/output) routines from the runtime support library.
Once the linker is finished linking all the object files (assuming all goes well), you will have an executable file.
Online C++ Complier http://www.compileonline.com/compile_cpp_online.php
Next : C++ Basics