Intro To C++ Lesson 9

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

C++

Introduction

• C++ is an object-oriented programming language.


• It was developed by Bjarne Stroustrup at AT&T Bell Laboratories in Murray Hill,
New Jersey, USA, in the early 1980’s.
• He combined Simula67 and C language.
• Therefore, C++ is an extension of C with a major addition of the class construct
feature of Simula67.
• Stroustrup initially called the new language ‘C with classes’. However, later in
1983, the name was changed to C++.
• The idea of C++ comes from the C increment operator ++, thereby suggesting
that C++ is an augmented (incremented) version of C.
• C++ is a superset of C. Most of what we already know about C applies to C++
also. Therefore, almost all C programs are also C++ programs.
• However, there are a few minor differences that will prevent a C program to run
under C++ compiler. We shall see these differences later as and when they are
encountered.
A SIMPLE C++ PROGRAM: To Print a String

• Like C, the C++ program is a collection of functions.


• The above example contains only one function, main().
• As usual, execution begins at main(). Every C++ program must have a
main().
• C++ is a free-form language. With a few exceptions, the compiler ignores
carriage returns and white spaces.
• Like C, the C++ statements terminate with semicolons.
Comments
• Single line comment
– The double slash comment is basically a single line comment.
– Comments start with a double slash symbol and terminate at the end of the
line.
– A comment may start anywhere in the line, and whatever follows till the
end of the line is ignored. Note that there is no closing symbol. Example:
// This is an example of
// C++ program to illustrate
// Some of its features
• Multiline comments
– The C comment symbols /*, */ are still valid and are more suitable for
multiline comments.
/* This is an example of
C++ program to illustrate
some of its features
*/
Output Operator
• The output statement for the program above is the statement:
cout << “C++ is better than C.”;
• It causes the string in quotation marks to be displayed on the screen.
• This statement introduces two new C++ features:
1. Cout:
– The identifier cout (pronounced as ‘C out’) is a predefined object that
represents the standard output stream in C++.
– Here, the standard output stream represents the screen.
– It is also possible to redirect the output to other output devices.
2. <<:
• The operator << is called the insertion or put to operator. It inserts (or
sends) the contents of the variable on its right to the object on its left
Output

It is important to note that we can still use printf()


for displaying an output. C++ accepts this
notation.
However, we will use cout << to maintain the spirit
of C++.

• The object cout has a simple interface. If string represents a string variable, then the
following statement will display its contents:
cout << string;
• You may recall that the operator << is the bit-wise left-shift operator and it can still be
used for this
• purpose.
• This is an example of how one operator can be used for different purposes, depending
on the context.
• This concept is known as operator overloading, an important aspect of polymorphism.
The iostream File
• A C++ program typically contains pre-processor directive statements at the
beginning.
• All C++ programs begin with a #include directive that includes the specified
header file contents into the main program.
• We have used the following #include directive in the program:
• #include <iostream>
• This directive causes the preprocessor to add the contents of the iostream file
to the program.
• It contains declarations for the identifier cout and the operator <<.
• Some old versions of C++ use a header file called iostream.h. This is one of the
changes introduced by ANSI C++.
• (We should use iostream.h if the compiler does not support ANSI C++ features.)
• The header file iostream should be included at the beginning of all programs
that use input/output statements.
• Note that the naming conventions for header files may vary. Some
implementations use iostream.hpp; yet others iostream.hxx.
• We must include appropriate header files depending on the contents of the
program and implementation.
Namespace

• Namespace is a new concept introduced by the ANSI C++ standards


committee.
• This defines a scope for the identifiers that are used in a program.
• For using the identifiers defined in the namespace scope we must include
the using directive, like
using namespace std;
• Here, std is the namespace where ANSI C++ standard class libraries are
defined.
• All ANSI C++ programs must include this directive. This will bring all the
identifiers defined in std to the current global scope.
• using and namespace are the new keywords of C++.
Return Type of main( )
• In C++, main() returns an integer type value to the operating system.
• Therefore, every main() in C++ should end with a return(0) statement;
otherwise a warning or an error might occur.
• Since main() returns an integer type value, return type for main() is
explicitly specified as int.
• Note that the default return type for all functions in C++ is int.
• following main without type and return will run with a warning:
main()
{
......
......
}
Let us consider a slightly more complex C++ program:
Program to add two numbers
• The output of Program would be
Enter two numbers: 6.5 7.5
Sum = 14
Average = 7
Variables
• The program uses four variables number1, number2, sum, and average.
• They are declared as type float by the statement.
float number1, number2, sum, average;
• All variables must be declared before they are used in the program.
• Input Operator
• The statement
cin >> number1;
• is an input statement and causes the program to wait for the user to type
in a number.
• The number keyed in is placed in the variable number1.
• The identifier cin (pronounced ‘C in’) is a predefined object in C++ that
corresponds to the standard input stream. Here, this stream represents
the keyboard.
Input Operator

• The operator >> is known as extraction or get from operator.


It extracts (or takes) the value from the keyboard and assigns it to the
variable on its right.
• This corresponds to the familiar scanf() operation.
• Like << , the operator >> can also be overloaded.
Cascading of I/O Operators
• We have used the insertion operator << repeatedly in the last two statements for printing
results.
• The statement
cout << “Sum = ” << sum << “\n”;
• first sends the string “Sum =” to cout and then sends the value of sum.
• Finally, it sends the newline character so that the next output will be in the new line.
• The multiple use of << in one statement is called cascading.
• When cascading an output operator, we should ensure necessary blank spaces between
different items.
• Using the cascading technique, the last two statements can be combined as follows:
cout << “Sum = ” << sum << “\n” << “Average = ” << average << “\n”;
• This is one statement but provides two lines of output. If you want only one line of output, the
statement will be:
cout << “Sum = ” << sum << “,”<< “Average = ” << average << “\n”;
• The output will be
Sum = 14, Average = 7
• We can also cascade input operator >> as shown below:
cin >> number1 >> number2;
• The values are assigned from left to right. That is, if we key in two values, say, 10 and 20, then 10
will be assigned to number1 and 20 to number2.
AN EXAMPLE WITH CLASS
Explanation
• cin can read only one word and therefore we cannot use names with
blank spaces.
• The program defines person as a new data of type class.
• The class person includes two basic data type items and two functions to
operate on that data.
• These functions are called member functions.
• The main program uses person to declare variables of its type.
• As pointed out earlier, class variables are known as objects.
• Here, p is an object of type person.
• Class objects are used to invoke the functions defined in that class.
C++ Structure
• As it can be seen from the Program above, a typical C++ program would contain
four sections as shown in fig below

• These sections may be placed in separate code files and then compiled
independently or jointly.
Visual C++
It is a Microsoft application development system for C++ that runs under Windows.
Visual C++ is a visual programming environment in which basic program components can
be selected through menu choices, buttons, icons, and other predetermined methods.
Development and execution of C++ programs under Windows are briefly explained in
Appendix C.
END OF THE LESSON

You might also like