SlideShare una empresa de Scribd logo
1 de 6
Descargar para leer sin conexión
Beginners Guide to C++ - Chapter 1



                        Chapter 1 – Introduction



Hi! Welcome to my beginners guide to C++. If you are starting to program for the
first time, I hope that you find the chapters I have written useful. C++ is an excellent
language to start programming in – a lot of applications that you use are probably
written in c++ and once you learn some basic concepts, learning other languages, like
java for example, will be much easier.
There are 8 chapters altogether, including this one. I have kept the chapters short and
concise so you won’t get bored or weighed down by too much information. After each
chapter you can rearrange the code in the examples provided or make up your own
code. Programming is very much a practical subject so you will learn a lot by messing
about with code or even looking at other people’s code.




What is a computer program?


A computer program is a set of instructions that a programmer writes to tell a
computer how to carry out a certain task. The instructions, however, must be in a
language that the computer understands. Computers only understand binary language
i.e. that composed of 1’s and 0’s. This is a low level language and very hard to
program in. So humans invented higher level languages such as C++ or Pascal to
make the job easier. As you will see, these languages are nearly like English but you
don’t have the freedom to write what you like – there are still rules you have to
follow.
To convert a program in C++ to a binary or executable file that the computer can
understand we use a compiler. The compiler which I recommend using for this guide
is Borland’s command line compiler. This compiler is completely free to use and can
be downloaded from:
http://www.borland.com/products/downloads/download_cbuilder.html Or you could
do a search on google for a free Borland compiler.
Beginners Guide to C++ - Chapter 1


Setting up Borland’s command line compiler


Now I will show you how to set up the Borland compiler as it can be a bit confusing if
you are new to this stuff.
When you download the Borland compiler you will see a file like the following:




Step 1: Click on the file and install it to the default directory C:BorlandBCC55


Step 2: Open notepad and type the following lines:
                       -I"c:BorlandBcc55include"
                       -L"c:BorlandBcc55lib"
Save this file in C:BorlandBCC55BIN as bcc32.cfg
Make sure you choose “All Files” when saving or it will be saved as a text file.
Beginners Guide to C++ - Chapter 1



Step 3:

Open notepad again and type:

                       -L"c:BorlandBcc55lib"

Save this file in C:BorlandBCC55BIN as ilink32.cfg

Again, make sure you choose “All Files” when saving or it will be saved as a text file.

That’s it! All the paths are set up and now you can get stuck into some programming!




Writing your first program in C++

With the Borland compiler, you type your program into a text editor such as notepad
then you compile and run it from the command line.


So open notepad and type the following code into it.

   1      #include<iostream.h>
   2
   3      int main()
   4      {
   5
   6      cout<<"Hello World!"<<endl;
   7
   8      return 0;
   9
  10      }
                                     Example 1a

Save it in C:BorlandBCC55BIN as p1.cpp

Remember to choose “All files” when saving.




Compiling your first program in C++

Now your program is saved and we need to compile it. First we need to open up a
command line.
Beginners Guide to C++ - Chapter 1



Click on Start, and then run. In the box that comes up type “command” and a
command line will pop up.




Navigate to the folder where your file is saved by typing “cd c:borlandbcc55bin”
and press enter.
The name of the compiler is bcc32.exe so to compile a program all we need to do is
type bcc32 followed by the name of the program.
In our case we should type: bcc32 p1 (you don’t have to type the .cpp bit here).


Now press enter and the program will be compiled.


You shouldn’t get any errors if you typed the program correctly but if you did you
will be notified here.


To run the program just type p1 and bingo! “Hello World!” is printed on the screen.
Congratulations! You’ve just written and compiled your first program in C++!


To run the program again just type p1 followed by enter. You only need to re-compile
the program if you make any changes.
Beginners Guide to C++ - Chapter 1




Understanding your first program

The first line tells the compiler to include a file called "iostrem.h". You will find this
file in a folder called "include" in “C:BorlandBCC55. This file tells the compiler
what certain keywords mean, such as "cout" on line 6.
For instance later on you will see that you will have to include <math.h> in order to
use reserved or keywords like "sine" and "rad". You will probably start all of your
programs with <iostream.h>


