Digital Reference

Introduction to programming --- C++

Overview
Home
Digital Reference --- C++ for beginners, Introduction

Welcome to the realm of C++!  As you sit in front of your computer and use various types of programs, it seems as if the computer has a mind of its own.  It performs tasks from the press of a key or a click with the mouse.  Everything that is performed by the computer is actually all done under the brains of human beings who created the programs.  Let's use an analogy:  imagine you are a movie producer.  In order to make a movie to viewers, you need to write scripts.  Now imagine yourself as a computer programmer.  You have to write C++ codes to create programs. Get the picture?

At Digital Reference, you will learn some vital information about getting into the programming world through our lessons of the C++ language. Some may say that it is difficult for first time learners to start programming in C++. We disagree! C++ has its basics, and they are easy to learn. Allow us to lead you to the "backstage" of computer programs. Ready? Let’s go!

Overview
Variables
Conditions
Loops
Arrays
Functions
Files
User-Defined
Pointers
Credits
BBS Discussion
Search
Be Informed: Join us

Subscribe
Unsubscribe 
 
General Info on Programming

Before you can start programming, a compiler must be available. A compiler is a program that deciphers C++ language into the machine (computer) language. Compilers are available in any store that sells software. Some brands include Borland C++ and Microsoft Visual C++. However, there are some free compilers on the Internet as well. Some may be trial software and some are freeware.

Such programs are:.........

How can a bunch of code create an individual program? Well, the code you type into the compiler is called the source code. After compiling the source code, the object code is made. The linker gathers the object codes to create an executable program. In the compiler, there may also be a debugger included that can help find errors in the program.

 

IN THE BEGINNING...

C++ is a highly popular programming language that had to undergo several stages of change and development before a finalized version was developed in November of 1997.

C++ has its roots in several programming languages, spanning a period of approximately 40 years. An early ancestor of C++ was BCPL, developed by Martin Richards in 1967. It was an attempt to make programming a little simpler, compared to its predecessor, CPL, yet the language was still intangible, and did not offer programmers many options in creating programs.

In 1970, Ken Thompson, co-developer of UNIX, created the B language, a director ancestor of the C language. The B language was much easier than CPL and BCPL, yet it still had its limitations, such as slower execution time and was system dependent.

Therefore, his colleague, Denis Ritchie, also a co-developer of UNIX, created the C programming language in 1972 at Bell Labs, which set the standard for programming languages the world over. The C programming language introduced a compiler that would be able to generate executable code directly, as well as new concepts to language, such as data types.

 

Bjarne Stroustrup is the man who is credited for the development and naming of C++. He started tooling around with C in the early 1980s, and finally published a book on its fundamentals and usage in October 1985.

In the past decade, C++ has undergone many revisions, and has endured several criticisms. Some programmers claim that C++ is just a copycat language of C, and that there are no significant changes made on its part. However, according to Stroustrup himself, C++ is a better language on the basis that it "is a better C, supports data abstraction, supports object-oriented programming, [and] support generic programming." Whether or not C++ is a better language depends solely on the user, however it can be agreed upon that C++, as well as all other programming languages, are fun and interesting to learn.

BIBLIOGRAPHY
History of C++ - http://www.cplusplus.com/info/history.html
Stroustrup: C++ - http://www.research.att.com/~bs/C++.html
Dennis Ritchie Homepage - http://www.cs.bell-labs.com/who/dmr/
The Creation of the UNIX Operating System: Ken Thompson Biography - http://www.bell-labs.com/history/unix/thompsonbio.html

 

Digital Reference---Getting Started

You are about to take your first step into C++ language.  We, at Digital Reference, will guide you all the way to making a complete, working  program.   Let us get a feel of C++ by making a simple program together.  The following rules are very essential to building a program:

Rule No. 1:

Always start your program first by making comments.  Comments give you a general idea of what your program does.  For example, if you open your .cpp file (C Plus Plus file), you will see a bunch of codes.  You may not remember what kind of program that file was written for, and then, you will have to look through the entire code to decipher what kind of program it is.  Therefore, commenting programs will be an advantage.

In order to make comments, type:

 /*type comments about your program here between the asterisks and the slashes*/

To make comments anywhere in your program in order to recognize what your code does, you can type before or after your code in such a manner:

//your comment here

Rule No. 2:

After writing the comment for the program, you should type on the next line:

#include <iostream.h>

This statement means to include the iostream library.  In C++, a library is an assortment of codes that can be utilized by the program to do certain assignments.  In the case of the iostream, this library allows the program to input and output.  (iostream means input output stream.)

The section where you type #include <header file> is called the preprocessor.

Hey there, how’s it going so far? Let’s get into the main part of the program, shall we?

Rule No. 3:

On the next line, type:

 int main()
{
}

What you see above in red is the main function. A function consists of lines of codes that tells the program what to do. The main function IS the core of the entire program. The codes that you will type between the braces will be the assignments of the program. int means that the function will return an integer. After writing the statements/assigning tasks to the program, you want to put on the line before the closing brace:

return(0);

return(0); means that you want the main function to return the integer 0. When the program returns the integer 0, it means that the program can run successfully.

Okay, here comes some FUN! We will now assign the program to do something! Excited? We sure hope so!

Rule No. 4:

In order to have the program display something on the screen, you can type within the main function:

cout << "Type message to be displayed here";

cout means console output stream. You want your message to be displayed. In another words, let us say you want your message to flow through the stream and out onto the screen. The << points the direction that the message should flow towards. << are called insertion operators. The semicolon is ALWAYS used after a statement. Otherwise, you will have an error in your program.

Now, here’s the goodie! We will construct your FIRST PROGRAM. Let’s have the program display the message, "Hi, this is your first working program!"

/*This is your first program displaying a message.*/

#include <iostream.h>

int main()

{
    cout << "Hi, this is your first working program!";
    return(0);
}

Rule No. 5:

When you are typing on your word processor, you press return to go to the next line. However, in C++, if you want to display something on the next line, you need to type endl.

endl means "end of line." Using endl, you can display anything on the next line.

For example:

If we want the program to display,

Hi!

How are you?

One way to write the code to display that on the screen is:

#include <iostream.h>

int main()

{
      cout << "Hi!" << endl;
      cout << "How are you?" << endl;
      return(0);
}

Rule No. 6:

To have the user of the program enter some data, you need to include cin. cin stands for console input stream. You also need to include the extraction operators >>. As you can see, these operators point in, unlike the insertion operators used with cout.

Let’s make another simple program asking the user to enter data.

#include <iostream.h>

int main()

{
    int Age;
    cout << "How old are you?";
    cin >> Age;
    cout << endl << "You are " << Age << " years old." << endl;
    return(0);
}

CONGRATULATIONS! You have just made a successful program. You ought to be very proud of yourself to have come to this stage. KEEP UP THE GOOD WORK! Please stay with us, and you will learn many more interesting codes that can help you elaborate your existing program.

NEXT

 

ThinkQuest Team C008294