Sunteți pe pagina 1din 13

1

Programming with C++

Lecture 2: Elementary C++ Programing


Sikander Naseem
2

Basic requirements
There are two basic requirements for programing with C++ (see plus
plus)
 A Text Edition: To write, save and edit programming
instructions e.g. two built-in Window’s editors are Notepad,
WordPad.
 A compiler: To translate the set of instructions to machine
language (binary codes). This translation is called
compilation e.g.
• Borland: Borland C++ compilers
• Microsoft: Microsoft Visual C++ compiler
• Open source (free): MinGW system of compilers (MinGW GCC)
3

Basic requirements
 Integrated Development Enviroment (IDE): is a
bundle with built in text editor, compilers and other
tools for programs development e.g.
• Borland: C++ builder and Turbo C++ ( with old Borland C++
compiler)
• Microsoft: Microsoft Visual C++ (2005/2008/2010/23), the
express edition is free to use.
• Open source: Code::Blocks (with built-in MinGW GCC compiler
system and can use other compilers such as from Borland and
microsoft visual C++). It our choice of IDE for this course
4

Using Code::Blocks
 Can be downloaded using the link below.
http://www.codeblocks.org/downloads/binaries
5

First Program
#include <iostream>
int main()
{
std::cout << "Hello world!\n“;
}
• The first line is Preprocessor derivative giving information regarding
the std::cout. The iostream is the library file name. Every program
requires this first line it if there is standard input/output used.
• The second line is also required, it denotes the beginning of the main
program by creating a main () function. Where int is the data type
“integer” and denote the return value after end of this main function
• There can only be one main function. A function is enclosed in { }
6

First Program
std::cout << "Hello world!\n“;

• This is the body of the program


• It send the line "Hello world!\n” to the standard output
stream std::cout. << is the output operator.
• Upon execution the Hello world! Line is printed on the screen
(output device).
• \n is the new line character. It starts the next line.
• Every set of instruction must end with (;).
• Blank Spaces are ignored.
7

Another Program
#include <iostream>
using namespace std;
int main()
{// Prints "Hello world";
cout << "Hello world!\n";
return 0;}
• The line using namespace std; tells the compiler that
prefixes such as std:: is applied in whole program. This make a
program easy to read/write.
• // Prints "Hello world"; is the comment line. This is
removed before compilation. It is good to add explanation in
human of understanding.
• return 0; is optional line. Some compilers expect it.
8

The output operator


 Inserts value in the output stream
 In cout << 66;, cout is the output stream object, and <<
output operator.
 Direction from right (66) to left (cout) so <<.
 In example below 4 output operators are used.
int main()
{// Prints "Hello world";
cout << "Hel”<<”lo w”<<”orld”<<endl;
return 0;}

Here endl; performs same function as character \n.


9

Characters
 Contain standard 26 Latin alphabets in upper and lower
cases. 0-9 Hindi-Arabic numerals.
 Character are stored in computer in integer with set
codes. Most common is the ASCII (Appendix A)
 \n, \t, \a characters.
 String literals
 Numerical values can also be part of those string
literals rather than values.
cout << “Birth date 20-03-1960”endl;
10

Variable and their Declaration


 A variable is representing a data storage location in computer’s
memory
 Deceleration syntax of a variable with name x.
X=value
 Variable can be assigned value during declaration
 Non Assigned variables.

int main()
{int m,n;
m=9; //assign value of 9 to variable m
n=m*14; //assign value of 9*14 to variable n
cout << "m="<<m << " and n="<<n<<endl;
}
11

Program Tokens
 The elements of the program are called tokens
 The tokens include key words such as main, int, <<
 The program use token to check the errors in the
syntax.
int main()
{//This CODE has an error
int m=9
cout << "m="<<m << endl;
}
12

The Input Operator


 Just like the output operator we have Input operator.
Also called get operator

 The input operator >> provide data to the input


stream cin, which in turn stores the data in memory
location assigned for data types.

 Program: Input
13

The End

Thanks for coming

Reference book:
Programming with C++, SCHAUM’s outlines, John Hubbard,
3rd Edition, McGraw Hill, New Delhi

S-ar putea să vă placă și