Line three, "int main()", is the header for the main function- where most of the
programming stuff happens. Later on you will learn to write your own functions
which can be called from the main function. Notice that the body of the function is
enclosed in brackets, {....}. At the end of the function there is a line "return 0;". This
tells the computer that when main() is finished what it is doing it returns or does
nothing.


Line 6, cout<<"Hello World!"; is called a statement. All statements end with a semi
colon. It is very easy to forget to put this in when you begin programming. "cout" tells
the computer to print on the screen the text between the pair of quotes. cout stands for
Beginners Guide to C++ - Chapter 1


"Channel OUT". Also notice the two pairs of arrows after cout and their direction. To
remember their direction, think of data flowing "out" to be printed on the screen.
"endl" tells the computer to go onto a new line after it prints the current one. This is
useful if you want different sentences on different lines.
To put a space in between lines printed on the screen, type
cout<<" "<<endl; in between the code lines.


So now that you know how to write a program that prints something, you can edit the
above program to output whatever you like. Repeat line 6 above to print out a few
sentences in the same program.




As you read through the various sections of this guide let me know what you think.
You can email me at dermot.mceneaney2@mail.dcu.ie

Más contenido relacionado

La actualidad más candente

La actualidad más candente (12)

Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Qbasic Tutorial
Qbasic TutorialQbasic Tutorial
Qbasic Tutorial
 
Q basic ch
Q basic chQ basic ch
Q basic ch
 
Introduction to qbasic
Introduction to qbasicIntroduction to qbasic
Introduction to qbasic
 
Switch case and looping kim
Switch case and looping kimSwitch case and looping kim
Switch case and looping kim
 
What comments hide
What comments hideWhat comments hide
What comments hide
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)
 
Qbasic introduction
Qbasic introductionQbasic introduction
Qbasic introduction
 
Tips and Tricks for Using Visual Studio.Net Effectively
Tips and Tricks for Using Visual Studio.Net EffectivelyTips and Tricks for Using Visual Studio.Net Effectively
Tips and Tricks for Using Visual Studio.Net Effectively
 
Php notes 01
Php notes 01Php notes 01
Php notes 01
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
 
Margareth lota
Margareth lotaMargareth lota
Margareth lota
 

Destacado

(Azo) ald로 성장한 ald 특성(영)
(Azo) ald로 성장한 ald 특성(영)(Azo) ald로 성장한 ald 특성(영)
(Azo) ald로 성장한 ald 특성(영)scraptor
 
Bab 1 m apos setting stad untuk meningkatkan pemahaman konsep homomorfisma grup
Bab 1 m apos setting stad untuk meningkatkan pemahaman konsep homomorfisma grupBab 1 m apos setting stad untuk meningkatkan pemahaman konsep homomorfisma grup
Bab 1 m apos setting stad untuk meningkatkan pemahaman konsep homomorfisma grupnazihah zuhrotun
 
Integrating Technology in a Classroom Lesson: Step-by-Step instructions on ho...
Integrating Technology in a Classroom Lesson: Step-by-Step instructions on ho...Integrating Technology in a Classroom Lesson: Step-by-Step instructions on ho...
Integrating Technology in a Classroom Lesson: Step-by-Step instructions on ho...maganharrell
 

Destacado (9)

(Azo) ald로 성장한 ald 특성(영)
(Azo) ald로 성장한 ald 특성(영)(Azo) ald로 성장한 ald 특성(영)
(Azo) ald로 성장한 ald 특성(영)
 
Wv powerpoint
Wv powerpointWv powerpoint
Wv powerpoint
 
Tugas dan uts kelas a
Tugas dan uts kelas aTugas dan uts kelas a
Tugas dan uts kelas a
 
Nilai jadi di siakad a
Nilai jadi di siakad aNilai jadi di siakad a
Nilai jadi di siakad a
 
Tulang.pptx tik
Tulang.pptx tikTulang.pptx tik
Tulang.pptx tik
 
Tulang.pptx tik
Tulang.pptx tikTulang.pptx tik
Tulang.pptx tik
 
Tulang.pptx tik
Tulang.pptx tikTulang.pptx tik
Tulang.pptx tik
 
Bab 1 m apos setting stad untuk meningkatkan pemahaman konsep homomorfisma grup
Bab 1 m apos setting stad untuk meningkatkan pemahaman konsep homomorfisma grupBab 1 m apos setting stad untuk meningkatkan pemahaman konsep homomorfisma grup
Bab 1 m apos setting stad untuk meningkatkan pemahaman konsep homomorfisma grup
 
