Introduction to C Programming 2nd Edition Purdue Univ pdf download
Introduction to C Programming 2nd Edition Purdue Univ pdf download
https://ebookmeta.com/product/introduction-to-c-programming-2nd-
edition-purdue-univ/
https://ebookmeta.com/product/an-introduction-to-c-gui-
programming-simon-long/
https://ebookmeta.com/product/modern-c-for-absolute-beginners-a-
friendly-introduction-to-the-c-programming-language-
and-c11-to-c23-standards-2nd-edition-slobodan-dmitrovic/
https://ebookmeta.com/product/an-introduction-to-parallel-
programming-2nd-edition-peter-pacheco/
https://ebookmeta.com/product/mathematica-by-example-5th-edition-
martha-l-abell-james-p-braselton/
Catch the Rabbit 2nd Edition Lana Bastaši■
https://ebookmeta.com/product/catch-the-rabbit-2nd-edition-lana-
bastasic/
https://ebookmeta.com/product/software-engineering-for-data-
scientists-meap-v2-andrew-treadway/
https://ebookmeta.com/product/entrepreneurial-marketing-how-to-
develop-customer-demand-3rd-edition-edwin-jacob-nijssen/
https://ebookmeta.com/product/durability-of-concrete-design-and-
construction-modern-concrete-technology-1st-edition-alexander/
https://ebookmeta.com/product/ethnic-nationalism-and-regional-
conflict-the-former-soviet-union-and-yugoslavia-1st-edition-w-
raymond-duncan-editor-g-paul-holman-editor/
Martinis Mysteries 1st Edition Rose Bak
https://ebookmeta.com/product/martinis-mysteries-1st-edition-
rose-bak/
Introduction to C++ Programming
CADCIM Technologies
Contributing
Sham Author
Tickoo
Professor
Purdue University Calumet
Hammond, Indiana
USA
ISBN 978-1-942689-38-6
www.cadcim.com
CADCIM Technologies
DEDICATION
THANKS
Note
If you are a faculty member, you can register by clicking on the
following link to access the teaching resources: The student
resources are available at We also provide Live Virtual Online
Training on various software packages. For more write us at
Table of Contents
Preface
C++
Welcome to Introduction to C++ an example based textbook,
written to cater to the needs of beginners and intermediate users.
C++ is an object-oriented programming language, which is a
preferred programming approach followed by the software industry.
C++ enjoys the distinction of being the most popular and widely
used OOP language in the world. The syntax, style, features and
philosophy of the language form the basis of many other
programming languages such as Java and C#.
Faculty Resources
• Technical Support
You can get online technical support by contacting
techsupport@cadcim.com.
• Instructor Guide
Solutions to all the review questions and exercises in the textbook
are provided to
help the faculty members test the skills of the students.
• Example Files
The C++ files used in examples are available for free download.
Student Resources
• Technical Support
You can get online technical support by contacting
techsupport@cadcim.com.
• Example Files
The C++ files used in examples are available for free download.
Stay Connected
You can now stay connected with us through Facebook and
Twitter to get the latest information about our textbooks, videos,
and teaching/learning resources. To stay informed of such updates,
follow us on Facebook (www.facebook.com/cadcim) and Twitter
(@cadcimtech). You can also subscribe to our YouTube channel
(www.youtube.com/cadcimtech) to get the information about our
latest video tutorials.
Chapter 1
Introduction to C++
Learning Objectives
Introduction
Object-Oriented Programming
Object-oriented programming (OOP) developed to overcome the
limitations of the procedure-oriented programming is an improved
technique for developing the programs. In case of OOP, the data is
treated as the most critical element and the primary focus is on the
data and not on the procedures. In this technique, the data is
grouped together with the functions that operate on it. A problem is
divided into entities known as objects. Each object maintains its own
copy of data and functions. The data cannot be accessed directly by
the other objects of the program. It can only be accessed through a
proper interface such as functions, as shown in Figure 1-2.
Figure 1-2 Representation of OOP
Features of Object-Oriented Programming
There are certain features that have made object-oriented
programming very popular. These features are as follows:
1. Objects
2. Classes
3. Encapsulation
4. Inheritance
5. Polymorphism
Objects
In object-oriented programming, a problem is divided into certain
basic entities called objects. The objects can be used to represent
real life objects such as people, bank account, and so on. In this
type of programming, all communication is carried out between the
objects. When a program is executed, the objects interact with each
other by sending messages. The objects contain the data and the
functions that can be used to manipulate the data. Each object
maintains its own copy of the data and methods, which can
communicate with each other through a proper channel or interface.
Classes
A class is a user-defined data type which is used to group the data
and the functions together. These objects are the instances of a
class. A class can also contain important members such as a
constructor to create objects. The objects that belong to same class
must have certain properties in common. For example, a table and a
chair are the objects of the furniture class. Both the objects have
certain properties in common. For example, both are made of wood
and so on.
Encapsulation
Inheritance
Different kinds of objects often have certain amount of properties in
common with each other. Inheritance is the process by which one
object can acquire the properties of another object. This is called the
reusability of code. In inheritance, whenever a new object is created,
it can only define those properties that make it unique from the
other objects. Inheritance supports the concept of the hierarchical
classifications. For example, the fruit ‘Apple’ is a part of the class
‘Fruits’, which is again a part of the class ‘Food’, as shown in Figure
1-3.
Polymorphism
Concept of c++
Code::Blocks Environment
Code::Blocks is a open-source platform IDE (integrated development
environment) that supports multiple compilers. It comes in an easy-
to-install, compressed executable file that is compatible with all
versions of Windows, Macintosh, and Linux.
When you install Code::Blocks for the first time, it scans the system
for any supported compilers. Once this scan is finished, Code::Blocks
is correctly configured for any detected compilers. Code::Blocks also
have default configurations even for those compilers that are not
detected. The process for downloading and installing Code::Blocks is
given next.
Downloading Code::Blocks
1. Navigate to http://www.codeblocks.org in your browser; the
Code::Blocks page is displayed, as shown in Figure 1-5. Next, choose
the Downloads tab from this page; the page with the Downloads
area is displayed, as shown in Figure 1-6.
Note
The codeblocks-16.01mingw-setup.exe file includes the GCC/G++ compiler
and GDB debugger.
2. Choose the Next button from this page; the License Agreement
page is displayed.
4. Accept the default location and then choose the Install button
from this page; the installing process begins. When the installation
completes, the Code::Block Installation message box is displayed.
Choose Yes from this message box; the Compilers auto-detection
dialog box is displayed. Now, choose OK from the Compilers auto-
detection dialog box to open the Code::Blocks environment.
Note
When you install Code::Blocks for the first time, the File Associates
dialog box is displayed prompting you to associate the C/C++ file type to
Code::Blocks. Select the Yes, associate Code::Blocks with C/C++ file
types radio button and then choose the OK button.
2. Choose File > New > Empty file from the menubar or press
CTRL+SHIFT+N; the Untitled1 tab is displayed in the editor.
3. Choose the File > Save file from the menubar or press CTRL+S;
the Save file dialog box is displayed. In this dialog box, type
cadcim.cpp in the File name text box and then choose the Save
button to save the file.
#include
using namespace std;
int main()
{
cout << “Cadcim Technologies!” << endl;
return 0;
}
Note
Once you save a file, the syntax highlighting feature will be enabled.
This feature displays text, especially source code, in different colors and
fonts according to the category of terms.
5. Choose Build > Build and run from the menubar or press F9 to
build and run the program. On doing so, the c:\cplusprojects\c01
window is displayed with the output CADCIM Technologies, as shown
in Figure 1-9. Press any key to close the window.
Notice that the name of the project file is FirstProject.cpp and it will
be saved at the following location: c:\cplusprojects\c01\FirstProject.
Figure 1-12 The C++ option selected
Figure 1-13 Choosing the browse button
Figure For box
4. Choose the Next button from the Console application wizard; the
Compiler drop-down list and Debug/Release configuration settings are
displayed. Accept the default settings and then choose the Finish
button, refer to Figure 1-15
Figure and
5. Expand the Sources node from the Projects tab; the main.cpp node
is displayed. Double-click on the node to open it in the editor. This
file contains the following standard code:
#include
using namespace std;
int main()
{
cout << “Hello world!” << endl;
return 0;
}
1. In main.cpp, replace with ”hello.h” and cout << “Hello world!” <<
endl; with hello Delete the line using namespace Given below is the
resulting code of
#include “hello.h”
int main()
{
hello ();
return 0;
}
2. Choose File > New > File from the menubar; the New from
template wizard is displayed.Select C/C++ header from the right pane
of the wizard, refer to Figure 1-17, and then choose the Go button;
the C/C++ header page is displayed, refer to Figure 1-18. In the
C/C++ header page, choose the browse button corresponding to the
File name with full path text box and then navigate to C:\
cplusprojects\c01\FirstProject in the Select filename dialog box. Now,
type hello.h in the File name text box and then choose the Save
button, refer to Figure 1-19.
Figure 1-17 The New from template wizard
Figure 1-18 The C/C++ header page
Figure box
3. Choose the Finish button from the C/C++ header page of the
wizard.
Note
A header file in C++ has an extension .h. This file sometimes is also
referred to as the include file. The header files hold declarations for other
files to use.
4. In the hello.h tab of the editor, type void hello(). The resulting
code is given below.
#ifndef HELLO_H_INCLUDED
#define HELLO_H_INCLUDED
void hello();
#endif // HELLO_H_INCLUDED
5. Create a new file with the name “hello.cpp” and save it in the
FirstProject folder. Type the following code in the editor:
#include
using namespace std;
void hello()
{
cout <<”Hello World”\!”<
}
6. Press F9 to run the code. Notice the text hello word is displayed
in the output.
For example:
int avg=10;
cout<<“Average is: ”<
In the above example, the << operator first sends the string Average
is: to Next, the content of the variable avg is sent to cout and
finally the keyword endl is sent which transfers the control to the
end of the line, as shown in Figure 1-21.
Note
Example 1
Explanation
Line 1
//Write a program to display “CADCIM Technologies”
This line starts with two forward slashes which represent a single-line
comment. This comment is ignored by the compiler (a software
program that translates the high-level language into a machine-level
language) until the end of the line is encountered. The basic purpose
of the comments is to provide short remarks during programming
wherever needed. You can also use multiline comments by using the
symbols /* and */, as shown in the following code:
/* This line
represents multiline
comments.
*/
Line 2
#include
It is a preprocessor directive. It tells preprocessor to include the
contents of the header file iostream in the program. This header file
contains functions that support the C++ Input/Output (I/O)
operations. This header file should be included in a program that
contains the I/O statements.
Line 3
using namespace std;
All elements of a standard C++ library are declared within a
namespace. This line allows you to use the functionality offered in
the namespace std. The keyword using specifies to the compiler that
you want to use the std namespace.
Line 4
int main( )
In this line, the main( ) is a function and the keyword int specifies
that an integer value must be returned by the main( ) function to
the operating system. In each program, the execution begins from
the main( ) function. Here, the argument list of the main function
does not contain the arguments as represented by the empty
parenthesis (
Line 6
cout << “CADCIM Technologies !” << endl;
Note
Arguments are the values that you can pass to a function to be used as
input information.
Line 7
return 0;
In this line, the return statement is used to return a value (0) to
the operating system. This statement is treated as a termination
statement which indicates that the program is normally terminated.
Example 2
The following program will prompt the user to enter two numbers,
add them, and then display the output on the screen. The line
numbers on the right are not a part of the program and are for
reference only.
int main( ) 4
{ 5
int i, j, k; 6
cout<<“Enter two numbers: ”<7
cin>>i; 8
cin>>j; 9
k=i+j; 10
cout<<“The result of the addition is: ”<11
return 0; 12
} 13
Explanation
Line 6
int i, j, k;
In this line, the keyword int represents the integer data type that can
only take numeric values. The variables and k are the names given
to the memory locations that are used to store the integer values
assigned to them.
Line 7
cout<<“Enter two numbers: ”<
Line 9
cin>>j;
The working of line 9 is same as line 8.
Line 10
k=i+j;
In this line, the value stored in the variable i is added to the value
in the variable j with the help of the addition operator After addition,
the resultant value is assigned to the variable k with the help of the
assignment operator You will learn more about operators in the next
chapter.
Line 11
Example 3
The following program will prompt the user to enter three numbers
and then calculate their average.
//Write a program to find the average of marks 1
#include 2
using namespace std; 3 int main( ) 4 { 5
int sub1, sub2, sub3; 6
float avg; 7
cout<<“Enter the marks scored in Physics: ”<8
cin>>sub1; 9
cout<<“Enter the marks scored in Biology: ”<10
cin>>sub2; 11
cout<<“Enter the marks scored in Chemistry: ”<12
cin>>sub3; 13
avg = (sub1+sub2+sub3)/3; 14
Line 7
float avg;
In this line, avg is declared as a float type variable (you will learn
about the C++ data types in the next chapter).
Line 8
cout<<“Enter the marks scored in Physics: ”<
This line is an output statement, which will display Enter the marks
scored in Physics: on the screen.
Line 9
cin>>sub1;
In this line, the cin statement is used to read an integer value from
the user. Next, the resultant value is assigned to the variable
Line 14
avg = (sub1+sub2+sub3)/3;
Line 16
return 0;
In this line, the return statement is used to return a value (0) to
the operating system. This statement is treated as a termination
statement which indicates that the program is terminated normally.
Self-Evaluation Test
Fig. 188. Zierformen von einem Bande aus der Bibliothek Jakobs I. (Nach
Quaritch.)