C++ Intro (Part 3) by Amin Patel

Style: In the first tutorial some people had pointed out that I had not declared main() to return void, this is no longer a standard expected from programmers. Get main() to return int & simply return 0; as the last line in main().

<-- content_start-->

Note: According to the latest standards <iostream.h> is not required it can be simply written as <iostream>, with the standard code that shall be used through out the tutorial (I hope!).

If the standard code in the tutorial does not work on your compiler and you still wish to use <iostream> instead of <iostream.h> just add a line using namespace std; 

& if this does not work you have an old compiler which cannot work with these standards.

<-- Begin Search Code -->

Now Pay attention, with a little explanation and review of the first tutorial we shall proceed. Some people requested for the theoretical details.

- preprocessor 

Preprocessing is done when the #include command is used to add header files. The actual preprocessing is done much before the compiler reaches the code section of your program. The #include command is one of the possible actions of preprocessing.

All Preprocessing is done with

#(Hash/Pound Symbol)

- #include

This a directive to the preprocessor, with the help of the " Include Hash/Pound " the file after the directive s read.

Eg: #include <filename> or #include <filename.h> 

- IO

When generally the topic of input is raised, the first thought that comes to a person's mind is that input means input from a keyboard, mouse, joystick, etc and is outputted to a device such as a disk, monitor (screen) or printer & this thought is technically correct; in explaining the concept of input-output.

- Input is done in streams of bytes or a sequence from devices (e.g.. keyboard, mouse, joystick, etc). 

-In Output bytes flow from the output devices.

A program is divided into bits, bytes, a byte may represent ASCII characters.

The C++ iostream library has many I/O capabilities but for now we are only concerned with a few.

The <iostream> header file defines the cin, cout objects.

 I think the theoretical explanation of the beginning of a program should be enough for now. Let us now get to the 'real' juicy part of programming. (Duh - Writing the code)

- 2 Input/Output Objects ( In relation to the tutorial)

cout (It ain't pronounced as Cowt, its pronounced as C  Out)

The cout operator has already been explained in tutorial 1 but here is a brief review + additions.

cout is a keyword, which is exclusively reserved for C++ defined purposes. It is used to output data info to the standard output device.

cout<<"This is Text"; 

By the way comments ( // ) have been explained previously. 

// << is  known as stream insertion operator. 

// "This is Text" is displayed to the screen.

//The << is also known as left shift operator, it is used after the cout command.

cout<<text; 

//The data from a variable text is displayed to the screen.

//Variables have not yet been explained at this point

Now to explain the next topic cin & datatype (int), we shall attempt to understand by studying a simple program. - Adding 2 integers

Non Standard Version

#include <iostream>
int main()
{
int one, int two, int sum;
cout<<"Please enter the first number"; //Requests for first integer
cin>>one; //accepts first integer
cout<<"Please enter the second number"; //Requests for second integer
cin>>two; //accepts second integer.
sum = one + two; // one & two get added and the value gets assigned to sum.
cout<<"The sum:"<<sum; //sum of 2 int's is displayed
return 0; //program has ended
}

Standard Version

#include <iostream>
using std::cout;
using std::cin;

int main()
{
int one, int two, int sum;
cout<<"Please enter the first number"; //Requests for first integer
cin>>one; //accepts first integer
cout<<"Please enter the second number"; //Requests for second integer
cin>>two; //accepts second integer.
sum = one + two; // one & two get added and the value gets assigned to sum.
cout<<"The sum:"<<sum; //sum of 2 int's is displayed
return 0; //program has ended
}

<-- content_end-->

Add your tutorials to this site Mail Me

 

Google
  Web www.gauravcreations.com

Home / Extra / Awards / Opinions / Downloads / CafeInks 

Copyright 2001- [Gaurav Creations]. All rights reserved