Keywords appear in bluein Visual C++. Each keyword has a predefined purpose in the language. Do not use keywords as variable and constant names!! We shall cover the following keywords in this class:
bool, break, case, char, const, continue, do, default, double, else, extern, false, float, for, if, int, long, namespace, return, short, static, struct, switch, typedef, true, unsigned, void, while
Identifiers:-
A valid identifier is a sequence of one or more letters, digits or underscore characters (_). Neither spaces nor punctuation marks or symbols can be part of an identifier. Only letters, digits and single underscore characters are valid. In addition, variable identifiers always have to begin with a letter. They can also begin with an underline character (_ ), but in some cases these may be reserved for compiler specific keywords or external identifiers, as well as identifiers containing two successive underscore characters anywhere. In no case they can begin with a digit.
Another rule that you have to consider when inventing your own identifiers is that they cannot match any keyword of the C++ language nor your compiler's specific ones, which are reserved keywords. The standard reserved keywords are:
auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, false, float, for, goto, if, int, long, namespace, operator, return, short, static_cast, switch, template, this, throw, true, union, using, void, volatile, while
Structure Of C++ Program: -
Data type definition.
Global data declaration.
Function definition(subroutines).
class definitions.
A special function called main() (where the action starts).

General Form of C++ Program:-
// Program description
#include directives
int main()
{
constant declarations
variable declarations
executable statements
return0;
}
Why int main()...????
Void means that the function has no return-value, so you don't need return. int -- which is also implied for functions with no stated return-type -- means that the function should return an integer, so you must use return. The return-value of main() is also the return-value for the program, and it's value is often used to tell the operating system if the program ran successfully or not. Typically a return-value of 0 (zero) tells that the program ran without a problem, while various non-zero values indicates different problems (e.g. 1=couldn't read a file, 2=bad arguments, 3=owerflow). Some OSs define which values should be used with what type of errors, others let's the programmer choose what he'd like for his program. You should use a main() of type int, and return a value (zero) at the end of your program. The revised language definition for C++ makes int the only legal type for main() (many compilers will at least warn you otherwise)... For C, void as return-type for main() has never really been in use... So use "int main()" and "return 0". When you use void main(), you are making a mistake Please don't do it. C programming language: ISO 9899 paragraph 5.1.2.2.1.1 Program startup The function called at program startup is named main. It shall be defined with a return type of int and with no parameters: intmain(void) { /* ... */ } or with two parameters int main(int argc, char *argv[]) { /* ... */ } or equivalent. C++ programming language : ISO 14882 paragraph 3.6.1 Main function A program shall contain a global function called main, which is the designated start of the program [...] This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main: int main() { /* ... */ } and intmain(int argc, char* argv[]) { /* ... */ }
Comments:-
Comments are parts of the source code disregarded by the compiler. They simply do nothing. Their purpose is only to allow the programmer to insert notes or descriptions embedded within the source code.
C++ supports two ways to insert comments:
Line comments
Block comment
The first of them, known as line comment, discards everything from where the pair of slash signs (//) is found up to the end of that same line. The second one, known as block comment, discards everything between the /* characters and the first appearance of the */ characters, with the possibility of including more than one line. We are going to add comments to our second program:
C++ Compiler Directives: -
Compiler Directives appear in blue in Visual C++.
The #include directive tells the compiler to include some already existing C++ code in your program.
The included file is then linked with the program.
There are two forms of #includestatements:
#include <iostream> //for pre-defined files
#include "my_lib.h" //for user-defined files
C++ PreProcessor : -
C++ Compilers automatically invoke a preprocessor that takes care of #include statements and some other special directives. Definitions that allow your program to use the function and classes that make up the standard C++ library are in these files. You don't need to do anything special to run the preprocessor - it happens automatically.
Declaration of variables:-
In order to use a variable in C++, we must first declare it specifying which data type we want it to be. The syntax to declare a new variable is to write the specifier of the desired data type (like int, bool, float...) followed by a valid variable identifier. For example:
int b;
float num;
These are two valid declarations of variables. The first one declares a variable of type int with the identifier b. The second one declares a variable of type float with the identifier num. Once declared, the variables a and num can be used within the rest of their scope in the program.If you are going to declare more than one variable of the same type, you can declare all of them in a single statement by separating their identifiers with commas. For example:
int a,b,c,d,e,f;
float num,greater,count;
Initialization of variables:-
When declaring a variable, its value is undetermined. But you may want a variable to store a concrete value at the same moment that it is declared. In order to do that, you can initialize the variable. There are two ways to do this in C++:
The first one, known as c-like, is done by appending an equal sign followed by the value to which the variable will be initialized
type identifier = initial_value ;
For example, if we want to declare an int variable called a initialized with a value of 0 at the moment in which it is declared, we could write:
int a=0;
float num=5.75;
Constants:-
With the const prefix you can declare constants with a specific type in the same way as you would do with a variable:
const int a=3.14;
Here, pathwidth and tabulator are two typed constants. They are treated just like regular variables except that their values cannot be modified after their definition.
Operators:-
Once we know of the existence of variables and constants, we can begin to operate with them. For that purpose, C++ integrates operators. Unlike other languages whose operators are mainly keywords, operators in C++ are mostly made of signs that are not part of the alphabet but are available in all keyboards. This makes C++ code shorter and more international, since it relies less on English words, but requires a little of learning effort in the beginning.
You do not have to memorize all the content of this page. Most details are only provided to serve as a later reference in case you need it.
Assignment (=):-
The assignment operator assigns a value to a variable.
a=25;
This statement assigns the integer value 5 to the variable a. The part at the left of the assignment operator (=) is known as the lvalue (left value) and the right one as the rvalue (right value). The lvalue has to be a variable whereas the rvalue can be either a constant, a variable, the result of an operation or any combination of these. The most important rule when assigning is the right-to-left rule: The assignment operation always takes place from right to left, and never the other way:
a=b;
This statement assigns to variable a (the lvalue) the value contained in variable b (the rvalue). The value that was stored until this moment in a is not considered at all in this operation, and in fact that value is lost.
Consider also that we are only assigning the value of b to a at the moment of the assignment operation. Therefore a later change of b will not affect the new value of a.
Algorithm:-
The expression A>B is a logical expression
it describes a condition we want to test
if A>B is true (if A is greater than B) we take the action on left
print the value of A
if A>B is false (if A is not greater than B) we take the action on right
print the value of B
Pseudo Code: -
If A>B then
print A
else
print B
end if
C++ Code:-
The ability to control the flow of your program, letting it make decision on what code to execute, is valuable to the programmer. The if statement allows you to control if a program enters a section of code or not based on whether a given condition is true or false. One of the important functions of the if statement is that it allows the program to select an action based upon the user's input. For example, by using an if statement to check a user entered password, your program can decide whether a user is allowed access to the program. Without a conditional statement such as the if statement, programs would run almost the exact same way every time. If statements allow the flow of the program to be changed, and so they allow algorithms and more interesting code. Before discussing the actual structure of the if statement, let us examine the meaning of TRUE and FALSE in computer terminology. A true statement is one that evaluates to a nonzero number. A false statement evaluates to zero. When you perform comparison with the relational operators, the operator will return 1 if the comparison is true, or 0 if the comparison is false. For example, the check 0 == 2 evaluates to 0. The check 2 == 2 evaluates to a 1. If this confuses you, try to use a cout statement to output the result of those various comparisons (for example cout<< ( 2 == 1 );) When programming, the aim of the program will often require the checking of one value stored by a variable against another value to determine whether one is larger, smaller, or equal to the other. There are a number of operators that allow these checks.source: www.cprogramming.com/tutorial/lesson2.html
C++ Standard Library:-
C++ programs consist of pieces called classes and functions. You can program each piece yourself, but most C++ programmers take advantage of the rich collections of classes and functions in the C++ Standard Library. Thus, there are really two parts to learning the C++ “world.” The first is learning the C++ language itself; the second is learning how to use the classes and functions in the C++ Standard Library. We discuss many of these classes and functions. P. J. Plauger’s book, The Standard C Library (Upper Saddle River, NJ: Prentice Hall PTR, 1992), is a must read for programmers who need a deep understanding of the ANSI C library functions included in C++. Many special-purpose class libraries are supplied by independent software vendors.The advantage of creating your own functions and classes is that you’ll know exactly how they work. You’ll be able to examine the C++ code. The disadvantage is the time consuming and complex effort that goes into designing, developing and maintaining new functions and classes that are correct and that operate efficiently.
No comments:
Post a Comment