Object-Oriented Programmingcontent.kopykitab.com/ebooks/2017/09/11719/sample/sample_1171… ·...

16

Transcript of Object-Oriented Programmingcontent.kopykitab.com/ebooks/2017/09/11719/sample/sample_1171… ·...

Page 1: Object-Oriented Programmingcontent.kopykitab.com/ebooks/2017/09/11719/sample/sample_1171… · Chapter 1 introduces the object-oriented programming paradigm and discusses the object-oriented
Page 2: Object-Oriented Programmingcontent.kopykitab.com/ebooks/2017/09/11719/sample/sample_1171… · Chapter 1 introduces the object-oriented programming paradigm and discusses the object-oriented

Object-Oriented Programming with C++

Rohit KhuranaFounder and CEO

ITLESL, Delhi

Second Edition

VIKAS® PUBLISHING HOUSE PVT LTD

Page 3: Object-Oriented Programmingcontent.kopykitab.com/ebooks/2017/09/11719/sample/sample_1171… · Chapter 1 introduces the object-oriented programming paradigm and discusses the object-oriented

VIKAS® PUBLISHING HOUSE PVT LTDE-28, Sector-8, Noida-201301 (UP) IndiaPhone: +91-120-4078900 • Fax: +91-120-4078999Registered Office: 576, Masjid Road, Jangpura, New Delhi-110014. India

E-mail: [email protected] • Website: www.vikaspublishing.com • Ahmedabad : 305, Grand Monarch, 100 ft, Shyamal Road, Near Seema Hall,

Ahmedabad-380 051 • Ph. 079-65254204, 98982 94208 • Bengaluru : First Floor, N.S. Bhawan, 4th Cross, 4th Main, Gandhi Nagar, Bengaluru-560009 • Ph. +91-80-22204639, 22281254 • Chennai : E-12, Nelson Chambers, 115, Nelson Manickam Road, Aminjikarai Chennai -600029 • Ph. +91-44-23744547, 23746090 • Hyderabad : Aashray Mansion, Flat-G (G.F.), 3-6-361/8, Street No. 20, Himayath Nagar, Hyderabad-500029 • Ph. +91-40-23269992 • Fax. +91-40-23269993 • Kolkata : 82, Park Street, Kolkata-700017 • Ph. 033-22837880 • Mumbai : 67/68, 3rd Floor, Aditya Industrial Estate, Chincholi Bunder, Malad (West), Mumbai-400064 • Ph. +91-22-28772545, 28768301 • Patna : Flat No. 101, Sri Ram Tower, Besides Chiraiyatand Over Bridge, Kankarbagh Main Road, Kankarbagh, Patna - 800 020, (Bihar)

Object-Oriented Programming with C++ISBN: 978-93259-7564-4

Vikas® is the registered trademark of Vikas Publishing House Pvt LtdCopyright © Author, 2014

All rights reserved. No part of this publication which is material protected by this copyright notice may be reproduced or transmitted or utilized or stored in any form or by any means now known or hereinafter invented, electronic, digital or mechanical, including photocopying, scanning, recording or by any information storage or retrieval system, without prior written permission from the publisher.

Information contained in this book has been published by VIKAS® Publishing House Pvt Ltd and has been obtained by its Authors from sources believed to be reliable and are correct to the best of their knowledge. However, the Publisher and its Authors shall in no event be liable for any errors, omissions or damages arising out of use of this information and specifically disclaim any implied warranties or merchantability or fitness for any particular use. Disputes if any are subject to Delhi Jurisdiction only.

Printed in India.

Page 4: Object-Oriented Programmingcontent.kopykitab.com/ebooks/2017/09/11719/sample/sample_1171… · Chapter 1 introduces the object-oriented programming paradigm and discusses the object-oriented

Dedication

I would like to dedicate this book to my uncle,

Shri G K Jajufor his understanding, constant support and trust in me.

He has always inspired me to aim higher and achieve bigger. He has been a great friend, philosopher and guide.

ROHIT KHURANA

Founder and CEOITLESL, Delhi

