Introduction to C++ Programming, 2nd Edition [2 ed.] 1942689381, 9781942689386

Introduction to C++ Programming , 2nd Edition is a textbook for software developers to familiarize them with the concept

153 35 11MB

English Pages 372 [568] Year 2016

Report DMCA / Copyright

DOWNLOAD PDF FILE

Recommend Papers

Introduction to C++ Programming, 2nd Edition [2 ed.]
 1942689381, 9781942689386

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

Introduction to C++ Programming

CADCIM Technologies 525 St. Andrews Drive Schererville, IN 46375 USA (www.cadcim.com) Contributing Sham Author Tickoo Professor Purdue University Calumet Hammond, Indiana USA

Introduction to C++ Programming Sham Tickoo CADCIM Technologies 525 St Andrews Drive Schererville, Indiana 46375, USA www.cadcim.com

Copyright © 2016 by CADCIM Technologies, USA. All rights reserved. Printed in the United States of America except as permitted under the United States Copyright Act of 1976.

No part of this publication may be reproduced or distributed in any form or by any means, or stored in the database or retrieval system without the prior permission of CADCIM Technologies.

ISBN 978-1-942689-38-6 NOTICE TO THE READER Publisher does not warrant or guarantee any of the products described in the text or perform any independent analysis in connection with any of the product information contained in the text. Publisher does not assume, and expressly disclaims, any obligation to obtain and include information other than that provided to it by the manufacturer.

The reader is expressly warned to consider and adopt all safety precautions that might be indicated by the activities herein and to avoid all potential hazards. By following the instructions contained herein, the reader willingly assumes all risks in connection with such instructions.

The Publisher makes no representation or warranties of any kind, including but not limited to, the warranties of fitness for particular purpose or merchantability, nor are any such representations implied with respect to the material set forth herein, and the publisher takes no responsibility with respect to such material. The

publisher shall not be liable for any special, consequential, or exemplary damages resulting, in whole or part, from the reader’s use of, or reliance upon, this material.

www.cadcim.com

CADCIM Technologies

DEDICATION To teachers, who make it possible to disseminate knowledge to enlighten the young and curious minds of our future generations To students, who are dedicated to learning new technologies and making the world a better place to live

THANKS To employees of CADCIM Technologies for their valuable help

Online Training Program Offered by CADCIM

Technologies CADCIM Technologies provides effective and affordable virtual online training on various software packages including Computer Aided Design and Manufacturing and Engineering (CAD/CAM/CAE), computer programming languages, architecture, and GIS. The training is delivered ‘live’ via Internet at any time, any place, and at any pace to individuals as well as the students of colleges, universities, and CAD/CAM training centers. The main features of this program are: Training for Students and Companies in a Classroom Setting Highly experienced instructors and qualified at CADCIM Technologies conduct the classes under the guidance of Prof. Sham Tickoo of Purdue University Calumet, USA. This team has authored several textbooks that are rated “one of the best” in their categories and are used in various universities, and training centers in North America, Europe, and in other parts of the world.

Training for Individuals CADCIM Technologies with its cost effective and time saving initiative strives to deliver the training in the comfort of your home or work place, thereby relieving you from the hassles of traveling to training centers. Training Offered on Software Packages CADCIM provides basic and advanced training on the following software packages:

CATIA, Pro/ENGINEER Wildfire, PTC Creo Parametric, Creo Direct, Autodesk Solid Edge, NX, AutoCAD, AutoCAD LT, AutoCAD Plant 3D, Customizing and ANSYS Architecture and Autodesk Revit AutoCAD Civil 3D, Autodesk Revit Structure, AutoCAD Map Revit MEP, Navisworks, Primavera, and Bentley STAAD Pro Animation and Autodesk 3ds Max, Autodesk 3ds Max Design, Autodesk Maya, Autodesk Alias, The Foundry NukeX, MAXON CINEMA 4D, Adobe Flash, and Adobe Premiere Computer C++, VB.NET, Oracle, AJAX, and Java

For more information, please visit the link: http://www.cadcim.com 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

Chapter 1: Introduction to C++ Chapter 2: Getting Started with C++ Chapter 3: Control Statements

