Class and Objects in C++ 193–278
Total Page:16
File Type:pdf, Size:1020Kb
OBJECT-ORIENTED PROGRAMMING C++ SIMPLIFIED OBJECT-ORIENTEDOBJECT-ORIENTED PROGRAMMINGPROGRAMMING C++C++ SIMPLIFIEDSIMPLIFIED By HARI MOHAN PANDEY Assistant Professor Computer Engineering Department NMIMS University Mumbai (Maharashtra) UNIVERSITY SCIENCE PRESS !N)MPRINTOF,AXMI0UBLICATIONS0VT,TD "E.'!,U2U ∑ #(%..!)∑ #/#(). ∑ '57!(!4) ∑ (9$%2!"!$ *!,!.$(!2∑ +/,+!4! ∑ ,5#+./7 ∑ -5-"!) ∑ 2!.#() ∑ NEW DELHI ).$)! 53! '(!.!∑ +%.9! OBJECT-ORIENTED PROGRAMMING C++ SIMPLIFIED © by Laxmi Publications (P) Ltd. All rights reserved including those of translation into other languages. In accordance with the Copyright (Amendment) Act, 2012, no part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording or otherwise. Any such act or scanning, uploading, and or electronic sharing of any part of this book without the permission of the publisher constitutes unlawful piracy and theft of the copyright holder’s intellectual property. If you would like to use material from the book (other than for review purposes), prior written permission must be obtained from the publishers. Typeset at ABRO Enterprises, Delhi First Edition: 2015 ISBN 978-93-81159-50-7 Limits of Liability/Disclaimer of Warranty: The publisher and the author make no representation or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties. The advice, strategies, and activities contained herein may not be suitable for every situation. In performing activities adult supervision must be sought. Likewise, common sense and care are essential to the conduct of any and all activities, whether described in this book or otherwise. Neither the publisher nor the author shall be liable or assumes any responsibility for any injuries or damages arising herefrom. The fact that an organization or Website if referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Website may provide or recommendations it may make. Further, readers must be aware that the Internet Websites listed in this work may have changed or disappeared between when this work was written and when it is read. All trademarks, logos or any other mark such as Vibgyor, USP, Amanda, Golden Bells, Firewall Media, Mercury, Trinity, Laxmi appear - ing in this work are trademarks and intellectual property owned by or licensed to Laxmi Publications, its subsidiaries or affiliates. Notwithstanding this disclaimer, all other names and marks mentioned in this work are the trade names, trademarks or service marks of their respective owners. & Bengaluru 080-26 75 69 30 & Chennai 044-24 34 47 26, 24 35 95 07 & Cochin 0484-237 70 04, 405 13 03 & Guwahati 0361-254 36 69, 251 38 81 & Hyderabad 040-27 55 53 83, 27 55 53 93 & Jalandhar 0181-222 12 72 Branches & Kolkata 033-22 27 43 84 PUBLISHED IN INDIA BY & Lucknow 0522-220 99 16 & Mumbai 022-24 91 54 15, 24 92 78 69 UNIVERSITY SCIENCE PRESS & Ranchi 0651-220 44 64 (An Imprint of Laxmi Publications Pvt.Ltd.) An ISO 9001:2008 Certifed Company 113, GOLDEN HOUSE, DARYAGANJ, NEW DELHI - 110002, INDIA Telephone : 91-11-4353 2500, 4353 2501 Fax : 91-11-2325 2572, 4353 2528 C— www.laxmipublications.com [email protected] Contents CHAPTER 0 : INTRODUCTION TO OOPs 1–9 Structured Programming 1 Procedural Programming 3 Programming Methodology 3 Object-Oriented Programming 5 Basic Concepts of OOPs 5 Characteristics of OOPs 8 Advantages of OOPs 9 Object-Oriented Languages 9 Object-based Languages 9 CHAPTER 1 : INTRODUCTION OF OBJECT-ORIENTED DESIGN 10–19 1.1 Introduction 10 1.2 Objects 11 1.3 Class and Instance 11 1.4 Polymorphism 12 1.5 Inheritance 12 1.6 Object-Oriented Analysis 12 1.7 Finding the Objects 13 1.8 Conceptual Modeling 13 1.9 Requirements Model 13 1.10 Analysis Model 13 1.11 The Design Model 13 1.12 The Implementation Model 14 1.13 Test Model 14 1.14 Object-Oriented Analysis and Design 14 1.15 The Evolution of Object Model 15 1.16 Object-Oriented Programming 15 1.17 Object-Oriented Design 15 1.18 Object-Oriented Analysis 16 (v) (vi) 1.19 Elements of Object Model 16 1.20 The Role of OOAD in the Software Life Cycle 17 1.21 OOAD Methodologies 18 1.22 Grady Booch Approach 18 CHAPTER 2 : STARTING WITH C++ 20–46 2.1 C++ Overview 20 2.2 C++ Character Set 20 2.3 C++ Tokens 21 2.4 Variables 25 2.5 Counting Tokens 26 2.6 Data Types 26 2.7 Qualifiers 27 2.8 Range of Data Types 28 2.9 Your First C++ Program 29 2.10 Structure of a C++ Program 33 2.11 Styles of Writing C++ Programs 35 2.12 Programming Examples 35 2.13 Ponderable Points 45 Exercise 45 CHAPTER 3 : C FEATURES OF C++ 47–115 3.1 Introduction 47 3.2 Operators and Expressions 47 3.3 Declaring Constants 72 3.4 Type Conversion 76 3.5 Decision Making: An Introduction 77 3.6 Unconditional Branching Using Goto 92 3.7 Introduction to Looping 94 3.8 Points to Ponder 110 Exercise 112 CHAPTER 4 : OPERATORS AND REFERENCES IN C++ 116–148 4.1 Introduction 116 4.2 Scope Resolution Operator 116 4.3 Reference Variables 122 4.4 The Bool Data Type 127 4.5 The Operator New and Delete 129 (vii) 4.6 Malloc Vs New 140 4.7 Pointer Member Operators 140 4.8 Ponderable Points 147 Exercise 148 CHAPTER 5 : FUNCTION IN C++ 149–192 5.1 Introduction 149 5.2 Function Declaration/Prototyping 150 5.3 The Main Function in C++ 154 5.4 Recursion 154 5.5 Call by Reference 157 5.6 Call by Reference Vs Call by Address 165 5.7 Return by Reference 165 5.8 Inline Function 169 5.9 Function Overloading 175 5.10 Function with Default Arguments 183 5.11 Ponderable Points 189 Exercise 190 CHAPTER 6 : CLASS AND OBJECTS IN C++ 193–278 6.1 Working with Class 193 6.2 Programming Examples (Part-1) 197 6.3 Structure in C++ 215 6.4 Accessing Private Data 216 6.5 Programming Example (Part-2) 220 6.6 Passing and Returning Object 240 6.7 Array of Object 248 6.8 Friend Function 254 6.9 Static Class Members 266 6.10 Constant Member Function 274 Exercise 277 CHAPTER 7 : WORKING WITH CONSTRUCTOR AND DESTRUCTOR 279–333 7.1 Introduction 279 7.2 Constructor with Parameters 281 7.3 Implicit and Explicit Call to Constructor 283 7.4 Copy Constructor 306 7.5 Dynamic Initialization of Objects 311 (viii) 7.6 Dynamic Constructor 316 7.7 Destructor 328 7.8 Ponderable Points 332 Exercise 332 CHAPTER 8 : WORKING WITH OPERATOR OVERLOADING 334–392 8.1 Introduction 334 8.2 Operator Overloading with Binary Operator 336 8.3 Overloading Assignment (=) Operator 346 8.4 Overloading Unary Operators 348 8.5 Overloading Using Friend Function 358 8.6 Rules of Operator Overloading 370 8.7 Type Conversion 371 8.8 Ponderable Points 390 Exercise 391 CHAPTER 9 : WORKING WITH INHERITANCE IN C++ 393–457 9.1 Introduction 393 9.2 Types of Inheritance 393 9.3 Public, Private and Protected Inheritance 398 9.4 Multiple Inheritance 424 9.5 Hierarchical Inheritance 431 9.6 Virtual Base Class 435 9.7 Constructor and Destructor in Inheritance 442 9.8 Containership 453 9.9 Ponderable Points 456 Exercise 456 CHAPTER 10 : POINTERS TO OBJECTS AND VIRTUAL FUNCTIONS 458–510 10.1 Pointer to Objects 458 10.2 The This Pointer 465 10.3 What is Binding in C++ ? 469 10.4 Virtual Functions 470 10.5 Working of a Virtual Function 476 10.6 Rules for Virtual Function 485 10.7 Pure Virtual Function and Abstract Class 485 10.8 Object Slicing 498 (ix) 10.9 Some Facts about Virtual Function 501 10.10 Virtual Destructor 504 10.11 Ponderable Points 508 Exercise 509 CHAPTER 11 : INPUT-OUTPUT AND MANIPULATORS IN C++ 511–562 11.1 Introduction 511 11.2 C++ Stream Classes 511 11.3 Unformatted Input/Output 513 11.4 Formatted Input/Output Operations 524 11.5 Manipulators 545 11.6 Ponderable Points 561 Exercise 561 CHAPTER 12 : FILE HANDLING IN C++ 563–608 12.1 Introduction 563 12.2 File Streams 564 12.3 Opening and Closing a File 564 12.4 File Opening Modes 569 12.5 Checking End of File 574 12.6 Random Access in File 580 12.7 Command Line Arguments 587 12.8 Working with Binary Mode 592 12.9 Error Handling 603 12.10 Ponderable Points 607 Exercise 607 CHAPTER 13 : TEMPLATE PROGRAMMING 609–642 13.1 Introduction 609 13.2 Function Template 609 13.3 Class Template 625 13.4 Ponderable Points 641 Exercise 642 CHAPTER 14 : EXCEPTION HANDLING IN C++ 643–667 14.1 Introduction 643 14.2 Basics of Exception Handling 643 14.3 Exception Handling Mechanism 645 (x) 14.4 Programming Examples 646 14.5 Exception Handling with Class 652 14.6 Catching all Exceptions 662 14.7 Specifying Exception for a Function 664 14.8 Ponderable Points 666 Exercise 666 CHAPTER 15 : OBJECT-ORIENTED PROGRAMMING HAND ON LAB 668–741 Experiment 1 : Program illustrating function overloading feature. 668 Experiment 2 : Programs illustrating the overloading of various operators. Ex : Binary operators, Unary operators, New and delete operators, etc. 671 Experiment 3 : Programs illustrating the use of following functions : (a) Friend functions (b) Inline functions (c) Static member functions (d) Functions with default arguments. 682 Experiment 4 : Programs to create singly and doubly linked lists and perform insertion and deletion Operations.