0% found this document useful (0 votes)
50 views

Structure C

C++ was developed by Bjarne Stroustrup at Bell Labs in the early 1980s as an extension of C. The main function serves as the driver function for a C++ program. A C++ program uses header files, preprocessor directives like #include, input/output streams like cout and cin, and follows a structure with function declarations and definitions.

Uploaded by

Dilip Dubey
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Structure C

C++ was developed by Bjarne Stroustrup at Bell Labs in the early 1980s as an extension of C. The main function serves as the driver function for a C++ program. A C++ program uses header files, preprocessor directives like #include, input/output streams like cout and cin, and follows a structure with function declarations and definitions.

Uploaded by

Dilip Dubey
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

History of C++

•Bjarne Stroustrup developed C++ at AT & T


Bell laboratories as an extension of C in the year
1980.
• C was also invented at the same place by
Dennis Ritchie in the early 1970’s.
• The heritage of C++ is shown in figure:
Simula 67 Algol 68
CC With
AAT & T
Classes
C++

AT & T
CTd
Structure of C++ Program
• C++ programs must contain a function called
main() from which the program starts.
Void main()
{
…………
// Program Body
…………
}
• The main function serves as the driver
function.
C program
• Void main()
{ int a;
printf(“ Hello World”);
scanf(“%d”,&a);
}
• It uses header file stdio.h, for supporting
standard I/O operations.

• The printf statement outputs the string


message Hello World on the console.
C program
• Scanf statement inputs the variable value
from the console.
• The function body consists of statements for
creating data storage variables called local
variable and executable statements.
• The program execution starts from main(), the
data variables defined by it are not visible to
any other function.
• C program has extension .c.
C++ Program
• The previous program will also work in c++, since
C++ supports the ANSI-C function library.
However, the program could be written using C+
+ streams.
• C++ program should have extension .cpp.
• #include< iostream.h>
{ int a;
cout <“Hello World”; // To print on screen
cin>> a; // To get input from the user
}
Structure of C++ Program
1. // Print Hello World Message….Comment
2. # include < iostream.h>….preprocessor directive
3. Void main()…………………….function declarator
4. { …………………………………….function begin
5. Cout<< “Hello World”;…body of the function main
6. }…………………………………… function end
Components of the program
First line :
• The statement starts from // symbol treated as
comment.

• Hence the compiler ignores the complete line


starting from the // character pair.

• Extensions are compiler dependent and most of


the compilers assume cpp as default extension.
Components of the program
Second line:
• #include<iostream.h> - It is preprocessor
directive.
• It includes predefined declarations and
constants that are needed by the
program(Cout, cin).
• All the other header files are used depending
on the built-in functions used in the program.
• User can also write preprocessor directives and
declare them in the beginning of the program
Components of the program
Third line -Main function
• Any C++ program consists of a set of
functions. One of them will be Main, from
where the execution starts.

• It can return a value.

• But if not then specify void before main()


Components of the program
Fourth line -Function Begin:
• The function will begin with with opening “{“
bracket.
Fifth line - Function Body
• All the statements to be executed will be in
this block.
Sixth line – Function End
• The function body ends with the closing
bracket “}”. As compiler encounters this
bracket, it is replaced by return statement
Components of the program
• Control is transferred to the caller after
function ending.
• Otherwise, last line marks the end of the
program and control is transferred to the
operating system on the termination of the
program.
Stream Based I/O
Streams in c++ are classified into:
• Output streams
– Output streams allow to perform write operations
on output devices such as screen, disk, etc.
– Output on the standard stream is performed using
the Cout object.
– C++ uses bit-wise left shift operator for
performing console output operation.
– Cout<< variable;
Stream Based I/O
• The world cout is followed by the symbol <<,
called the insertion or put-to operator, and
then with the items (variables /constants /
expressions) that are to be output.
• Cout<< “hello world”
• Cout << age;
• More than one item can be displayed using a
single cout output stream object.Such
operations in c++ are known as cascaded
output operations.
Stream Based I/O
• Cout<< “Age = “ << age;
• The cout object will display all the items from
left to right.
• Cout<< “\n” << msg << endl;
• End line serves the same purpose as “\n”
(linefeed and carriage return) and is known as
a manipulator.
Stream Based I/O
Input streams:
• The input streams allow to perform read
operation with the input devices such as
keyboard, disk, etc.
• Input from the standard stream is performed
using the Cin object.
• C++ uses the bit-wise right shift operator for
performing console input operation.
Stream Based I/O
• Cin >> variable;
• The world cin is followed by the symbol >>
(extraction or get operator) and with the
variable, into which the input data is to be
stored.
• Cin>> age;
• Input of more than one item can also be
performed using the cin input stream object.
Such operations are known as cascaded input
operations.
Stream Based I/O
• Ex : cin >> name >> age;
• The cin object will read all the items from left
to right.
Two important points about
stream operations
• Streams do not require explicit data type
specification in I/O statement.
• Streams do not require explicit address
operator prior to the variable in the input
statement.
<< and >>, cout and cin
• Bit wise operators in c/c++.
• These operators are overloaded in c++ to
serve input and output values.
• Cout(console output) and Cin(console input)
are stream objects.

You might also like