Sie sind auf Seite 1von 11

BRIEF HISTORY AND TURBO C++ EDITOR ENVIRONMENT

C++ was designed by Bjarne Stroustrup while he was working for AT&T Bell Labs, which
eventually packaged and marketed it. Initial versions of the language were made available
internally at AT&T beginning in 1981. C++ evolved steadily in response to user feedback.

The first edition of Stroustrup's book, The C++ Programming Language, was published
in early 1986. After the release of Version 2.0 in 1989, C++ was rapidly acknowledged as a
serious, useful language. Work began that year to establish an internationally recognized
language standard for it. In 1997, a committee of the American National Standards Institute
(ANSI) completed and published internally the Draft Standard - The C++ Language, X3J16/97-
14882, Information Technology Council (NSITC), Washington, DC.

To execute the integrated version of Turbo C++, simply use the command prompt then
go to the root directory, open the TCC folder and execute the TC.EXE application

Figure 1. Integrated Development Environment of Turbo C++

Summary of the Main Menu Items


BASIC ELEMENTS OF A C++ PROGRAM

 IDENTIFIERS

These are a sequence of one or more letters, digits, or underline symbols ( _ )


that are used to identify functions, classes, or other elements of C++ language.

TYPES OF IDENTIFIERS

• Predefined Identifier – Reserved Words / Keywords


• User-defined Identifier – Variable Names

PREDEFINED IDENTIFIERS

USER-DEFINED IDENTIFIERS WORDS

What is a variable?

A variable is a data storage location that has a value, which can change
during program execution. By using a variable’s name in your program, you are,
in effect referring to the data stored there. You must initialize a variable -
probably giving it a value of zero - before you use it in a calculation or you need
to make sure that it is given a value in some other way. All variables in C++ must
be declared prior to their use.

Variables can be given any name up to 31 characters in length which is


composed of any of the following characters:

• Characters a to z and A to Z (NOTE: upper case and lower case are


distinct).
• The first character must be a letter.
• Only letters, digits or underscores may follow the initial letter.
• Blank spaces and special characters are not allowed.
• Use the underscore to separate words in a name consisting of
multiple words or capitalize the first letter of one or more words.
• Do not use underscore as the first character of a name. Must not be
any of the 32 keywords.
• Variable names must be meaningful.

THERE ARE THREE PLACES IN A C++ PROGRAM WHERE


VARIABLES CAN BE DECLARED:

1. Outside of all functions, including the main() function. This sort of variable is
called global and may be used by any part of the program.
2. Inside the function. Variables in this way are called local variables and may
be used only by statements that are also in the same function.
3. In the declaration of a formal parameter of a function. The formal parameters
are used to receive the arguments when that function is called.

ESCAPE CODES OR BACKSLASH CODES

These are special character constants that can be expressed using its respective
backslash code

 DATA TYPES
 OPERATORS

These are the symbols that tell the compiler to perform specific mathematical or
logical manipulations.

Types of Operators

1. Assignment Operator - In C++, assignment operator is a single equal sign (=)


which means that the value on the right side of the assignment expression after
the equal sign is assigned to the variable on the left. (Example : SUM = A + B)

2. Arithmetic Operators - C++ supports the following binary operators:


3. Compound Operators - Below are the compound operators:
+= , -= , *= , /=, %=

(Example: x += y;  this means x = x + y)

4. Increment/Decrement Operator - These allow you to use a single operator that


adds 1 to or subtract 1 from any value. (Example: a++, b--)

5. Relational Operators - These are operators that allow the comparison of two or
more numerical values, yielding a result based on whatever the comparison is
true(1) or false(0).

6. Logical Operators - These operators work with logical values true(1) and
false(0) allowing to combine relational operators.

STRUCTURE OF A C++ PROGRAM


 PREPROCESSOR DIRECTIVE - These are various instructions in the source code of
the program written to be included in the Turbo C++ compiler. These are not actually
part of the C language but they are used to expand the scope of the C programming
language. All preprocessor directives begin with a # sign.
 REMARKS/COMMENTS - The text contained between /* and */ are considered
comments. They are ignored by the compiler. Also, // can be used to make a line of
statement a comment.
 MAIN() FUNCTION - The main function is the point where all C programs begin their
execution. It is independent from whether it is at the beginning, at the end, or at the
middle of the code. Its content is always the first to be executed when a program starts.
In addition, it is essential that all C++ programs have a main function.

TO COMPILE AND RUN THE PROGRAM PRESS “CTRL + F9”.

SAMPLE PROGRAM:

// my first program in C++ OUTPUT

#include<iostream.h> Hello World!

void main ()

clrscr();

cout << "Hello World!";

