Nothing Special   »   [go: up one dir, main page]

0% found this document useful (0 votes)
40 views4 pages

File: Main - CPP Author: A K Created On April 28, 2018 / #Include

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 4

C++

A C++ program involves the following section:


 Documentation
 Preprocessor Statements
 Global Declarations
 The main() function
o Local Declarations
o Program Statements & Expressions
 User Defined Functions

Let's begin with a simple C++ program code.

/*
* File: main.cpp
* Author: A K
* Created on April 28, 2018
*/
#include <iostream>

int main()
{
cout<<"This is my first C++ Program.";
cout<<endl<<"and its very easy to learn";
}

The above example has been used to print text on the screen.
Let’s take a look into various parts of the above C++ Program.

/* Comments */ Comments are a way of explaining what makes a program. The compiler ignores
comments and used by others to understand the code.
or
This is a comment block, which is ignored by the compiler. Comment can be used
anywhere in the program to add info about program or code block, which will be
helpful for developers to easily understand the existing code in the future.
#include <iostream.h> This is a preprocessor directive. It tells the preprocessor to include the contents of
iostream header file in the program before compilation. This file is required for input-
output statements.
Int void Int void is a return value, which will be explained in a while.
main() The main() is the main function where program execution begins. Every C++ program
must contain only one main function.
or
This is a main function, which is the default entry point for every C++ program and the
void in front of it indicates that it does not return a value.
Braces Two curly brackets "{…}" are used to group all statements.
or
Curly braces which shows how much the main() function has its scope.

This is my first C++ Program.


The above line is a statement in C++. A statement must always terminate with a semicolon; otherwise,
it causes a syntax error. This statement introduces two new features of C++
language, cout and << operator.
You will also notice that the words are inside inverted commas because they are what is called a string.
Each letter is called a character, and a series of characters that are grouped is called a string. Strings
must always be put between inverted commas.
We used std:: before cout. This is required when we use #include <iostream >.
It specifies that we are using a name cout which belongs to namespace std. A namespace is a new
concept introduced by ANSI C++ which defines the scope of identifiers which are used in the program.
std is the namespace where C++ standard libraries are defined.
Operator << is the insertion stream operator. It sends contents of the variable on its right to the object
on its left. In our case, the right operand is the string "This is my first c++ Program" and left operand is a
cout object. So it sends the string to the cout object, and cout object then displays it on the output
screen.
Return Statement
return 0 At the end of the main function returns value 0.

In new C++ you have to use:


 int main() instead of void main()
 After you import your headers you required to use using namespace std;
 There is no header file like iostream.h, you only required to use this as #include <iostream>
Note: void main() and iostream.h is only valid for Turbo C++.
In every program, there is some data which is taken as input and generate the processed data as
output following the input > process > output cycle. Therefore it is essential to know how to provide data
as input and how to present the output in the desired form. C++ supports a rich set of I/O functions and
operations to do this. As these functions use the advanced features of C++, programmers need to know
a lot about them before implementing them in any program. It is to be noted that C++ supports all of C's
rich sets of I/O functions. Programmers can use any of them within a C++ program. In this chapter, you
will learn about how to manage the input/output capabilities of a C++ program.
The input/output in C++
C does not have built-in input/output facilities. Instead, it left the I/O to the compiler as external library
functions (such as printf and scanf) in stdio (standard input-output) library. The ANSI C standard
formalized these IO functions into Standard IO package (stdio.h). Likewise, C++ continues this
approach and formalizes IO in iostream and fstream libraries.
Features of I/O in C++
 C++ IO is type safe.
 C++ IO operations are based on streams of bytes and are device independent.
Streams in C++
C++ IO is based on streams, which are a sequence of bytes flowing in and out of the programs (just like
water and oil flowing through a pipe). I/O systems in C++ are designed to work with a wide variety of
devices including terminals, disks and tape drives. The input-output system supplies an interface to the
programmer that is independent of the actual device being accessed. This interface is known as a
stream. A stream is a sequence of bytes which acts either as a source from which input data can be
obtained or as a destination to which output data can be sent. The source stream which provides data
to the program is called the input stream and the destination stream which receives output from the
program is called the output stream.
Follow the steps below to perform input and output:
 Construct a stream object.
 Connect (Associate) the stream object to an actual IO device
 Perform input/output operations on the stream, via the functions defined in the stream's public
interface in a device-independent manner.
 Disconnect (Dissociate) the stream to the actual IO device (e.g., close the file).
 Free the stream object.

Unformatted input / output operations


You have already used the cin and cout (pre-defined in the iostream file) for input and output of data of
various types. This has been made possible by overloading the operators << and >> to recognize all
the basic C++ types.
 cin standard input stream
 cout standard output stream
#include <iostream>

void main()
{
int g;
cin>>g;
cout << "Output is: "<< g;
}
put() and get() functions
The classes istream and ostream defines two member functions get() and put() respectively to handle
single character input/output operations.
Get() function is of two types:
1. get(char *)
2. get(void)
Both of them can be used to fetch a character including blank space, tab or new-line character.
Code snippet
char ch;
cin.get(ch);
while(ch != '\n')
{
cout<<ch;
cin.get(ch);
}
Similarly, the function put(), a member of a stream class can be used to output a line of text character
by character.
Example:
cout.put ('g');
char ch;
cout.put(ch);
getline() and write()
You can read and display lines of text more efficiently using the lie oriented input/output functions. They
are:
getline()
write()
The getline() function reads the entire line of texts that ends with a newline character. The general form
of getline() is:
cin.getline (line, size);
The write() function displays the entire line of text, and the general form of writing this function is:
cout.write (line, size);

Manipulators are operators used in C++ for formatting output. The data is manipulated by the
programmer's choice of display.
Some of the more commonly used manipulators are given below:
endl Manipulator
endl is the line feed operator in C++. It acts as a stream manipulator whose purpose is to feed the
whole line and then point the cursor to the beginning of the next line. We can use \n (\n is an escape
sequence) instead of endl for the same purpose.
setw Manipulator
This manipulator sets the minimum field width on output.
Syntax:
setw(x)
Example:
/*
* File: main.cpp
* Author: Gautam
* Created on October 16, 2011, 12:58 PM
*/
#include <iostream>
#include <iomanip>
using namespace std;

int main() {

float basic, ta,da,gs;


basic=10000; ta=800; da=5000;
gs=basic+ta+da;
cout<<setw(10)<<"Basic"<<setw(10)<<basic<<endl
<<setw(10)<<"TA"<<setw(10)<<ta<<endl
<<setw(10)<<"DA"<<setw(10)<<da<<endl
<<setw(10)<<"GS"<<setw(10)<<gs<<endl;
return 0;
}

You might also like