0% found this document useful (0 votes)
71 views6 pages

CP II Unit II

The document provides an introduction to C++ programming, including: - C++ is an object-oriented extension of C that adds features like classes, inheritance, and polymorphism. - A simple C++ program is presented that prints a string using cout and the insertion operator <<. - Key aspects of C++ programs are discussed like data types, input/output streams, and program structure.

Uploaded by

virat gautam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views6 pages

CP II Unit II

The document provides an introduction to C++ programming, including: - C++ is an object-oriented extension of C that adds features like classes, inheritance, and polymorphism. - A simple C++ program is presented that prints a string using cout and the insertion operator <<. - Key aspects of C++ programs are discussed like data types, input/output streams, and program structure.

Uploaded by

virat gautam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Reference: - Object Oriented Programming With C++ by E.

Balagurusamy

Beginning with C++


- What is C++?
- Applications of C++,
- Structure of C++ Program,
- Creating, Compiling, Linking

Introduction of C++
- 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.
- Stroustrup, an admirer of Simula67 and a strong supporter of C, wanted to combine the
best of both the languages and create a more powerful language that could support object-
oriented programming features and still retain the power and elegance of C. The result
was C++.
- C++ is an extension of C with a major addition of the class construct feature of Simula67.
Since the class was a major addition to the original C language, 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 version of C.
- C+ + is a superset of C.
- Almost all C programs are also C++ programs.
- The most important facilities that C++ adds on to C care classes, inheritance, function
overloading and operator overloading. These features enable creating of abstract data
types, inherit properties from existing data types and support polymorphism, thereby
making C++ a truly object-oriented language.

Application of C++
- C++ is a versatile language for handling very large programs; it is suitable for virtually
any programming task including development of
o Editors
o compilers
o databases
o communication systems and
o Complex real life applications systems.
- Since C++ allow us to create hierarchy related objects, we can build special object-
oriented libraries which can be used later by many programmers.
- While C++ is able to map the real-world problem properly, the C part of C++ gives the
language the ability to get closed to the machine-level details.
- C++ programs are easily maintainable and expandable. When a new feature needs to be
implemented, it is very easy to add to the existing structure of an object.

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Simple C++ Program


- Let us begin with a simple example of a C++ program that prints a string on the screen.
Printing A String “C++ is better than C””

#include<iostream.h>
int main( ) {
cout<< “C++ is better than C \n”;
return 0;
}

Program:- 2.1 Program to print Message


This simple program demonstrates several C++ features.

Program feature
- Like C, the C++ program is a collection of function.
- The above example contain 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 exception, the compiler ignore carriage return
and white spaces.
- Like C, the C++ statements terminate with semicolons.

Comments
- C++ introduces a new comment symbol // (double slash).
- Comment 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.
- The double slash comment is basically a single line comment.
- Multiline comments can be written as follows:
// This is an example of
// C++ program to illustrate
// some of its features
- The C comment symbols /*,*/ are still valid and are more suitable for multiline
comments.
- The following comment is allowed:
/* This is an example of
C++ program to illustrate
some of its features
*/

Output operator
- The statement in program 2.1
cout<< “C++ is better than C.\n”;
- Causes the string in quotation marks to be displayed on the screen.
- This statement introduces two new C++ features, cout and <<.
- 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.
- The operator << is called the insertion or put to operator.

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

The iostream File


- We have used the following #include directive in the program 2.1:
#include <iostream.h>
- The #include directive instructs the compiler to include the contents of the file enclosed
within angular brackets into the source file.
- The header file iostream.h should be included at the beginning of all programs that use
input/output statements.

Return Type of main( )


- In C++, main () returns an integer value to the operating system.
- Therefore, every main () in C++ should end with a return (0) statement; otherwise a
warning an error might occur.
- Since main () returns an integer type for main () is explicitly specified as int.
- Note that the default return type for all function in C++ is int.
- The following main without type and return will run with a warning:

main( ){

……

More C++ Statements


- Let us consider a slightly more complex C++ program. Assume that we should like to read two
numbers from the keyboard and display their average on the screen. C++ statements to
accomplish this is shown in program 2.2
#include<iostream.h>
int main( )

Program 2.2 Computing Sum & Average.

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Variables
- The program 2.2 uses four variables number1, number2, sum and average. They are
declared as type float by the statement.
float number1, number2, sum, average;
- All variable must be declared before they are used in the program.

Input Operator
The statement in program 2.2
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.
- 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
fig 2.1.
- This corresponds to a familiar scanf() operation.
- Like <<, the operator >> can also be overloaded.

Fig:- 2.1 Cin & Extraction Operator

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(program 2.2) can be combined as
follows:
Cout << “Sum = “ << sum << “\n” << “Average = “ << average << “\n”;

This is one statement but provides two line of output.


- If you want only one line of output, the statement will be:
Cout << “Sum = “ << sum << “,” << “Average = “ << average << “\n”;

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

The output will be:


Sum = 14, average = 7
- We can also cascade input iperator >> 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 nunber1 and 20 to number2.

Structure of C++ Program


- A typical C++ program would contain four sections as shown in fig. 2.2.
- This section may be placed in separate code files and then compiled independently or
jointly.
- It is a common practice to organize a program into three separate files.
- The class declarations are placed in a header file and the definitions of member functions
go into another file. This approach enables the programmer to separate the abstract
specification of the interface from the implementation details (member function
definition).
- Finally, the main program that uses the class is places in a third file which “includes: the
previous two files as well as any other file required.

Fig: 2.2 Structure of C++ Program

- This approach is based on the concept of client-server model as shown in fig. 2.3.
- The class definition including the member functions constitute the server that provides
services to the main program known as client.
- The client uses the server through the public interface of the class.

Fig 2.3 Client Server Model.

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Creating Program, Compiling & Linking

C++-Program Library files + Object files

#include<iostream.h>

main( ){
Compiler Linker Loader
cout<<“Wel-
Come”;

msg.cpp msg.obj msg.exe Loads executable file


to memory (RAM)

Operating System We will get output


by providing
necessary input
msg.bak
Fig:- C Program Execution Life Cycle
- A program written (created) in higher level language must be translated into machine
level language before it can be executed.
- Compiler is used to compile (HLLLLL)program
- In above diagram, C++ program by name ‘msg.cpp’ compiled with the help of
compiler & compiler produces ‘msg.obj’ file as a output. Object file ‘msg.obj’ and
library files will be given as input to linker which generates executable file ‘msg.exe’.
- With the help of loader ‘msg.exe’ will be loaded to memory for execution & we will
get output by giving input (if any).
- On other hand operating system produces ‘msg.bak’ file which contains C++ program
code.

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi

You might also like