ebook img

Object Oriented Programming With C++ PDF

353 Pages·2017·6.727 MB·English
by  SharmaA. K
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Object Oriented Programming With C++

C++ Object-oriented Programming with A. K. Sharma Professor and Dean Department of Computer Science and Engineering B. S. Anangpuria Institute of Technology and Management Faridabad, Haryana, India Delhi Chennai Copyright © 2009 Dorling Kindersley (India) Pvt. Ltd. Licensees of Pearson Education in South Asia This eBook is licensed subject to the condition that it shall not, by way of trade or otherwise, be lent, resold, hired out, or otherwise circulated without the publisher’s prior written consent in any form of binding or cover other than that in which it is published and without a similar condition including this condition being imposed on the subsequent purchaser and without limiting the rights under copyright reserved above, no part of this publication may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording or otherwise), without the prior written permission of both the copyright owner and the above-mentioned publisher of this eBook. This eBook may or may not include all assets that were part of the print version. The publisher reserves the right to remove any material in this eBook at any time. ISBN 978-93-325-1583-3 eISBN 978-93-325-4065-1 Head Office: A-8(A), Sector 62, Knowledge Boulevard, 7th Floor, NOIDA 201 309, India Registered Office: 11 Local Shopping Centre, Panchsheel Park, New Delhi 110 017, India Layout designed by Satwinder Singh Channey Illustrations by Tarak Sharma Dedicated to all the objects around me This page is intentionally left blank C ONTENTS Preface xi About the Author xiii 1. INTRODUCTION TO C++ 1 1.1 Introduction—1 1.2 Characters Used in C++—2 1.3 Basic Data Types—2 1.3.1 Data Types Modif ers 3 1.4 C++ Tokens—3 1.4.1 Identif ers 3 1.4.2 Keywords 4 1.4.3 Constants 4 1.4.4 Variables 5 1.5 Input–Output Statements—6 1.6 Structure of a C++ Program—8 1.6.1 Comments 9 1.7 Escape Sequence (Backslash Character Constants)—9 1.8 Operators and Expressions—12 1.8.1 Arithmetic Operators 12 1.8.2 Relational and Logical Operators 13 1.8.3 Conditional Operator 15 1.8.4 Order of Evaluation of Expressions 16 1.8.5 Some Special Operators 16 1.9 Flow of Control—18 1.9.1 The Compound Statement 19 1.9.2 Selective Execution (Conditional Statements) 20 1.9.3 Repetitive Execution (Iterative Statements) 25 1.9.4 Nested Loops 30 1.10 Arrays—31 1.10.1 One-dimensional Arrays 32 1.10.2 Multi-dimensional Arrays 33 1.10.3 Array Initialization 35 1.11 Structures—36 1.11.1 Def ning a Structure in ‘C++’ 36 1.11.2 Arrays of Structures 38 vi Contents 1.11.3 Initializing Structures 38 1.11.4 Assignment of Complete Structures 39 1.11.5 Nested Structures 39 1.12 Functions—40 1.12.1 Function Prototypes 41 1.12.2 Calling a Function 41 1.12.3 Parameter Passing in Functions 42 1.12.4 Returning Values from Functions 46 1.13 I/O Functions—48 1.13.1 getchar( ) and putchar( ) Functions 48 1.13.2 getc( ) and putc( ) Functions 50 1.13.3 gets( ) and puts( ) Functions 50 1.14 Strings—51 1.15 Summary—54 Multiple Choice Questions 54 Answers 56 Exercises 56 Answers 58 2. POINTERS 59 2.1 Introduction—59 2.1.1 The & Operator 59 2.1.2 The * Operator 60 2.2 Pointer Variables—61 2.2.1 Dangling Pointers 66 2.3 Pointers and Arrays—67 2.4 Array of Pointers—73 2.5 Pointers and Structures—74 2.6 Dynamic Allocation—76 2.6.1 Self Referential Structures 82 2.7 Summary—88 Multiple Choice Questions 89 Answers 89 Exercises 89 Answers 93 3. PROGRAMMING TECHNIQUES: A SURVEY 94 3.1 Introduction—94 3.2 Unstructured Programming—95 3.3 Structured Programming—96 3.3.1 Procedural Programming 97 3.3.2 Modular Programming 98 3.4 Drawbacks of Structured Programming—99 Contents vii 3.5 Object-Oriented Programming—101 3.6 Summary—103 Multiple Choice Questions 104 Answers 105 Exercises 105 4. CLASSES AND OBJECTS 106 4.1 Introduction to Objects—106 4.2 Classes—109 4.3 Declaration of Classes in C++—110 4.3.1 Abstraction and Encapsulation 111 4.3.2 Member Function Def nition 112 4.4 Creating Objects—115 4.4.1 Calling Member Functions 116 4.5 Array of Objects—119 4.6 Objects as Function Arguments—121 4.7 Scope Resolution Operator—124 4.8 Static Data Members—129 4.9 Properties of Classes and Objects—131 4.10 Summary—132 Multiple Choice Questions 132 Answers 133 Exercises 133 5. MORE ON FUNCTIONS: ADVANCED CONCEPTS 136 5.1 Polymorphism—136 5.2 Function Overloading—136 5.3 Inline Functions—143 5.4 Friend Functions—146 5.4.1 Member Functions of a Class as Friends of Another Class 153 5.4.2 Friend Function as a Bridge Between Two Classes 153 5.5 Friend Classes—157 5.6 Recursion—160 5.6.1 Types of Recursion 167 5.7 Summary—173 Multiple Choice Questions 173 Answers 174 Exercises 174 Answers 176 viii Contents 6. CONSTRUCTORS AND DESTRUCTORS 177 6.1 Constructors—177 6.2 Types of Constructors—178 6.2.1 Default Constructor and User Def ned Constructor 178 6.2.2 Parameterized Constructors 181 6.2.3 Copy Constructor 185 6.2.4 Constructors with Default Arguments 193 6.2.5 Rules for Constructor Def nition and Usage 193 6.3 Destructors—193 6.4 Summary—195 Multiple Choice Questions 195 Answers 196 Exercises 196 7. INHERITANCE: EXTENDING CLASSES 198 7.1 Introduction to Code Reuse—198 7.2 Containership—198 7.3 Inheritance—204 7.3.1 Visibility Modes 210 7.4 Types of Inheritance—211 7.4.1 Multilevel Inheritance 212 7.4.2 Multiple Inheritance 213 7.5 Function Overriding—214 7.5.1 Virtual Functions 216 7.6 Role of Constructors and Destructors in Inheritance—226 7.7 Virtual Base Class—230 7.8 Summary—232 Multiple Choice Questions 233 Answers 233 Exercises 233 8. TEMPLATES: CODE SHARING (GENERICITY) 235 8.1 Introduction to Code Sharing—235 8.2 Templates—236 8.3 Generic Classes—240 8.4 Templates with More than One Generic Parameter—243 8.5 Summary—245 Multiple Choice Questions 245 Answers 245 Exercises 245 Contents ix 9. OPERATOR OVERLOADING 247 9.1 Introduction—247 9.2 Operator Overloading—249 9.3 Binary Operators—250 9.3.1 Arithmetic Assignment Operators 255 9.4 Unary Operators—256 9.5 Input/Output Operators—258 9.6 Rules for Operator Overloading—260 9.7 Summary—260 Multiple Choice Questions 261 Answers 261 Exercises 261 10. FILE HANDLING IN C++ 262 10.1 File Concepts—262 10.2 Files and Streams—264 10.3 Opening and Closing a File (Text Files)—266 10.3.1 Function get( ) 267 10.3.2 Function getline( ) 269 10.3.3 Function put( ) 272 10.4 Opening the Files by Using Function Open( )—273 10.5 Reading and Writing Blocks and Objects (Binary Files)—274 10.5.1 Storing Objects in Files 280 10.6 Detecting End of File—284 10.7 Summary—289 Multiple Choice Questions 289 Answers 290 Exercises 290 11. EXCEPTION HANDLING 292 11.1 Introduction—292 11.2 Traditional Error Handling—292 11.3 Exception Handling in C++—296 11.3.1 Multiple Throw Statements and Multiple Catch Blocks 299 11.3.2 Throwing Objects 303 11.4 Summary—309 Multiple Choice Questions 309 Answers 309 Exercises 309

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.