Page 5: Object-Oriented Programmingcontent.kopykitab.com/ebooks/2017/09/11719/sample/sample_1171… · Chapter 1 introduces the object-oriented programming paradigm and discusses the object-oriented
Page 6: Object-Oriented Programmingcontent.kopykitab.com/ebooks/2017/09/11719/sample/sample_1171… · Chapter 1 introduces the object-oriented programming paradigm and discusses the object-oriented

Preface to the Second Edition

The outstanding response to the first edition of the book Object-Oriented Programming with C++ has inspired us to revise the book. The revised edition has become more comprehensive with the inclusion of several topics. More emphasis has been laid on the aesthetics of text to make it more presentable and appealing. Like its previous edition, the revised edition provides an in-depth coverage of the concepts of object-oriented programming and their implementation in C++. In addition, certain sections of the book have been revised and a few more exercises have been added. I hope the readers will find the present edition more helpful and informative in expanding their knowledge in C++.

Key AdditionsThough several enhancements have been made to the text, we are here highlighting the key additions to this new edition.

• The topic Symbolic Constants has been added in Chapter 3.• The concept of Empty Class and main() as Member Function of Class has been

introduced in Chapter 6.• A few topics, including Anonymous Objects, Recursive Constructors, Constructors and

Destructors with Static Members have been added in Chapter 7.• Chapter 9 introduces one more type of inheritance named Multipath Inheritance.• Chapter 10 covers some more topics related to pointers, such as Pointer to Pointer,

Wild/Dangling Pointers, Null Pointer Assignment, Memory Leak and Allocation Failures, and Object Slicing.

• The text of Chapter 13 now also includes the concept of Templates and Inheritance.• Some new topics, including Stack Unwinding, Constructors, Destructors and Exception

Handling, and Exceptions and Inheritance have been added in Chapter 14.• Chapter 17 (Multithreading) is entirely new to this edition. This chapter introduces

the concept of multithreading and discusses its implementation in C++.

Chapter Organization The text is organized into 18 chapters.Chapter 1 introduces the object-oriented programming paradigm and discusses the object-oriented concepts.Chapter 2 begins with the history of C++ and then discusses the structure and execution of a C++ program.Chapter 3 discusses various programming elements of C++ such as data types, operators, expressions, control statements, etc.Chapter 4 discusses the significance and implementation of arrays, structures and unions in C++. It also describes the main features of structures in C++ that differentiate them from the structures in C.

Page 7: Object-Oriented Programmingcontent.kopykitab.com/ebooks/2017/09/11719/sample/sample_1171… · Chapter 1 introduces the object-oriented programming paradigm and discusses the object-oriented

vi Preface to the Second Edition

Chapter 5 discusses the importance of functions while writing a program. It also explains the concept of function prototyping, function definition and recursive functions. It then introduces the concept of function overloading which is not supported by C language.Chapter 6 introduces the concept of classes and objects that form the basis of object-oriented programming.Chapter 7 introduces various types of constructors such as default, parameterised and copy constructor. It also discusses destructor, its special features and use.Chapter 8 introduces the concept of operator overloading. It then discusses how various unary and binary operators are overloaded using member functions and friend functions. It also explains different types of type conversions involved in operator overloading. Chapter 9 describes how the logical relationship between the classes can be implemented using inheritance. It introduces the concept of base class, derived classes and virtual base class. It also discusses various types of inheritance, and their implementation in C++.Chapter 10 discusses the basic concepts of pointers like their declaration and initialisation. It also explains the use of pointers with other data types. It then introduces the concept of pure virtual functions and how they can be used to implement run-time polymorphism.Chapter 11 discusses I/O streams that help in handling I/O operations during the execution of a C++ program. It also explains the difference between formatted and unformatted output.Chapter 12 discusses file streams and various operations that can be performed on the files. It also discusses the ways of handling errors encountered during file operations. It also explains how a file can be accessed randomly.Chapter 13 introduces C++ templates and their two types, namely, function template and class template.Chapter 14 discusses different types of exceptions that can occur during program execution. It also explains the C++ exception-handling mechanism using try, catch and throw keywords.Chapter 15 introduces the concept of Standard Template Library and its various components such as containers, algorithms and iterators.Chapter 16 discusses the new features of ANSI C++ such as new data types, operators, and keywords. It also introduces the concept and need of C++ namespaces.Chapter 17 familiarizes the students with the concept of multithreading. It discusses how multithreading is implemented in C++.Chapter 18 discusses the various phases of object-oriented approach of software development. It also explains the conventional approaches of software development such as procedural and prototyping paradigm.