Integrating Technology in a Classroom Lesson: Step-by-Step instructions on ho...
Integrating Technology in a Classroom Lesson: Step-by-Step instructions on ho...Integrating Technology in a Classroom Lesson: Step-by-Step instructions on ho...
Integrating Technology in a Classroom Lesson: Step-by-Step instructions on ho...
 

Similar a Chapter1 introduction it2

How to work with code blocks
How to work with code blocksHow to work with code blocks
How to work with code blocksTech Bikram
 
His162013 140529214456-phpapp01
His162013 140529214456-phpapp01His162013 140529214456-phpapp01
His162013 140529214456-phpapp01Getachew Ganfur
 
How to run C Program in Linux
How to run C Program in LinuxHow to run C Program in Linux
How to run C Program in LinuxJohn425873
 
C plus plus for hackers it security
C plus plus for hackers it securityC plus plus for hackers it security
C plus plus for hackers it securityCESAR A. RUIZ C
 
C++ In One Day_Nho Vĩnh Share
C++ In One Day_Nho Vĩnh ShareC++ In One Day_Nho Vĩnh Share
C++ In One Day_Nho Vĩnh ShareNho Vĩnh
 
C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...bhargavi804095
 
Introduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docIntroduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docMayurWagh46
 
Introduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxIntroduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxNEHARAJPUT239591
 
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJmeharikiros2
 
C language industrial training report
C language industrial training reportC language industrial training report
C language industrial training reportRaushan Pandey
 
Introduction of c language
Introduction of c languageIntroduction of c language
Introduction of c languagefarishah
 

Similar a Chapter1 introduction it2 (20)

How to work with code blocks
How to work with code blocksHow to work with code blocks
How to work with code blocks
 
C++ for hackers
C++ for hackersC++ for hackers
C++ for hackers
 
His162013 140529214456-phpapp01
His162013 140529214456-phpapp01His162013 140529214456-phpapp01
His162013 140529214456-phpapp01
 
Session 1 - c++ intro
Session   1 - c++ introSession   1 - c++ intro
Session 1 - c++ intro
 
How to run C Program in Linux
How to run C Program in LinuxHow to run C Program in Linux
How to run C Program in Linux
 
C plus plus for hackers it security
C plus plus for hackers it securityC plus plus for hackers it security
C plus plus for hackers it security
 
C++ In One Day_Nho Vĩnh Share
C++ In One Day_Nho Vĩnh ShareC++ In One Day_Nho Vĩnh Share
C++ In One Day_Nho Vĩnh Share
 
C
CC
C
 
Csharp_Chap01
Csharp_Chap01Csharp_Chap01
Csharp_Chap01
 
2621008 - C++ 1
2621008 -  C++ 12621008 -  C++ 1
2621008 - C++ 1
 
C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...
 
Introduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docIntroduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).doc
 
Cpb2010
Cpb2010Cpb2010
Cpb2010
 
Lab 1.pptx
Lab 1.pptxLab 1.pptx
Lab 1.pptx
 
Introduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxIntroduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptx
 
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
 
C lecture notes new
C lecture notes newC lecture notes new
C lecture notes new
 
Learn C Language
Learn C LanguageLearn C Language
Learn C Language
 
C language industrial training report
C language industrial training reportC language industrial training report
C language industrial training report
 
Introduction of c language
Introduction of c languageIntroduction of c language
Introduction of c language
 