getch();

BASIC INPUT/OUTPUT STATEMENTS

C++ uses a convenient abstraction called streams to perform input and output operations
in sequential media such as the screen, the keyboard or a file. A stream is an entity where a
program can either insert or extract characters to/from. There is no need to know details about
the media associated to the stream or any of its internal specifications. All we need to know is
that streams are a source/destination of characters, and that these characters are
provided/accepted sequentially.

The standard library of iostream.h defines a handful of stream objects that can be used
to access what are considered the standard sources and destinations of characters by the
environment where the program runs:
stream description

cin standard input stream

cout standard output stream

Standard Output (cout) - On most program environments, the standard output by default is the
screen, and the C++ stream object defined to access it is cout. For formatted output
operations, cout is used together with the insertion operator, which is written as << (i.e., two "less
than" signs).

cout << "Output sentence"; // prints Output sentence on screen


cout << 120; // prints number 120 on screen
cout << x; // prints the value of x on screen

Standard Input (cin) - In most program environments, the standard input by default is the
keyboard, and the C++ stream object defined to access it is cin. For formatted input
operations, cin is used together with the extraction operator, which is written as >> (i.e., two
"greater than" signs). This operator is then followed by the variable where the extracted data is
stored.

int age;
cin >> age;
Example:
//Input/Output Example Please enter an integer value: 702
The value you entered is 702 and
#include <iostream.h> its double is 1404.
#include <conio.h>

void main ()
{
clrscr();
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 <<
".\n";
getch();
}

conio.h header used in c programming contains functions for console input/output. Some of the
most commonly used functions of conio.h are clrscr, getch, getche, kbhit etc. Functions of
conio.h can be used to clear screen, change color of text and background, move text, check if a
key is pressed or not and many more.

 clrscr() - clrscr function clears the screen amd move the cursor to upper left hand corner
of screen.
 getch() - getch function prompts the user to press a character and that character is not
printed on screen, getch header file is conio.h.
 gotoxy() - gotoxy function places cursor at a desired location on screen i.e. we can
change cursor position using gotoxy function.
Declaration : void gotoxy( int x, int y);
where (x, y) is the position where we want to place the cursor.

 textcolor() - textcolor function is used to change the color of drawing text in c programs.
 textbackground() - textbackground function is used to change of current background
color in text mode.

SELECTION CONTROL STRUCTURES

Conditional control statements are at the very core of programming, in almost any
language. The idea behind conditional control is that it allows you to control the flow of the code
that is executed based on different conditions in the program, input taken from the user, the
internal state of the machine the program is running on, etc.
Conditional statements specify whether another statement or block of statements should
be executed or not. These are often called "selection constructs". A conditional expression is an
expression that produces a true or false result. There are three major structures related to the
conditional execution of code in C/C++ - the if statement, the if-else statement, and the switch-
case statement.

if-statement

Syntax:

if(condition_expression)

statement;

Where condition is the expression that is being evaluated. If this condition is true,
statement is executed. If it is false, statement is ignored (not executed) and the program
continues right after this conditional structure.

Example:

if (x == 0)

cout<<“The no. is zero.”;

if-else statement

Syntax:

if(Condition_Expression)
Statement_TRUE;
else
Statement_FALSE;

If the if statement evaluates to true, then all the statements inside the ifblock are
executed and the else if will be ignored. When the if statement is false, it will then check the
condition in the else if statement.
Example No. 1

if (x >= 0)

cout<<”The no. is a whole number.”;

else

cout<<“The no. is a negative number.”;

Example No. 2

if (x %2 == 0)

cout<<“It is EVEN.”;

else

cout<<“It is ODD.”;

Nested-if statement

Syntax:

if(Condition_Expression1)
Statement1;
else if(Condition_Expression2)
Statement2;
else if(Condition_Expression3)
Statement3;
else
Statement-n;

Reminders:

• condition_expression contains the test expression. If the test expression evaluates to


true (ie. non-zero), StatementBlock is executed, otherwise the ElseStatementBlock is
executed.
• If there is more than one statement to be executed within the ThenStatementBlock or
ElseStatementBlock branch, the blocks must be enclosed with {}'s.
• It is not necessary to have an else statement within each if() conditional.
• Multiple expressions can be evaluated by using conjunctions (e.g. &&, ||).
• Indent your programs to increase readability.
• Do not confuse the uses of assignment (=) and equality (==) operators, especially within
the test condition. They can have adverse effects on your program.

switch statement

The syntax of the switch statement is:

switch(expression) {
case constant_value_1: statement1;

break;
case constant_value_2: statement2;

break;
case constant_value_3: statement3;

Das könnte Ihnen auch gefallen