AcknowledgementsIn all my efforts towards making this book a reality, my special thanks goes to my technical and editorial team, without whom this work would not have achieved its desired level of excellence. I sincerely extend my thanks to my research and development team for devoting their time and relentless effort in bringing out this

Page 8: Object-Oriented Programmingcontent.kopykitab.com/ebooks/2017/09/11719/sample/sample_1171… · Chapter 1 introduces the object-oriented programming paradigm and discusses the object-oriented

Preface to the Second Edition vii

high-quality book. I convey my gratitude to my publisher Vikas Publishing House Pvt. Ltd for sharing this dream and giving all the support in realizing it.In our attempt towards further improvement, I welcome you all to send your feedback to [email protected]. I will highly appreciate all your constructive comments.

I hope you will enjoy reading the book and hope it proves to be a good resource for all.

Rohit KhuranaFounder and CEO

ITLESL, Delhi

Page 9: Object-Oriented Programmingcontent.kopykitab.com/ebooks/2017/09/11719/sample/sample_1171… · Chapter 1 introduces the object-oriented programming paradigm and discusses the object-oriented

Preface to the First Edition

This is an era of rapid technological advancements. The software development technology has also grown by leaps and bounds. We have been able to build applications that were difficult, if not impossible, just a short time ago. However, with the technological advancement and increase in user requirements, a need to build large and complex software systems also arose. To handle the complexities of the software, various programming approaches of software design and implementation have been evolved. Object-Oriented Programming (OOP) is the most popular and preferred programming approach as it provides new and effective ways.

The C++ programming language, which Danish programmer Bjarne Stroustrup developed in the early 1980s, is the most widely used object-oriented language in the computing field. It retains C’s strengths—efficiency, flexibility, and portability—while providing better support for program design and development of reusable software components. C++’s support for design and programming has undergone a great deal of change and improvement from the time it was developed. The language was standardised in 1988 by American National Standards Institute (ANSI) and the International Standards Organisation (ISO). Several helpful tools have been developed for using C++. The most popular and easy-to-use development environments are Turbo C++ and Borland C++. The user-interfaces of both these environments have been discussed in this book.

The main aim of the book is to create a balance between object-oriented programming and C++ by bridging the gap between the design and implementation. Beginning with object-oriented concepts such as encapsulation, abstraction, inheritance, polymorphism, message passing and dynamic binding, it moves on to the implementation of these concepts through C++. The book gives formal definitions for object-oriented concepts and describes how they relate to the features of C++. The discussion is well supported by figures and programming examples, which are kept simple and concise for better understanding of the concepts and methods. The book discusses both introductory and advanced topics such as templates, exception handling, streams and standard template library (STL).

The book meets the requirements of students enrolled in various courses both at undergraduate and postgraduate levels, such as BCA, BE, BTech, BIT, BIS, BSc, PGDCA, MCA, MIT, MIS, MSc, various DOEACC levels, and so on. It will also be suitable for those software developers and programmers who wish to expand their knowledge in C++.

Key Features • Notes, learn more boxes and keypoints have been provided to enhance the reader’s

learning.• Various application-oriented projects in C++ that are a part of our daily lives, are

given to provide an insight into how various C++ features are integrated in real-life problems.

• Detailed coverage of topics makes the book useful to students both at undergraduate and postgraduate levels.

• The topics are well supported with tabular and diagrammatic representations. • A deeper insight has been provided into the concepts, with the help of programming

examples.

Page 10: Object-Oriented Programmingcontent.kopykitab.com/ebooks/2017/09/11719/sample/sample_1171… · Chapter 1 introduces the object-oriented programming paradigm and discusses the object-oriented