Chapter1 introduction it2

  • 1. Beginners Guide to C++ - Chapter 1 Chapter 1 – Introduction Hi! Welcome to my beginners guide to C++. If you are starting to program for the first time, I hope that you find the chapters I have written useful. C++ is an excellent language to start programming in – a lot of applications that you use are probably written in c++ and once you learn some basic concepts, learning other languages, like java for example, will be much easier. There are 8 chapters altogether, including this one. I have kept the chapters short and concise so you won’t get bored or weighed down by too much information. After each chapter you can rearrange the code in the examples provided or make up your own code. Programming is very much a practical subject so you will learn a lot by messing about with code or even looking at other people’s code. What is a computer program? A computer program is a set of instructions that a programmer writes to tell a computer how to carry out a certain task. The instructions, however, must be in a language that the computer understands. Computers only understand binary language i.e. that composed of 1’s and 0’s. This is a low level language and very hard to program in. So humans invented higher level languages such as C++ or Pascal to make the job easier. As you will see, these languages are nearly like English but you don’t have the freedom to write what you like – there are still rules you have to follow. To convert a program in C++ to a binary or executable file that the computer can understand we use a compiler. The compiler which I recommend using for this guide is Borland’s command line compiler. This compiler is completely free to use and can be downloaded from: http://www.borland.com/products/downloads/download_cbuilder.html Or you could do a search on google for a free Borland compiler.
  • 2. Beginners Guide to C++ - Chapter 1 Setting up Borland’s command line compiler Now I will show you how to set up the Borland compiler as it can be a bit confusing if you are new to this stuff. When you download the Borland compiler you will see a file like the following: Step 1: Click on the file and install it to the default directory C:BorlandBCC55 Step 2: Open notepad and type the following lines: -I"c:BorlandBcc55include" -L"c:BorlandBcc55lib" Save this file in C:BorlandBCC55BIN as bcc32.cfg Make sure you choose “All Files” when saving or it will be saved as a text file.
  • 3. Beginners Guide to C++ - Chapter 1 Step 3: Open notepad again and type: -L"c:BorlandBcc55lib" Save this file in C:BorlandBCC55BIN as ilink32.cfg Again, make sure you choose “All Files” when saving or it will be saved as a text file. That’s it! All the paths are set up and now you can get stuck into some programming! Writing your first program in C++ With the Borland compiler, you type your program into a text editor such as notepad then you compile and run it from the command line. So open notepad and type the following code into it. 1 #include<iostream.h> 2 3 int main() 4 { 5 6 cout<<"Hello World!"<<endl; 7 8 return 0; 9 10 } Example 1a Save it in C:BorlandBCC55BIN as p1.cpp Remember to choose “All files” when saving. Compiling your first program in C++ Now your program is saved and we need to compile it. First we need to open up a command line.
  • 4. Beginners Guide to C++ - Chapter 1 Click on Start, and then run. In the box that comes up type “command” and a command line will pop up. Navigate to the folder where your file is saved by typing “cd c:borlandbcc55bin” and press enter. The name of the compiler is bcc32.exe so to compile a program all we need to do is type bcc32 followed by the name of the program. In our case we should type: bcc32 p1 (you don’t have to type the .cpp bit here). Now press enter and the program will be compiled. You shouldn’t get any errors if you typed the program correctly but if you did you will be notified here. To run the program just type p1 and bingo! “Hello World!” is printed on the screen. Congratulations! You’ve just written and compiled your first program in C++! To run the program again just type p1 followed by enter. You only need to re-compile the program if you make any changes.
  • 5. Beginners Guide to C++ - Chapter 1 Understanding your first program The first line tells the compiler to include a file called "iostrem.h". You will find this file in a folder called "include" in “C:BorlandBCC55. This file tells the compiler what certain keywords mean, such as "cout" on line 6. For instance later on you will see that you will have to include <math.h> in order to use reserved or keywords like "sine" and "rad". You will probably start all of your programs with <iostream.h> Line three, "int main()", is the header for the main function- where most of the programming stuff happens. Later on you will learn to write your own functions which can be called from the main function. Notice that the body of the function is enclosed in brackets, {....}. At the end of the function there is a line "return 0;". This tells the computer that when main() is finished what it is doing it returns or does nothing. Line 6, cout<<"Hello World!"; is called a statement. All statements end with a semi colon. It is very easy to forget to put this in when you begin programming. "cout" tells the computer to print on the screen the text between the pair of quotes. cout stands for
  • 6. Beginners Guide to C++ - Chapter 1 "Channel OUT". Also notice the two pairs of arrows after cout and their direction. To remember their direction, think of data flowing "out" to be printed on the screen. "endl" tells the computer to go onto a new line after it prints the current one. This is useful if you want different sentences on different lines. To put a space in between lines printed on the screen, type cout<<" "<<endl; in between the code lines. So now that you know how to write a program that prints something, you can edit the above program to output whatever you like. Repeat line 6 above to print out a few sentences in the same program. As you read through the various sections of this guide let me know what you think. You can email me at dermot.mceneaney2@mail.dcu.ie