Ask C/C++ Expert


Home >> C/C++

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.

C/C++, Programming

  • Category:- C/C++
  • Reference No.:- M9506108

Have any Question?


Related Questions in C/C++

Question 1find the minimum and maximum of a list of numbers

Question: 1. Find the Minimum and Maximum of a List of Numbers: 10 points File: find_min_max.cpp Write a program that reads some number of integers from the user and finds the minimum and maximum numbers in this list. Th ...

Software development fundamentals assignment 1 -details amp

Software Development Fundamentals Assignment 1 - Details & Problems - In this assignment, you are required to answer the short questions, identify error in the code, give output of the code and develop three C# Console P ...

What are the legal requirements with which websites must

What are the legal requirements with which websites must comply in order to meet the needs of persons with disabilities? Why is maximizing accessibility important to everyone?

There are several ways to calculate the pulse width of a

There are several ways to calculate the pulse width of a digital input signal. One method is to directly read the input pin and another method (more efficient) is to use a timer and pin change interrupt. Function startTi ...

Assignment word matchingwhats a six-letter word that has an

Assignment: Word Matching What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than ant itself, of ...

1 implement the binary search tree bst in c using the node

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding ...

Assign ment - genetic algorithmin this assignment you will

ASSIGN MENT - GENETIC ALGORITHM In this assignment, you will use your C programming skills to build a simple Genetic Algorithm. DESCRIPTION OF THE PROGRAM - CORE REQUIREMENTS - REQ1: Command-line arguments The user of yo ...

Project - space race part a console Project - Space Race Part A: Console Implementation

Project - Space Race Part A: Console Implementation INTRODUCTION This assignment aims to give you a real problem-solving experience, similar to what you might encounter in the workplace. You have been hired to complete a ...

Why do researcher drop the ewaste and where does it end

Why do researcher drop the ewaste and where does it end up?

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As