Preface to the First Edition ix

• Exercises are given at the end of each chapter to test your knowledge along with answers to selected exercises.

• Besides, regular features like chapter objectives at the beginning of each chapter, a comprehensive glossary explaining technical words and an index have also been provided.

Chapter Organisation The book is structured around object-oriented concepts and each chapter addresses the specific area of object-oriented programming.

The first two chapters are introductory. They provide the history of C++ and discuss the structure and execution of its programs.

The next three chapters discuss various elements of C++ such as data types, operators, expressions, etc., along with providing a detailed description of the main features of C++ as opposed to C. The importance of functions and the concept of classes and objects have also been covered in these chapters.

The next five chapters that follow take up the concepts of constructors, destructors, inheritance, operation overloading and pointers in detail.

Chapters 11 and 12 discuss I/O and file streams, explaining the difference between formatted and unformatted output as well as various ways of handling errors.

Thereafter, the more complex features of C++ such as standard template library (STL) and its various components, exceptions handling mechanism and new features of ANSI C++ have been described, along with the concept of C++ namespaces.

The last chapter is on the various phases of the object-oriented approach of software development and also talks about the conventional approaches such as procedural and prototyping paradigms.

AcknowledgementsI thank the technical and editorial consultants for devoting their precious time to improve the quality of the book; the entire research and development team for putting in their earnest efforts and relentless perseverance to bring out a high quality book; and the publisher Vikas Publishing House Pvt. Ltd., their editorial team, and the panel of reviewers for their priceless contributions towards content enrichment.

Hope you enjoy reading this book as much as we have enjoyed writing it. For any suggestions and comments about it, please feel free to send your feedback to [email protected].

Rohit KhuranaFounder and CEO

ITLESL, Delhi

Page 11: Object-Oriented Programmingcontent.kopykitab.com/ebooks/2017/09/11719/sample/sample_1171… · Chapter 1 introduces the object-oriented programming paradigm and discusses the object-oriented
Page 12: Object-Oriented Programmingcontent.kopykitab.com/ebooks/2017/09/11719/sample/sample_1171… · Chapter 1 introduces the object-oriented programming paradigm and discusses the object-oriented

Contents

Preface to the Second Edition vPreface to the First Edition viii

Chapter 1 Introduction to Object-Oriented Programming1.1 Introduction 11.2 Evolution of Various Programming Paradigms 21.3 An Overview of Procedural Programming Paradigm 21.4 Object-Oriented Programming Paradigm 4Summary 14Exercises 15

Chapter 2 Getting Started with C++2.1 Introduction 172.2 Evolution of C++ 172.3 Structure of A C++ Program 202.4 The Input/Output Operator 242.5 Creation and Execution of C++ Programs 262.6 Integrated Development Environment 27Important Shortcuts of Borland C++ 34Summary 35Exercises 35

Chapter 3 Programming Elements of C++3.1 Introduction 393.2 Character Set 393.3 Tokens 403.4 Data Types 443.5 Variables 493.6 Expressions 543.7 New Operators 573.8 Control Statements 63Summary 74Exercises 76

Chapter 4 Arrays, Structures and Unions4.1 Introduction 824.2 Arrays 824.3 Structures 914.4 Unions 98Summary 101Exercises 101

Chapter 5 Functions5.1 Introduction 1065.2 Function Declaration 1075.3 Function Definition 1085.4 Invoking Functions 110

Page 13: Object-Oriented Programmingcontent.kopykitab.com/ebooks/2017/09/11719/sample/sample_1171… · Chapter 1 introduces the object-oriented programming paradigm and discusses the object-oriented

xii Contents

5.5 Inline Functions 1225.6 Static Variables Within Functions 1235.7 Returning From Functions 1245.8 Recursion 1265.9 Function Overloading 1285.10 Storage Class Specifiers For Functions 1325.11 External Functions 1325.12 Static Functions 132Summary 133Exercises 134

