Jump to content
xisto Community
Sign in to follow this  
Codemaster Snake

C++ Tutorial I Worte Back In 2003

Recommended Posts

Download it from here! in html

WELCOME TO AN EXCITING JOURNEY INTO PROGRAMMING WITH C++!

This Tutorial is written for all those beautiful minds there who are interested in computer programming but don't know where to start. Well, You have just came the right place. This tutorial is for an absolute beginner. Since, I am writing this tutorial for a beginner I will only teach basics.


HISTORY OF C++

The C++ language was developed at AT & T bell Laboratories in the early 1980's by Bjarne Stroustrup. It as originally known as 'C with classes' as two languages contributed to its design: C, which provided the class concept. C++ is an example of an object-oriented language. Other object-oriented languages include Java and Smalltalk.

C++ language is a superset of C. Like the C language C++ is compact and can be use for system programming. It can use the existing C software libraries. (Libraries are collections of programs that you can reuse in your program.) C++ also has object-oriented programming (OOP) capabilities similar to an earlier language called Simula67. C++ is called a hybrid language because it can be used both as a procedural language like C or as an object-oriented language like Simula67. Other object-oriented languages include Smalltalk and Ada.


C++ is a 'Programming Language' in which we will create 'Programs'.

A program is a set of instructions that are written to carry out some meaningful task. In other words, you program a computer to do some work. This is done by writing a set of lines. This program is written in a specific language which a computer can understand. Since a computer is going to some work for us so, it is necessary for us to write this set of lines in a language which a computer can understand and interpret. This language is called 'Programming Language'. We will use C++ for this.



You will need a C++ compiler to make your own program. I assume that you have 'Turboc3 IDE' and know how to use it.





Before we start our programming let us try to have an idea about a c++ program. Let us take a real life example:-

Suppose your mom asks you to bring 1 Kg of Apples from market. So, what would you do ? You will first take the money and a bag with you. Then you will then go to market and ask the greengrocer to give you a Kg of Apples. Then you will take them and get back to home and give Apples to your mom.

As you can see, every thing is done in systematic way. So is done in c++.

That's it. You are now ready to create your own programs.

Now let us write our first c++ program :

Program # 1 :



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


#include<iostream.h>

void main()

{

cout << "Hello, This is my first c++ program.";

}



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


This is a very very basic program. Save this code (those set of lines written is called a code) as filename.cpp. Compile and run it.

When you run this program you will see following output :



Now, Let me explain this code to you.

#include <iostream.h> This is called preprocessor directive. This statement lets computer know that what functions you are going to use in your program (this is similar to including money and bag in your journey to market).

iostream.h is a name of a header file which contains some functions in it which we used. There are lots of them. I have listed few of them below:

string.h
It contains functions used for string manipulation.

conio.h
It contains functions used for screen manipulation.

stdio.h
It contains some standard functions.

fstream.h
It contains functions used for file manipulation.

dos.h
It contains some dos related functions


for e.g.

#include <stdio.h>

#include <dos.h>

void main() is a function (it is called main function). Any word in c++ ending with '()' is called a functions. there are thousands of functions in C++ stored in their corresponding Header File. That is why we have so many Header Files. This is same as those books which are arranged subject wise in a library to avoid confusion. Even a computer can get confused;)

{ this is the starting point of a program (we also started our journey from our home).

cout << "Hello, This is my first c++ program."; this statement tells computer to print 'Hello, This is my first c++ program.'. cout << is defined in iostream.h header file. That's why we have included iostream.h. You should enclose your message between " " otherwise it will give error (more on this later).

Notice that semicolon at the end of that statement. It is called string terminator. Each statement must end with it.

} this is the ending point of a program (we also ended our journey at our home).

Each and every c++ program starts at main() and ends at main().

Next comes comments.

// or /* */ anything written after this is ignored by compiler.

for e.g..

// this is a single line comment.

/* this is also a comment */

// this may be used if you want to give a single line comment

/* this may be used

if you want many lines

to be commented*/

Some of you would be thinking that if compiler ignores comments then why use it? Well, these can be used in many ways. You can use them at the top of your program for future reference to know what your program is about just reading it which is better than getting to conclusion after reading 7869 lines of code;).

We can modify our code as :

Program # 2 :



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


#include <iostream.h>
/* this is called preprocessor directive. it tell compiler what functions we are going to use in our program*/




void main()
/*this is a function main(). Each and every program must have this.*/




{
/*this is the starting point of our program.*/




cout<<"Hello, This is my first c++ program.";
/*here we write our instructions which will be followed by computer.*/




}
/*this is the ending point of our program.*/




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


Output of program # 2 will be:



Difficult to understand, don't worry it will take time to get familiar with that. For now just learn it by heart.

Now let us do something else.

Program # 3 :



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


#include<iostream.h>

void main()

{

cout << "My Name is Neeraj.";

}



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


Output of program # 3 will be :



When computer executes our program it searches for main() function then reads first line of main() and executes it. Then it reads second line and executes it, then it executes third and so on. In our program we have only one line in main() so computer will read it and executes it then it encounters } and program in then ended.

Let us elaborate our code :

Program # 4 :



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


#include<iostream.h>

void main()

{

cout << "******************";

cout << "\n";

cout << "My Name is Neeraj.";

cout << "\n";

cout << ******************";

}



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


Output of Program #4 will be :



Nice graphics;)

Let us take a look at the code of above program. You would be able to see some four new statements.

Well, don't panic that's nothing.

Let us understand what's going on :

cout << "******************";
This prints ******************

cout << "\n";
This is called new line character. It starts a new line

cout << "My Name is Neeraj.";
This prints My Name is Neeraj.

cout << "\n";
It also starts a new line

cout << ******************";
This prints ******************


If the above code is :



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


#include<iostream.h>

void main()

{

cout << "******************";

cout << "My Name is Neeraj.";

cout << ******************";

}



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


Then the output will be :



See that difference.

Try Printing Something else. Remember more you practice more you learn.



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


Practice Questions

#1 Write a program to print your name on the screen.

#2 Write a program to print following :

************

******

**

#3 Write a program to print following :

*

* *

********



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

Home Chapter # 2 Chapter # 3 Chapter # 4


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


Have Suggestions? Then, Email Me



If you like this tutorial then please vote me at Planet-Source-Code

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


I created this tutorial back in 2003... just want to share those momments..

Notice from BuffaloHELP:
We appreciate your tutorial, however, excessive copy of published material is not part of quality contribution to our forum. Refrain from copying so much but express with your own, genuine words when making your post in our forum. Thank you.


Edited the post as said!
Edited by Codemaster Snake (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.