- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Hello Word

Posted by Tayyab



Right, now we can get into some programming! Your first program will be the simplest and most widely used example in learning a programming language; writing a single phrase on the screen. Seem simple? It is, but is still much more complicated than just saying it aloud! First I shall present you with the code, and then we’ll look over it and delve into what it actually means.

1
2
3
4
5
6
7
8
9
#include <iostream.h>
using namespace std;

int main()
{
    cout << "Hello, World!";
    return 0;
}

Note: >> Second statement not allowed in TurboC++ 3.0 editor so please remove it if you use this example in TurboC++ 3.0.


The first line tells the compiler that we want to link the iostream library to our program. A library is is a kind of repository that stores little snippets of code, called functions, that you can use. They also store other things too, such as variables and occasionally operators. More on all of that shortly. The iostream library contains code that allows us to send characters into a text stream, which is then fed into the screen. Think of it as an actual river; we are the river’s source and the screen is the sea. Whatever is put into the river from the source will go onto the screen. Maybe a bit over-analogised, but it’s still a good way of thinking about it. This library also lets us read this stream and modify it as it flows.

The next line tells the compiler that we want to use the namespace called std, which is the standard namespace. A namespace is a sort of area in which your code is stored. This is very useful if you want to have two pieces of code that have the same name. Normally pieces of code cannot share the same name, but if one is in one namespace and one is in the other then they can. They are in separated places, but come together in your code. If you’re confused it doesn’t matter, they aren’t really important now and we’ll cover them in depth later. Since the iostream library uses the standard namespace, that is what we shall use.

The third line of code is the beginning of the actual code. It is the main code block, as it’s name implies. Everything inside those two braces ({}) is read and translated by the compiler. You will need this line in every program, because otherwise nothing will happen, and a program that does absolutely nothing is pretty useless!

Next comes the actual printing of our phrase, which in this case is ‘Hello, World!’. To do this we use some code that is stored inside the iostream library that we discussed earlier. This piece of code is called a statement, the cout, which sends some information into the stream that we discussed earlier. The << after it is called an operator. This one means “output”, so together with cout it means 'output the following to the stream'. The following in question is 'Hello, World!', and is called a string. A string is just a sequence of characters, and is shown by enclosing it inside double quotation marks. The coutstatement doesn't just output strings though, as you will see later. It is much more powerful than that. Note that this line ends with a semi-colon (;). This signifies the end of a statement, and if not used the program will not compile.

The last line, not including the closing brace of the main code block, is simple, but important. It is always placed at the end of main (although it can be in other places as well) and tells the computer that the main block is finished; it returns a value of 0 to the CPU. A non-zero value usually denotes an error, so it is a very useful line.

Now that you've gotten the so-called source code of a program you need to compile it. We mentioned this previously in the previous section. If your program closes straight away once you run it, then try running it from a terminal/cmd prompt instead.
Well, that little explanation could have been a lot longer, but I think that will be enough for now.



<< Previous                                                                                                                 Next >>    

0 comments:

Post a Comment

Introduction

Posted by Tayyab


What is C++?

C++ is a statically typed, compiled, general-purpose, case-sensitive, free-form programming language that supports procedural, object-oriented, and generic programming.
C++ is regarded as a middle-level language, as it comprises a combination of both high-level and low-level language features.
C++ was developed by Bjarne Stroustrup starting in 1979 at Bell Labs in Murray Hill, New Jersey, as an enhancement to the C language and originally named C with Classes but later it was renamed C++ in 1983.
C++ is a superset of C, and that virtually any legal C program is a legal C++ program.
Note: A programming language is said to use static typing when type checking is performed during compile-time as opposed to run-time.
Well, that sure is a question, and many people will give you many answers, but the simplest one is this; It is an object-orientated, compiled programming language. If you’ve never programmed before, even that answer may be confusing, so let me explain.
First, the “compiled” part. There are two different types of programming languages; interpreted languages and compiled languages. Interpreted languages are run by an aptly-named “Interpreter” program, which reads your program’s code and then executes each command one by one. Compiled languages, on the other hand, are not read by programs at all, but are read by your computer’s CPU. However, as some of you may already know, computers can’t read letters. They only understand binary numbers, so first your code must be translated into computer readable machine code. This process is named compiling. In a nutshell, an interpreted language is read straight away by a normal program, but a compiled language is first translated and then read by the CPU. C++, as previously mentioned, is a compiled language.
Now for the “object-orientated” bit. Basically, this is just the way the code is structured. This won’t matter to begin with, so I won’t explain it now. We’ll be covering it later, don’t worry.

What does this all mean to me?

As a C++ programmer you will need more than just a text editor. There are very few programming languages where that is all you need, and they aren’t really true programming languages at all. Anyway, one of these things you will need is, obviously, a compiler. 
Another thing you will probably want to use is an IDE, which stands for Integrated Development Environment. This is basically a programmer-orientated text editor, which has syntax highlighting and normally compiler integration. For both Linux and Windows I recommend Code::Blocks, but there are many other great options such as Dev-C++, TurboC++ 3.0 and GNU Emacs. You don’t need a dedicated IDE however, a basic text editor will suffice, although something like Notepad can certainly make things difficult. Word processors are definitely not the best choice, since they usually have annoying spell correction and automatic formatting to suit writing, and not coding, styles.

Is there anything else I should know?

Yes, as a matter of fact there is. For a start, C++ has very rigid syntax. While it doesn’t really care about white-space, like Python does, it is case-sensitive, so beware of what you type. “Hello” is different to “hello” in C++’s view.
Well then, that’s about it as regards to what is it, and I’m guessing you probably want to get into some actual programming, don’t you? All right then, here we go.
OVER VIEW

Object-Oriented Programming

C++ fully supports object-oriented programming, including the four pillars of object-oriented development:
  • Encapsulation
  • Data hiding
  • Inheritance
  • Polymorphism

Standard Libraries

Standard C++ consists of three important parts:
  • The core language giving all the building blocks including variables, data types and literals, etc.
  • The C++ Standard Library giving a rich set of functions manipulating files, strings, etc.
  • The Standard Template Library (STL) giving a rich set of methods manipulating data structures, etc.

The ANSI Standard

The ANSI standard is an attempt to ensure that C++ is portable -- that code you write for Microsoft's compiler will compile without errors, using a compiler on a Mac, UNIX, a Windows box, or an Alpha.
The ANSI standard has been stable for a while, and all the major C++ compiler manufacturers support the ANSI standard.

Learning C++

The most important thing to do when learning C++ is to focus on concepts and not get lost in language technical details.
The purpose of learning a programming language is to become a better programmer; that is, to become more effective at designing and implementing new systems and at maintaining old ones.
C++ supports a variety of programming styles. You can write in the style of Fortran, C, Smalltalk, etc., in any language. Each style can achieve its aims effectively while maintaining runtime and space efficiency.

Use of C++

C++ is used by hundreds of thousands of programmers in essentially every application domain.
C++ is being highly used to write device drivers and other softwares that rely on direct manipulation of hardware under realtime constraints.
C++ is widely used for teaching and research because it is clean enough for successful teaching of basic concepts.
Anyone who has used either an Apple Macintosh or a PC running Windows has indirectly used C++ because the primary user interfaces of these systems are written in C++.



<< Previous                                                                                                                 Next >>    

0 comments:

Post a Comment