c improves on many of cs features and provides


C++ improves on many of C's features and provides object-oriented programming capabilities used for software production, quality and reusability. C++ was developed by Bjarne Stronstrup at the Bell laboratories and was originally called C with classes. The name C++ includes C's increment operator (++) to indicate that C++ is an enhanced version of C.

Input and output in C++

Output in C++

The standard output stream cout and the stream insertion operator (<<) are used to output data onto the screen. Eg. cout<

cout<<" Age is  :  "  <

 

cout statement can be used with a series of statements by separating them with insertion operators as shown below:

cout<<"Value of x is  :  "<

 

Using multiple stream insertion operators (<<) in a single statement is referred as concatenating, chaining or cascading string insertion operators.

endl (end of line) is used to force a line feed in an output. endl is virtually identical in function to the escape sequence (\n) and the two are interchangeable for all practical purposes.

 

Input in C++

The standard input stream cin and the standard stream extraction operator (>>) are used to input data from the keyboard. Eg. cin>>x; will wait for a value to be entered at the keyboard and when the enter key is
pressed will put that value into the variable x. Unlike the operator cout, cin automatically forces a line feed and therefore endl is not applicable.

 

Example 1

Write a C++ program that inputs two integer numbers and then it computes for the sum.The computed value should be displayed on the screen.

/*Old version C++*/
#include
int main()

{

int number1, number2, sum;

cout<<"Enter two integer numbers:"<>number1>>number2;

Sum=number1+number2;

cout<<"Sum is  :"< return  0;

}

 

/*ANSI C++*/

#include

using namespace std;
int main()

{

int number1, number2, sum;

cout<<"Enter two integer numbers:"<>number1>>number2;

sum=number1+number2;

cout<<"Sum is:"< return  0;

}

 

Note:  ANSI is the recommended version.
Comments

There are two ways of writing comments in C++, one inherited from C and the other from BCPL.

  • The following is the C style type of comment:

/*this is a comment*/

This is mostly used for block comments.

  • The BCPL style of comment is as follows:

//this is a comment.

This is mostly used for comments that don't span more than one line.

Standard C++ Library

C++ programs are constructed using two major building blocks: functions and user defined types called classes

The classes and functions can either be from the standard library, classes and functions created by the programmers or from the various popular libraries provided by the third party vendors.
The advantage of creating own functions and classes is that one exactly knows how they function.  The disadvantage is that they are time consuming and the complex effort that goes into designing and
maintaining new functions and classes that are correct and operate efficiently.

Programming Notes ~ Wainaina                                                                        

Note: C++ usually takes the advantage of the rich collections of existing classes and functions in the C++ library.

 

Header Files

Each standard library has a corresponding header file containing the function prototypes and class definitions for all the functions and class in that library.

The programmer can create custom header files. Programmer defined header files are also included by using the #include pre-processor directives. E.g. the header file square.h can be included in the program by using the following pre-processor directive:

#include  "square.h".

The following are some of the header files in the standard library:

  • iostream: - It contains the function prototypes and class definitions for the standard input and output.
  • iomanip: - It contains function prototypes for the stream manipulators that enables the formatting of
    streams of data.
  • fstream: - It contains function prototypes for functions that perform input from files on disk and
    output to files on disk.
  • time: - It contains function prototypes and types for manipulating time and date.
  • functional: - It contains classes and functions used by the standard library algorithms.
  • cstring: - It contains function prototypes for c-styled string processing functions.
  • cctype: - It contains function prototypes for functions that test characters for certain properties, that
    can be used to convert lowercase letters to uppercase letters and vice versa.
  • cstdlib: - It contains function prototypes for functions used for conversion of numbers to text, vice
    versa, memory allocation, random number generation and various other utility functions.

Request for Solution File

Ask an Expert for Answer!!
C/C++ Programming: c improves on many of cs features and provides
Reference No:- TGS0158510

Expected delivery within 24 Hours