Chapter 4: Functions and Arrays Chapter 5: Strings, Pointers, and Structures Chapter 6: Union, Enumeration, and Preprocessor Chapter 7: Data Structure Chapter 8: Classes and Objects Chapter 9: Constructors, Destructors, and Operator Overloading Chapter 10: Inheritance Chapter 11: Virtual Functions and Polymorphism Chapter 12: The C++ Console I/O Operations Chapter 13: Exception Handling

Chapter 14: The File I/O System 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#.

Created by Bjarne Stroustrup in the early 1980s, C++, over the period, has undergone many changes and improvements. In 1998, the language was standardized by the American National Standards Institute (ANSI) and the International Standards Organization (ISO). The textbook confirms to these standards and explains various features of the language in a simple and easy style. The highlight of the textbook is that each concept introduced in it has been exemplified by a program to clarify and facilitate better understanding. Also, the line-by-line explanation of each program ensures that the users with no previous programming experience are able to understand the concepts and master the programming techniques and use them with flexibility while designing programs. The main features of the book are as follows:

Programming This textbook introduces the key ideas of objectoriented programming in an intuitive way. The concepts are illustrated through best programming examples, covering all aspects of OOP and C++. Additional information is provided to the users in the form of notes.

Illustrations: There is an extensive use of examples, schematic representation, flow-charts, tables, screen capture images, and programming exercises. Learning The first page of every chapter summarizes the topics that are covered in it.

Self-Evaluation Test, Review Questions, and Each chapter ends with a Self-Evaluation Test so that the users can assess their knowledge. The answers of the Self-Evaluation Test are given at the end of the chapter. Also, the Review Questions and Exercises are given at the end of each chapter that can be used by the Instructors as test questions and exercises. Free Companion Website It has been our constant endeavor to provide you the best textbooks and services at affordable price. In this endeavor, we have come out with a Free Companion Website that will facilitate the process of teaching and learning of C++. If you purchase this textbook, you will get access to the files on the Companion website. The following resources are available for faculty and students in this website:

Faculty Resources • Technical Support You can get online technical support by contacting [email protected]. • 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 [email protected].

• Example Files The C++ files used in examples are available for free download. If you face any problem in accessing these files, please contact the publisher at or the author at [email protected] or

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 After completing this chapter, you will be able to: • Understand the concept of procedure-oriented programming • Understand the concept of object-oriented programming • Understand the features of object-oriented programming • Understand the fundamentals of C++ • Understand the >>(extraction) and 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.

4. Type the following code in the Untitled1 tab.

#include using namespace std; int main() {

cout New > Project from the menubar; the New from in the previous section is an example of a console application that runs template wizard is displayed, as shown in Figure 1-10. in the text mode in the terminal window. A terminal window is the best way of creating basic programming concept. However, when your 2. In thisis wizard, select and then choose the Go program configured, youConsole need toapplication create a project to host all the button; the Console application page is displayed, as shown in Figure corresponding files. In this section, you will create the project. 1-11. In this page, select the Skip this page next time check box to skip this page the next time. Now, choose the Next button; the Console application page with the Please make selection list is

displayed. Select the C++ option from the list, as shown in Figure 112.

Figure 1-10 The New from template wizard

Figure 1-11 The Console application page

3. In the next displayed page of the wizard, type FirstProject in the Project title text box. Next, choose the browse button located next to the Folder to create project in text box, refer to Figure 1-13; the Browse For Folder dialog box is displayed, as shown in Figure 1-14. In the Browser for Folder dialog box, browse to the c:\cplusproject\c01 and then choose the OK button. The project file will be saved with the extension

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 Notice that the project structure is displayed in the Management > Projects tab of the IDE, refer to Figure 1-16.

Figure 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 > (extraction) operator is used with the cin statement. The purpose of this operator is to get data as input from the user. The >> operator is used to extract data from an input device through the cin object and assign it to the variable on its right. For example: int i; cin>>i;

In this example, the >> (extraction) operator extracts a value such as 20 from the keyboard and assigns it to the variable as shown in Figure 1-20.

Figure 1-20 Working of an extraction operator The