Chapter 6 Classes and Objects6.1 Introduction 1396.2 Defining A Class 1406.3 Creating Objects 1436.4 A Simple C++ Program With Class 1476.5 Objects and Arrays 1486.6 Objects and Functions 1526.7 Const With Classes 1576.8 Friends to A Class 1596.9 Nesting 1646.10 Local Class 1676.11 Static Members of A Class 168Summary 173Exercises 174

Chapter 7 Constructors and Destructors7.1 Introduction 1797.2 Constructors 1797.3 Const Objectsas 1927.4 Destructors 1937.5 Anonymous Objects 1957.6 Dynamic Operators and Constructors 1967.7 Order of Calling Constructors and Destructors 1987.8 Recursive Constructors 2007.9 Constructor and Destructor with Static Members 202Summary 203Exercises 204

Chapter 8 Operator Overloading and Type Conversions8.1 Introduction 2118.2 Operator Overloading 2128.3 Type Conversions 239Summary 245Exercises 245

Chapter 9 Inheritance9.1 Introduction 2509.2 Base Class And Derived Class 2519.3 Types of Inheritance 262

Page 14: Object-Oriented Programmingcontent.kopykitab.com/ebooks/2017/09/11719/sample/sample_1171… · Chapter 1 introduces the object-oriented programming paradigm and discusses the object-oriented

Contents xiii

9.4 Virtual Base Classes 2779.5 Constructors and Destructors In Inheritance 2789.6 Function Overriding 2879.7 Containership 289Summary 292Exercises 293

Chapter 10 Pointers, Virtual Functions and Polymorphism10.1 Introduction 29810.2 Pointers 29810.3 Virtual Functions: Implementing Run-Time Polymorphism 334Summary 343Exercises 344

Chapter 11 Console I/O Operations11.1 Introduction 35111.2 Basic Concepts of Streams 35111.3 Types of I/O Operations 355Summary 377Exercises 378

Chapter 12 File Handling12.1 Introduction 38212.2 File Streams 38212.3 Opening and Closing A File 38512.4 Reading and Writing Files 38812.5 Detecting End-of-File 39612.6 File Pointers 39612.7 Randomly Accessing A File 39912.8 Error Handling During File Operations 40412.9 Command-Line Arguments 406Summary 409Exercises 410

Chapter 13 Templates13.1 Introduction 41413.2 Function Templates 41513.3 Class Templates 428Summary 445Exercises 446

Chapter 14 Exception Handling14.1 Introduction 45114.2 Conventional Error-Handling Mechanism 45214.3 C++ Exception Handling Mechanism 45314.4 Exception Handling in Classes 46914.5 Exception Handling Functions 473Summary 474Exercises 475

Page 15: Object-Oriented Programmingcontent.kopykitab.com/ebooks/2017/09/11719/sample/sample_1171… · Chapter 1 introduces the object-oriented programming paradigm and discusses the object-oriented

xiv Contents

Chapter 15 Standard Template Library (STL)15.1 Introduction 48015.2 Stl Components 481Summary 512Exercises 513

Chapter 16 ANSI Features of C++16.1 Introduction 51816.2 New Data Types 51816.3 New Operators 52116.4 New Keywords 52716.5 C++ Namespaces 530Summary 537Exercises 538

Chapter 17 Multithreading17.1 Introduction 54117.2 Concepts of Threads 54217.3 Thread Library 54317.4 Creating Multithreaded Programs 54317.5 Error Handling 54717.6 Thread Synchronization 549Summary 559Exercises 560

Chapter 18 Object-Oriented Systems Development18.1 Introduction 56418.2 Software Development Approach 56418.3 Procedure-Oriented Paradigm 56518.4 Object-Oriented Paradigm 56918.5 Prototyping Paradigm 58218.6 A Sample Object Model 584Summary 586Exercises 587

Index 589

Page 16: Object-Oriented Programmingcontent.kopykitab.com/ebooks/2017/09/11719/sample/sample_1171… · Chapter 1 introduces the object-oriented programming paradigm and discusses the object-oriented

Object Oriented Programming with C++

Publisher : SChand Publications ISBN : 9789325975644 Author : Rohit Khurana

Type the URL : http://www.kopykitab.com/product/11719

Get this eBook

20%OFF