An Object Oriented Programming with C By Tanmay Kasbe Dr. Ravi Singh Pippal IDEA PUBLISHING WWW.ideapublishing.in i
Publishing-in-support-of, IDEA PUBLISHING Block- 9b, Transit Flats, Hudco Place Extension Near Andrews Ganj, New Delhi Zipcode : 110049 Website: www.ideapublishing.in Copyright, Author All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted, in any form by any means, electronic, mechanical, magnetic, optical, chemical, manual, photocopying, recording or otherwise, without the prior written consent of its writer. ISBN: 978-81-932706-4-6 Price: 599.00 D The opinions/ contents expressed in this book are solely of the author and do not represent the opinions/ standings/ thoughts of idea publishing. Printed in India ii
About the Author X Tanmay Kasbe is currently working as an asst. professor in computer department. He is pursuing PhD in computer science application and holds an MCA degree from RGPV University, Bhopal. He has a long experience of teaching postgraduate, graduate and engineering students. He has 7+ year of experience as a freelancer in software development in.net technologies. He is also the author of "DBMS Concepts- A Practical approach". Dr. Ravi Singh Pippal is presently working as a Professor (Computer Science) in RKDF University, Bhopal, India. He has obtained Ph.D. from ABV-Indian Institute of Information Technology and Management, Gwalior. His primary research areas of interest include Cryptology (Cryptography, Cryptanalysis), Steganography, Key Management, Image Encryption and many more. iii
About the book X This book provides a comprehensive and practical overview of the object oriented programming with C++, It has been thoughtfully structured to introduce the readers to all the important concept of C++ though a single book. This book with its numerous programming examples with explanation will be an ideal text for undergraduate and postgraduate student of computer science and application (MCA and BCA). Besides, for any programmer who wishes to know the fundamental C++, this would be a useful book. iv
Table of Contents CHAPTER 1 :: Software Programming Language 1-18 1.1 Introductions1 1.2 Levels of Programming Language 4 Low Level Programming Language4 Machine Language5 Assembly Language6 High Level Programming Language 7 1.3 Hierarchy of Programming Language 9 1.4 Translator Program9 Assembler 11 Compiler 12 Interpreter14 1.5 Differences between compiler & Interpreter15 1.6 Introductions of Linker & Loader16 Key Points to Remember18 CHAPTER 2 :: Object Oriented Programming Language 19-36 2.1 Introduction 19 2.2 Procedure Oriented Programming 20 2.3 Comparison of OOPs & POP 22 2.4 Structure Programming 23 2.5 Object Oriented Programming Concepts 24 Objects 24 Class26 v
Polymorphism 27 Inheritance 28 Data Abstraction 29 Data Encapsulation 31 Message Passing 33 2.6 Advantages of OOPs 34 Key Points to Remember 36 CHAPTER 3 :: Basic Features of C++ 37-76 3.1 Introductions of C++ 37 3.2 Differences between C++ & C 38 3.3 Tokens in C++ 39 Keywords 40 Variables/Identifiers 41 Constants/Literals 46 Integer-Constants 46 Character-constants 47 Floating-constants 48 Strings-constants 49 Punctuators 49 Operators 51 Arithmetic Operators 51 Relational Operators 52 Logical Operators 54 Bit-wise Operators 55 Assignment Operator 55 Misc Operators 56 3.4 Data Types in C++ 57 vi
Built-In data types 58 Int 58 Float 61 Char 62 Double User define data types 61 Structure 63 Union 66 Class 67 Enumerations 3.5 Expression & Statements 67 Integral expressions 68 Float expressions 68 Relational or Boolean expressions 69 Logical expressions 69 Bitwise expressions 69 Pointer expressions 70 3.6 Data Type Conversion 70 3.7 Manipulator functions 72 Endl 73 hex, dec, oct setbase 73 setw setprecision 74 ends flush Key Points to Remember 75 vii
CHAPTER 4 :: C++ Programs & Standards 77-98 4.1 Introductions 77 4.2 Guidelines for Programs 77 4.3 Structure of C ++ Programs 83 4.4 Header file in C++ Programs 85 Iostream.h Cout<< functions Cin>> functions Conio.h Math.h Other header files 4.5 First program in C 90 4.6 Compilation & Execution 91 4.7 Formatting Output 93 Key Points to Remember 97 CHAPTER 5 :: Functions 99-140 5.1 Introductions 99 5.2 Why Functions? 104 5.3 Function Example 105 Function prototyping OR Function declarations 107 Declaring type of functions 108 Defining a function 108 Actual argument & formal argument 109 Return Statement 111 5.4 Calling a function by using call by value 112 5.5 Calling a function by using call by Reference 115 viii
5.6 Comparisons of call by value & call by reference 117 5.7 Multi-function 118 5.8 Programming example 120 5.9 Recursion 125 5.10 Recursion programs example 125 Key Points to Remember 138 CHAPTER 6 :: Classes & Objects 141-176 6.1 Introductions 141 6.2 Difference between class & structures 142 6.3 Structure of class 142 Syntax of class Variable Declaration Class Function Definition Access specifiers Public 144 Private 145 Protected 146 Defining the object of a class 148 Accessing member function using object 148 6.4 Defining function outside the class 153 6.5 Inline Functions 157 6.6 Friend Functions 161 6.7 Programming Examples 166 6.8 Multiple classes 170 6.9 Static Class members 172 Static Data members 173 Static Member functions 174 ix
Key Points to Remember 176 CHAPTER 7:: Control Statements 177-238 7.1 Introductions 177 7.2 Decision making/conditional Statements 178 Simple if statement 178 If...else statement 180 Nested if...else statement 187 7.3 Selection statement (Switch) 191 7.4 Loop Statements 204 While Loop 205 Do..While Loop 213 For loop 215 7.5 Comparision of for, while, do..while loop 230 7.6 Jumping statements 232 Break 232 Continue 233 Key Points to Remember 236 CHAPTER 8 : : Constructors & Destructors 239-266 8.1 Introductions 239 8.2 What is constructors 240 8.3 Constructors types 243 Default Constructors 243 Parameterized constructors 246 Copy constructors 250 Dynamic constructors 253 8.4 Multiple constructor in class 255 8.5 Programming examples 258 x
8.6 Destructors 262 Key Points to Remember 266 CHAPTER 9 : : Array, String Handling and Pointers 267-320 9.1 Introductions 267 9.2 Array 267 One Dimensional Array 268 Two Dimensional Array 282 M Dimensional Array 294 9.3 String Handling 300 9.4 Pointer Introduction 306 What is pointer? 308 9.5 This pointer 312 9.6 Dynamic Memory Allocation 314 Key Points to Remember 320 CHAPTER 10 : : Compile Time Polymorphism 321-356 10.1 Introductions 321 10.2 Function Overloading 321 10.3 Default Arguments 328 10.4 Operator Overloading 332 10.5 Overloading Unary Operator 333 Overloading using Friend Function 339 10.6 Overloading Binary Operator 342 Overloading using Friend Function 343 Key Points to Remember 354 CHAPTER 11 : : Inheritance 357-420 11.1 Introductions 357 xi
11.2 Syntax for defining derived classes 358 Access Specifiers 359 11.3 Single Inheritance 363 11.4 Multiple Inheritance 372 11.5 Multilevel Inheritance 380 11.6 Hierarchical Inheritance 386 11.7 Hybrid Inheritance 392 11.8 Constructors in Derived Class 400 11.9 Protected Access Specifiers 407 11.10 Virtual Base Class 413 Key Points to Remember 418 CHAPTER 12 : : Run Time Polymorphism 421-436 12.1 Introduction 421 12.2 Virtual Function 421 12.3 Difference between function overloading & overriding 429 12.4 Pure Virtual Function 430 12.5 Early Binding & Late Binding 434 CHAPTER 13 : : Exception Handling & 437-462 13.1 Introduction 437 13.2 Exception Handling 440 Try Block 440 Catching Errors 441 Throwing an Error 442 Multiple Catch Statement 446 Catch All Exception 449 13.3 Templates 452 Function Template 453 xii
Class Template 458 Key Points to Remember 461 CHAPTER 14 : : File Handling 463-482 14.1 Introduction 463 14.2 File Stream 465 14.3 Opening & Closing a File 466 Opening a file in C++ 467 Using Constructor 468 Using Open () function 468 Closing a file in C++ 470 Binary File Program 475 Appendix:(A): 75 Objective Question with answers key Appendix:(B): Complete Programs according to Universities Syllabus 483-504 513-516 xiii
Introduction to software programming language Chapter 1 INTRODUCTION TO SOFTWARE PROGRAMMING LANGUAGE 1.1 Introduction A software programming language is a special language programmers use to create software programs or other sets of instructions for computers to execute. These commands can be interpreted into a code understood by a machine. Programs are created through programming languages to control the behavior and output of a machine. As we all know computer cannot understand anything, for everything we must provide a set of instruction which is executed by programming language & then provide us an output. All the instruction which we have writes using any high level programming language is called source code. Most of the programming languages use translator programs to translate instructions into computer specific language. Although there are over 2,000 computer languages, relatively few are widely used. If we are talking on current scenario then JAVA &.NET are the most 1
Introduction to software programming language popular languages which are used by most of the small and large MNC for software development. A programming language is typically divided into three elements: Syntax Semantic Logic Developments Syntax: Like human languages, each programming language has its own grammar and syntax. In computer programming language it is very important to follow complete syntax otherwise it will not execute. A translator program responsibility is to check whether our program follow all the syntax of the language or not. If any mistake found by translator program then translator program will give errors and we must remove all the error then only our program will execute successfully. Syntax deals solely with the form and structure of symbols in a language without any consideration given to their meaning. Each programming languages has its own syntax. If we take some of the C programming language syntax likes ; (Semicolon) used or represent termination of line. (Double Equals) means when we have compared two values 2
Introduction to software programming language X ++ means we have increase the value of X by 1 Semantics: Semantics describes the behavior that a computer follows when executing a program in the language. We might disclose this behavior by describing the relationship between the input and output of a program or by a step-by-step explanation of how a program will execute on a real or an abstract machine. Semantics follow directly from syntax. Semantics deal with the meaning assigned to the symbols, characters and words. Syntax is what the computer understands; semantics is what the human understands. Examples: The sentence "Baby milk drinks" does not have a syntactic meaning, but through semantics most people would interpret it as meaning Baby drinks milk as our prior knowledge tells us that a baby drinks milk, and therefore we can find a meaning from the key words. The Arabic symbol '3' (for example) contains no meaning in and of itself. They are just pixels. You & I assign meaning to it. We assign the numeric value 'three'. Indeed the whole literate world seems to agree with us in this numeric assignment. And obviously, it works for us. But only because we agree on this convention. 3
Catch Your Complete Book From Idea Store www.ideapublishing.in