Recent Posts

Monday, November 26, 2012

C++

First C++ Program
//File: welcome.cpp

//A program to print a welcoming message

#include <iostream>

using namespace std;

int main()

{

    cout << “Welcome to C++” << endl;

    return 0;

} //main


Note the following:
The include file is iostream
Therefore no .h in the header
The use of using namespace std will be discussed later
The main() function has a return value
The use of cout for outputting to the monitor
The use of endl for new line.



// File: welcome.cpp
// A program to print a welcoming message
  Comments provide information to the people who read
     the program.
•  Comments are removed by the preprocessor,
     therefore the compiler ignores them.

•  In C++ there are two types of comments
 *Line comments
 *Delimited comments

More on Comments
•Line comments begin with // and continue for the rest of the line.
Delimited comments begin with /* and end with */

Preprocessor Command
#include <iostream>

When using classes or functions that are stored in different files from the one you are writing, include the

 header file for those classes.

•The header file containing the class needed for the object ‘cout is iostream.h
#include <iostream>

using namespace std;

•The keyword namespace is used to define a scope.
std is a namespace that is defined by C++.
•The object cout is part of the scope defined by std
•To access the object cout, you need to type std::cout
•The directive/keyword using is used to simplify the access.


count<<Welcome to C++
<< endl;
Produces output:
Welcome to C++
-
cout   is an object which displays whatever is   sent to it on the screen.
<<   is the insertion operator used to send   information to cout



Free Format

A language has free format if statements can appear anywhere on one or several lines.

Blank lines are ignored. Extra spaces are ignored.

Multiple statements can be placed on the same line.

Statements can be broken over more than one line anywhere other than in the middle of a string literal, operator,
or identifier.
















0 comments:

Post a Comment