c++ raju sir

  C with Class is C++. It means, without understanding Object Oriented Programming System (OOPS) Concepts properly, no one can understand C++ and any other Modern Programming Language like Java, C#, VB.Net, etc… So, In this EBook, I have covered each and every concept related to OOPS and I have tried to Implement each OOPS Concept with easy to understand Demo C++ Program. In this EBook, I have defined more then 350 Programs and hundreds of Code Fragement to clear each and every C++ and OOPS Concept. So, This EBook would not only be easy to learn OOPS and C++ but also very useful if you are interested in learning Java or .NET Language like C# or VB.NET. 1 www.BccFalna.com C++ in Hindi Kuldeep Chand BetaLab Computer Center Falna 2 www.BccFalna.com C++ in Hindi C++ Programming Language in Hindi Copyright © 2011 by Kuldeep Chand All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editors: Kuldeep Chand Distributed to the book trade worldwide by Betalab Computer Center, Behind of Vidhya Jyoti School, Falna Station Dist. Pali (Raj.) Pin 306116 e-mail bccfalna@gmail.com, or visit http://www.bccfalna.com For information on translations, please contact Betalab Computer Center, Behind of Vidhya Jyoti School, Falna Station Dist. Pali (Raj.) Pin 306116 Phone 097994-55505 The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, the author shall not have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this book. 3 www.BccFalna.com C++ in Hindi This book is dedicated to those who really wants to be a ProfessionAl Developer 4 www.BccFalna.com C++ in Hindi Index of Contents 5 www.BccFalna.com C++ in Hindi Table of Contents OOPS and C++ ................................................................................ 11 The Object-Oriented Approach................................................................................14 Features of Object-Oriented Languages .................................................................16 Classes .................................................................................................................18 Data Hiding, Data Abstraction and Encapsulation ..................................................20 Inheritance ............................................................................................................22 Reusability ............................................................................................................22 Creating New Data Types ....................................................................................23 Polymorphism and Overloading ...........................................................................23 Dynamic Binding...................................................................................................24 Message Passing .................................................................................................25 Benefits Of OOPS....................................................................................................28 Object Oriented Languages .....................................................................................28 Object-Based Programming Language ...................................................................29 Object-Oriented Programming Language................................................................29 OOPS with C++ ................................................................................ 31 Class and Objects....................................................................................................31 Basic C++ Data Types.............................................................................................55 Assignment Operator ( = )........................................................................................56 Escape Sequences ..................................................................................................56 Integers.....................................................................................................................57 Unsigned Integers.................................................................................................58 Floating Point ...........................................................................................................58 Comments................................................................................................................60 Type Conversion ( Type Casting )...........................................................................73 Abstract Data Types.................................................................................................83 Reusability..............................................................................................................119 Arrays and Strings..........................................................................154 Array Fundamentals...............................................................................................154 Defining an Array....................................................................................................154 Multidimensional Arrays.........................................................................................159 Arrays as Instance Data.........................................................................................160 Employee Class .....................................................................................................161 The Enter Key.....................................................................................................165 Postfix and Prefix Increment Operators .............................................................166 The Stack Class..................................................................................................166 Pushing and Popping .........................................................................................168 An Array Disguised as a Stack...........................................................................170 Arrays of Objects ................................................................................................170 Array of Time Objects.........................................................................................171 Strings ....................................................................................................................173 String Variables ..................................................................................................173 String Constants .................................................................................................174 Using const Variables.........................................................................................176 String Library Functions......................................................................................181 Copying String (strcpy() Function).........................................................................182 Appending Strings (strcat() Function) ................................................................182 Comparing Strings (strcmp() Function)..............................................................183 Self-Made String Class ..........................................................................................184 6 www.BccFalna.com C++ in Hindi Arrays of Strings .................................................................................................188 The weekday Class ............................................................................................190 Functions ........................................................................................206 Function Calls.........................................................................................................206 Function Definitions................................................................................................208 Function Declarations ............................................................................................211 Standalone Member Functions..............................................................................213 Inline Functions ......................................................................................................213 Specifying an Inline Function .............................................................................214 Member Functions Defined Within a Class ...........................................................215 Member Functions Defined Outside a Class.........................................................216 Revised weekdays Program..................................................................................217 Macros....................................................................................................................220 Overloaded Functions............................................................................................221 Default Arguments .................................................................................................226 Declarations and Definitions ..................................................................................232 Lifetime and Visibility..............................................................................................233 Automatic Variables............................................................................................234 Register Variables ..............................................................................................235 External Variables...............................................................................................235 Local Static Variables .........................................................................................237 Objects ...................................................................................................................239 Visibility of Instance Data ...................................................................................239 Lifetime of Instance Data....................................................................................239 Creating Static Data............................................................................................240 Accessing Static Data.........................................................................................240 Static Functions ..................................................................................................241 Reference Arguments............................................................................................244 Swapping Integers ..............................................................................................244 Passing by Value ................................................................................................244 Passing by Reference ........................................................................................245 Returning by Reference......................................................................................250 Constructors ...................................................................................254 Constructor Arguments..........................................................................................261 No-Argument Constructor......................................................................................263 The One-Argument Constructor ............................................................................264 Initialize Array Size.................................................................................................271 Initializing a Member Array Elements....................................................................276 Copy Constructors .................................................................................................277 The Default Copy Constructor...............................................................................279 const for Function Arguments................................................................................280 const Objects..........................................................................................................286 const Function........................................................................................................288 Operator Overloading .....................................................................295 The operator X() Function......................................................................................296 Relational Operators ..............................................................................................304 Assignment Operators ...........................................................................................307 Overloading Unary Operators................................................................................310 Prefix Version of Operator ++.............................................................................311 Postfix Version of Operator ++ ...........................................................................313 The Unary Minus Operator.................................................................................316 7 www.BccFalna.com C++ in Hindi Conversion from Objects to Basic Types ..............................................................319 Type Casting: Conversion for Basic Types ...........................................................320 Conversions Between Classes..............................................................................327 Overloading the Assignment Operator (=).............................................................334 Overloading [ ] Operator.........................................................................................341 Constant Overloaded Operators............................................................................345 *this Object.............................................................................................................349 Inheritance ......................................................................................357 Reusability..............................................................................................................357 Inheritance and Program Design...........................................................................358 Composition: A “Has a” Relationship.....................................................................359 Inheritance: A “Kind of” Relationship .....................................................................360 Class Hierarchy......................................................................................................369 Reusability..............................................................................................................375 The Base Class Constructor..................................................................................379 The protected Access Specifier.............................................................................379 Constructors and Inheritance.................................................................................381 The Great Chain of Constructors .......................................................................381 No Argument Inheritance Constructor................................................................385 Arguments Inheritance Constructor ...................................................................386 Adding Functionality to the Derived Class Constructor.....................................388 Access Specifier.....................................................................................................390 Public Inheritance ...............................................................................................400 Private Inheritance..............................................................................................402 Protected Inheritance..........................................................................................404 Composition ...........................................................................................................409 Multiple Inheritance................................................................................................417 Pointers ...........................................................................................428 Addresses and Pointers.........................................................................................428 Pointers to Objects.................................................................................................434 Pointer to void ........................................................................................................438 Pointers and Arrays................................................................................................439 Pointers and Functions ..........................................................................................442 Pointers and Strings...............................................................................................447 Membership Access Operator (->).........................................................................451 new Operator..........................................................................................................454 Delete Operator......................................................................................................456 Creating Objects with new .....................................................................................459 this and const.........................................................................................................464 Pointers and the const Modifier .............................................................................469 Linked List Class....................................................................................................479 Containers ..............................................................................................................483 Virtual Functions and Friend Functions ........................................491 Polymorphism.........................................................................................................491 Normal Member Functions Accessed with Pointers..............................................493 Virtual Member Functions Accessed with Pointers ...............................................495 Late Binding ...........................................................................................................497 Arrays of Pointers to Objects.................................................................................499 Passing Reference.................................................................................................513 Passing Pointers ....................................................................................................517 Abstract Classes ....................................................................................................525 8 www.BccFalna.com C++ in Hindi Pure Virtual Functions............................................................................................527 Abstract Classes and Pure Virtual Functions ........................................................532 Pure Virtual Functions with Bodies ........................................................................532 Virtual Destructors..................................................................................................533 Friend Functions.....................................................................................................544 Friend Classes .......................................................................................................556 Interclass Communication......................................................................................556 Pointers in Interclass Communication ...................................................................558 Nested Classes......................................................................................................561 Communication between Nested Classes.............................................................562 Exception Handling ........................................................................565 Throwing Multiple Exceptions ................................................................................572 Specifying Data in an Exception Class..................................................................587 Initializing an Exception Object..............................................................................588 Extracting Data from the Exception Object............................................................588 Exception and Function Nesting ............................................................................591 Streams and Files ...........................................................................593 Stream Classes......................................................................................................593 Stream Class Hierarchy.........................................................................................593 ios Class.................................................................................................................595 Formatting Flags.................................................................................................595 Manipulators .......................................................................................................596 Functions ............................................................................................................597 Istream Class .........................................................................................................599 The ostream Class.................................................................................................600 The iostream and the _withassign Classes...........................................................600 Predefined Stream Objects....................................................................................601 Stream Errors.........................................................................................................601 Error-Status Bits .................................................................................................602 Inputting Numbers ..............................................................................................603 Too Many Characters .........................................................................................603 No-Input Input .....................................................................................................604 Inputting Strings and Characters........................................................................605 Error-Free Distances ..........................................................................................605 All-Character Input..............................................................................................609 Disk File I/O with Streams......................................................................................609 Formatted File I/O...............................................................................................610 Writing Data........................................................................................................610 Reading Data......................................................................................................612 Strings with Embedded Blanks...........................................................................613 Detecting End-of-File..........................................................................................615 Character I/O ......................................................................................................616 Direct Access to the streambuf Object...............................................................617 Binary I/O................................................................................................................618 Object I/O............................................................................................................620 Writing an Object to Disk....................................................................................620 Reading an Object from Disk .............................................................................621 Compatible Data Structures ...............................................................................622 I/O with Multiple Objects.....................................................................................623 The fstream Class..................................................................................................626 The Mode Bits.....................................................................................................626 9 www.BccFalna.com C++ in Hindi Error Handling in File I/O ....................................................................................627 Reaction to Errors...............................................................................................628 Analyzing Errors .................................................................................................629 File Pointers ...........................................................................................................631 Specifying the Position .......................................................................................632 Specifying the Offset...........................................................................................632 The tellg() Function.............................................................................................634 File I/O Using Member Functions.......................................................................634 Object That Read and Write Themselves..............................................................634 Classes That Read and Write Themselves...........................................................638 Static Functions......................................................................................................639 Size Of Derived Objects.........................................................................................639 Using the typeid( ) Function...................................................................................640 Interaction with empl_io .........................................................................................648 Overloading the << and >> Operators...................................................................650 Overloading for cout and cin..................................................................................651 Overloading for Files ..............................................................................................654 Overloading for Binary I/O .....................................................................................657 Memory as a Stream Object..................................................................................660 Fixed Buffer Size....................................................................................................661 The ostrstream Object............................................................................................662 Input Memory Streams...........................................................................................663 Universality.............................................................................................................664 File Pointers ...........................................................................................................664 Dynamic Buffer Size...............................................................................................664 Last but not Least. There is more…...............................................666 10 www.BccFalna.com C++ in Hindi OOPS & C++ 11 www.BccFalna.com C++ in Hindi OOPS and C++ lcls igyk loky rk s ;gh g S fd C++ D;k sa lh[kk tk,\ ; s vkt dh i zHkko”kkyh Hkk’kk g SA tc Programmers dk s cM s o tfVy i zk sx zke cukus gk srs g Sa] rc Professional Programmers C++ dk s Choose djrs g SaA db Z vk Sj Hkh ljy o i zHkkoh Hkk’kk, a g Sa] ysfdu mudh d qN dfe;k sa dh otg ls muesa i zk sx zke Development dh ,d lhek g SA t Sls Visual Basic Microsoft Company dh ,d cg qr gh ljy Hkk’kk g S] ysfdu tc i zk sx zke cg qr gh cM s o tfVy gk srs g Sa] rc bl Hkk’kk esa Program Develop djuk le>nkjh dh ckr ugh a gk srh g SA D;k safd bl Hkk’kk esa Graphics dk cg qr i z;k sx gk srk g S] vk Sj Hkh db Z dkj.k g Sa] ftlls ;fn bl Hkk’kk esa cM s i zk sx zke Develop fd, tk, a rk s i zk sx zke dh Speed cg qr de gk s tkrh g SA Assembly Hkk’kk Hkh dkQh vPNh g SA blesa fy[k s x, i zk sx zkek sa dh xfr dkQh vPNh gk srh g S] ysfdu ; s Hkk’kk Hardware d s Device Drivers d s i zk sx zke fy[kus d s fy; s T;knk vPNh g S uk fd Application i zk sx zke fy[kus d sA blh rjg Java Internet d s fy; s vPNh g S] gkyk afd Java C++ ls gh i zsfjr g SA ysfdu ,d cM s Standalone Application Development djus d s fy; s C++ lcls yk sdfi z; Hkk’kk g SA ; s ,d cg qr gh Flexible o Best Performing Language g SA Procedural Languages Pascal, C, Basic, Fortran t Slh ikjEifjd Hkk’kk, a Procedural Languages d s mnkgj.k g SaA ftlesa i zR; sd Statement Computer dk s d qN dke djus dk vkns”k nsrk g SA ;kuh Procedural Languages Instructions dk ,d lewg gk srk g SA Procedural Languages esa Nk sVs Programs d s fy; s fdlh Hkh vU; i zdkj d s Pattern dh vko”;drk ugh gk srh g SA Programmer Instructions dh List cukrk g S vk Sj Computer mud s vuqlkj dke djrk g SA tc i zk sx zke dkQh cM s o tfVy gk s tkrs g Sa] rc Instructions dh ;g List dkQh ijs”kkuh i Snk djrh g SA blfy; s ,d cM s i zk sx zke dk s Nk sVs&Nk sVs VqdMk sa esa ck aV fn;k tkrk g SA bu Nk sVs&Nk sVs VqdMk sa dk s Functions dgk tkrk g SA Functions dks nwljh vU; Hkk’kkvk sa esa Subroutine, Subprogram ;k Procedure dgk tkrk g SA ,d cM s i zk sx zke dk s Nk sVs&Nk sVs Functions esa foHkkftr djus ls i wjk Program Functions dk ,d lewg cu tkrk g S] ftls Module dgk tkrk g SA ysfdu ; s Modules Hkh Procedural Programming d s vUrx Zr gh vkrs g aS D;k safd lHkh Functions esa Statements dh ,d List gk srh g S vk Sj lHkh Functions fey dj i wjk Program cukrs g Sa] ftlls i wjk Program Instructions dh ,d cg qr cMh List cu tkrk g SA 12 www.BccFalna.com C++ in Hindi Procedural Languages d s “k q:vkrh nk Sj esa buesa gh Program Develop fd, tkrs Fk sA “C” Hkh ,d Procedural Languages g S vk Sj tc “C” Hkk’kk dk vkfo’dkj g qvk Fkk] rc Programmers vU; Hkk’kkvk sa dk s Nk sM dj “C” esa gh vius Program Develop djus yx s Fk sA ysfdu le; o vko”;drk d s vuqlkj tc Program cM s o tfVy gk sus yx s] rc Programmers dk s bl Hkk’kk esa i zk sx zke cukus esa fnDdrsa vkus yxh aA mUgk sus eglwl fd;k fd bl Hkk’kk esa d qN lq/kkj dh vko”;drk g S rkfd ; s Hkk’kk ljy o yk sdfi z; cu ld sA ; s Hkk’kk ljy cu ld s bld s fy; s bldk okLrfod thou d s vuqlkj gk suk t:jh FkkA ;kuh ge gekjs lkekU; thou esa ftl i zdkj ls O;ogkj djrs g Sa] bl Hkk’kk dk Hkh o Slk gh gk suk t:jh Fkk rkfd Programmers blesa vf/kd ljyrk o lQyrk ls Program cuk ld saA Hkk’kk okLrfod thou d s vuqlkj gk s] ;gh Concept Object Oriented Programming ;kuh OOPS dk vk/kkj cukA “C” Hkk’kk dh bu dfe;k sa dk s igpkuk x;k vk Sj blesa lq/kkj fd;k x;kA QyLo:i gesa “C” Hkk’kk dk ,d u;k laLdj.k “C++” i zkIr g qvkA vkb; sa] ge Hkh tkuus dh dk sf”k”k djrs g Sa fd “C” Hkk’kk esa , slh dk Sulh dfe;k a Fkh a] ftuesa lq/kkj dh vko”;drk eglwl dh xb Z \ Procedural Languages esa dke gk sus dk egRo Fkk Data dk ugh a] ;kuh fd Keyboard ls Data Input fd;k tk,] Data ij Processing dh tk,] Errors dk s Check fd;k tk, vkfnA Functions esa Hkh blh egRo dk s tkjh j[kk x;kA Functions dk sb Z dke djrs g Sa] mlh i zdkj ls ftl i zdkj ls lk/kkj.k Statement djrk g SA Functions dk sb Z tfVy dke Hkh dj ldrs g Sa] ysfdu buesa Hkh dke d s gk sus dk gh egRo FkkA i wjs Program esa Data ij dk sb Z /;ku ugh a fn;k tkrk Fkk] tcfd i wjs izk sx zke dk ewy vk/kkj Data gh gk srk g SA fdlh Inventory d s Program esa bl ckr dk dk sb Z T;knk egRo ugh a gk srk g S fd Data dk s fdl i zdkj ls Display fd;k tkrk g S ;k ,d Function fdl i zdkj ls Corrupt Data dk s Check djrk g S] cfYd bl ckr dk gk srk g S fd Data D;k g S vk Sj og fdl i zdkj ls Program esa dke vk jgk g SA Procedural Program esa Data dk s f}rh; Lrj ij j[kk x;k Fkk tcfd fdlh Hkh Program dk ewy vk/kkj Data gh gk srk g SA mnkgj.k d s fy; s] fdlh Inventory d s Program esa fdlh Data File dk s Memory esa Load fd;k tkrk g S] rc ; s File ,d Global Variable dh rjg gk srh g S] ftls dk sb Z Hkh Function Use dj ldrk g SA ; s Functions Data ij fofHkUu i zdkj d s Operations djrs g SaA ;kuh ; s Data dk s Read djrs g S] Analyze djrs g Sa] Update djrs g Sa] Rearrange djrs g Sa] Display djrs g Sa vk Sj okil Disk ij Write djrs g SaA “C” esa Local Variables Hkh gk srs g Sa ysfdu Local Variables, egRoi w.k Z Data d s fy; s brus mi;k sxh ugh a gk srs g Sa] tk s fd fofHkUu Functions }kjk Access fd, tkrs g SaA 13 www.BccFalna.com C++ in Hindi eku yk s fd ,d u, Programmer dk s Data dk s fdlh [kkl rjhd s ls Analyze djus d s fy; s ,d Function fy[kus dk s dgk x;kA i zk sx zke dh x w 

Comments