C++ An Introductory Guide For Beginners Francis John Thottungal C++ All Rights Reserved Copyright 2015 Francis John Thottungal This book may not be reproduced, transmitted, or stored in whole or in part by any means, including graphic, electronic, or mechanical without the express written consent of the publisher except in the case of brief quotations embodied in critical articles and reviews. Booktango 1663 Liberty Drive Bloomington, IN 47403 www.booktango.com 877-445-8822 ISBN: 978-1-4689-6322-9 (ebook) Contents C++ is generally a language taught to technical students in college around the world. The main reason C++, a derivative of C, has become important is that it is used in pretty much anything of a serious kind in programming such as developing drivers, graphical user interfaces, operating systems etc. It is a technical language apt for technologists. However, anyone who is interested in programming can master it with some effort. This language has two parts to it- standalone and object oriented.
The approach: In this guide, the approach is to emphasize the basic building blocks of a C++ program. The emphasis is to help one to make simple standalone programs. The examples in this guide are relatively small and perhaps academic. One can download the zip file of all example code used in this guide at the location given at the end of the book. Software Used: There are many C++ editors in the market. I used the Microsoft Visual Express 2010.
To use this software or its latest version, just install the express version or full version after download from http://www.microsoft.com . You may want to do a search for editors especially if use non-windows based computers such as Linux or Mac. As this is not a textbook, the goal of this guide is just for a quick start to understanding how to write basic C++ code. It is intended for those completely new to the subject or programming in general. It might also serve those who prefer less technical jargon and simple examples to get a grasp of the language. 1
The first program Come forth into the light of things, let nature be your teacher. 1
The first program Come forth into the light of things, let nature be your teacher.
William Wordsworth The first program usually written in any programming language class is one that displays the word Hello World. This is a kind of an unwritten tradition. Let us write the code for that now:
#include int main() { std::cout << Hello World!; } |
In the above code: A hash sign (#) indicates directives read and interpreted by the pre-processor . The int main() is the declaration of a function. All C++ programs have a main function. The open brace ({) indicates the beginning of mains function definition, and the closing brace (}), indicates its end.
The std::cout << Hello World! is a C++ statement to be executed. Here << means insertion and cout is the keyword that is needed to output the text All statement ends with a semicolon (;). One can enter blank lines between lines to make the code readable. In C++, one can write several statements in a single line, or each statement can be in its own line. Now, let us add an additional statement to our first program. The output of the code is Hello World! My C++ program.
In other words, the two statements are on the same line. To avoid this use endl at the end of each statement.
#include int main() { std::cout << Hello World!; std:: cout << My C++ program; } |
#include int main() { std::cout << Hello World!; << endl; std:: cout << My C++ program; << endl; } |
C++ supports two ways of commenting code:
// line comment /* block comment */ |
One can write short comments using the line comment. The block comments are used when larger amount of text is required in the comments. Let us add comments to our Hello World program: We will use block comments at the beginning of the program and line comments for the statements.
/*This is a block comment. This program is written to show the usage of comments- block and line*/ #include int main () { std::cout << Hello World!; // prints Hello World! } |
By using the namespace std we can avoid writing std::cout everytime we want something written to the screen. For example:
/* A program to write Hello World. This demonstrates the use of block comments*/ #include using namespace std; int main () { cout << Hello World!; // prints Hello World! } |
Variables can be found in mathematics. You must have seen statements such as: A= 1; or a =5; or a+b= c etc. Variables hold values or data for a period and reference a particular area of memory. Like many other programming languages, C++ uses variables.
Each variable should have a distinct name or identifier. Instead of calling a variable by the letters of the alphabet, we could give the variable useful names such as result as long as the name is not reserved as an exclusive C++ identifier. A valid identifier or variable name is a sequence of one or more letters, digits, or underscores characters (_). Spaces, punctuation marks, and symbols cannot be part of an identifier. In addition, identifiers shall always begin with a letter. They can also begin with an underline character.
In no case can it begin with a digit. C++ language is case sensitive so a capital variable identifier is different from lowercase variable identifier. Some examples of reserved identifiers in C++ are: The list is only a partial one.
asm | else | new |
auto | enum | operator |
bool | explicit | private |
break | export | protected |
case | extern | public |
catch | FALSE | register |
char | float | reinterpret_cast |
class | for | return |
const | friend | short |
const_cast | goto | signed |
continue | if | sizeof |
default | inline | static |
delete | int | static_cast |
do | long | struct |
double | mutable | switch |
Specific compilers may have additional reserved words.
To declare a variable of type integer we write as follows: int a; int b; float firstnumber: double w; char a; We can also declare more than one variable of the same type in a single line as follows: int x, y, z; One can initialize variables. int x=0;
#include using namespace std; int main () { int x=5; // initial value: 5 int y=2; // initial value: 2 int z; z = x + y; cout << z; return 0; } |
Sometimes it is convenient to give a constant value to a variable. For example: const pi= 3.1415926; However if you declare pi without const also the program will work.
Next page