Object-Oriented Programming Using C++ 9789389447460


163 57 22MB

English Pages [514] Year 2019

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
Cover
Half Titlle
Title
Copyright
Preface
Acknowledgements
Contents
Chapter 1: Different Programming Paradigms and C++ Environment
1.1 Introduction
1.2 Procedure-Oriented Programming
1.3 Object-Oriented Programming
1.4 Comparison between Object-Oriented and Procedure-Oriented Programming
1.5 Functional Decomposition
1.6 Data Decomposition
1.7 Basic Concepts of Object-Oriented Programming
1.8 Advantages and Disadvantages of Object-Oriented Programming
1.9 Application of OOPS
1.10 Comparison between C and C++
1.11 C++ Program Development Environment
1.12 Program Compilation Process
1.13 The C++ Language Standards
1.14 C++ Standard Libraries
1.15 Introduction to Various C++ Compilers
Summary
Practice Questions
Chapter 2: Moving from C to C++
2.1 Introduction
2.2 Tokens
2.3 Keywords
2.4 Identifiers and Constants
2.5 Data Types
2.6 Declaration of Variables
2.7 A Simple C++ Program
2.8 Dynamic Initialization of Variables
2.9 Operators and Manipulators
2.10 Expressions
2.11 Casts
2.12 Selection Statements
2.13 Iteration Statements
Solved Programs
Summary
Practice Questions
Chapter 3: Functions
3.1 Functions
3.2 About int main() Function in C++
3.3 Function Prototyping
3.4 Use of void
3.5 C++ Function Definition
3.6 C++ Function Prototype and Function Definition Example
3.7 Default Values of Arguments of Functions
3.8 const Function Arguments and Return Types
3.9 Recursion
3.10 Function Overloading
Solved Programs
Summary
Practice Questions
Chapter 4: Classes and Objects–I
4.1 Encapsulation and Information Hiding
4.2 Data Abstraction
4.3 Abstract Data Types
4.4 Structures in C++
4.5 Classes and Objects
4.6 Define C++ Objects
4.7 C++ Class Member Functions
4.8 Inline and External Functions
4.9 Nesting of Member Functions
4.10 const Member Function
4.11 const Objects
4.12 Memory Allocation for Objects
4.13 Reference Variable
4.14 State, Identity and Behaviour of an Object
Solved Programs
Summary
Practice Questions
Chapter 5: Classes and Objects-II
5.1 static Data Members
5.2 Static Member Functions
5.3 Using Array of Objects
5.4 Arrays as Class Members
5.5 Passing Objects as Parameters
5.6 Friend Functions
5.7 Member Function of One Class as Friend Function of Other Class
5.8 Friend Class
5.9 Functions Returning Objects
Some Miscellaneous Important Topics
Solved Programs
Summary
Practice Questions
Chapter 6: Pointers and Dynamic Memory Allocation
6.1 Introduction to Dynamic Memory Management
6.2 new Operator
6.3 delete Operator
6.4 1-Dimensional Array Allocation using New and
6.5 2-Dimensional Array as Array of Arrays
6.6 Pointer to Object
6.7 Dynamic Memory Allocation and Deallocation for an Array of Objects
6.8 this Pointer
6.9 void Pointer
6.10 Constant Pointer
6.11 Pointer to Constant
6.12 Comparison between Pointer to Constant and Constant Pointer
6.13 Pointer to Function
Summary
Practice Questions
Chapter 7: Constructors and Destructors
7.1 Introduction
7.2 Constructor
7.3 Types of Constructors
7.4 Constructor Overloading
7.5 Dynamic Initialization of Objects
7.6 Dynamic Constructor
7.7 Destructor
Solved Programs
Summary
Practice Questions
Chapter 8: Inheritance
8.1 Introduction
8.2 Types of Inheritance
8.3 Derived Class
8.4 Inheritance Visibility Modes
8.5 Access Control to Private, Protected and Public Members of the Class
8.6 Multilevel Inheritance
8.7 Ambiguity Resolution in Multilevel Inheritance (Function Overriding)
8.8 Multiple Inheritance
8.9 Ambiguity Resolution in Multiple Inheritance in C++
8.10 Hierarchical Inheritance
8.11 Hybrid Inheritance
8.12 Multipath Inheritance and Virtual Base Class
8.13 Constructors in Derived Classes
8.14 Nested Classes/Object Composition/Containership/Aggregation/
8.15 Symbols for Inheritance and Aggregation Techniques
Solved Programs
Summary
Practice Questions
Chapter 9: Polymorphism
9.1 Introduction
9.2 Function Call Binding with Class Objects
9.3 Function Call Binding using Base Class Pointer
9.4 Virtual Functions
9.5 Using Virtual Keyword and Accessing Private Method of Derived Class
9.6 Mechanism of Late Binding
9.7 Virtual Functions in Multilevel Inheritance
9.8 Explicit Casting to Enable a Derived Class Pointer to Point to Base
9.9 Make a Virtual Function Inline
9.10 Pure Virtual Functions
9.11 Abstract Base Class
9.12 Object Slicing in C++
Solved Programs
Summary
Practice Questions
Chapter 10: Operator Overloading
10.1 Introduction
10.2 Advantage of Operator Overloading
10.3 Creating a Member Operator Function
10.4 Overloading Increment (++) Operator in C++
10.5 Best Practices while using Operator Overloading
10.6 Nameless Temporary Object to be Returned
10.7 Creating Prefix and Postfix Forms of the Increment and
10.8 Binary Operator Overloading
10.9 Overloading the Shorthand Operators
10.10 Operator Overloading using a Friend Function
10.11 Using a Friend to Overload ++ or – –
10.12 Overloading Prefix and Postfix Decrement Operator (– –) using
10.13 Advantage of using Friend Operator Functions
10.14 Overload “” for Index
10.15 Overloading new and delete
10.16 Overloading Some Special Operators
10.17 Overloading Comma Operator
10.18 Overloading “”
10.19 Data Conversion
Solved Programs
Summary
Practice Questions
Chapter 11: Parametric Polymorphism: The Generic Programming
11.1 Introduction
11.2 Function Templates
11.3 Class Template
Solved Programs
Summary
Practice Questions
Chapter 12: Namespace
12.1 Namespace
12.2 Definition of Namespace
12.3 Using a Namespace
12.4 Detailed Example Codes for Accessing Namespace Variables with
12.5 Class Declaration Inside Namespace and Definition Outside Namespace
12.6 Define Methods of Class Outside the Namespace
12.7 Anonymous or Unnamed Namespaces
12.8 Discontiguous Namespaces
12.9 Nesting of Namespace
12.10 The std Namespace
Summary
Practice Questions
Chapter 13: Exception Handling
13.1 Introduction
13.2 Generation of an Exception
13.3 Mechanism of Exception Handling
13.4 Multiple Exceptions Handling using Multiple Catch Blocks
13.5 General-Purpose Catch Block for Handling All Exceptions
13.6 Exceptions Thrown by Functions Invoked from within Try Block
13.7 Rethrowing an Exception
13.8 Specifying Exceptions
Summary
Practice Questions
Chapter 14: Managing Input and Output
14.1 Introduction
14.2 Stream I/O
14.3 C++ Stream Classes
14.4 I/O Library Header Files
14.5 Formatted Console I/O Operations
14.6 ios Class Functions and Flags
14.7 Using Manipulators
14.8 Unformatted Console I/O Operations
Solved Programs
Summary
Practice Questions
Chapter 15: File Handling
15.1 Introduction
15.2 File Stream Classes: Hierarchy
15.3 Opening and Closing a File
15.4 Detecting End-of-File
15.5 File Modes
15.6 File Pointers
15.7 Error Handling During File Operations
15.8 Sequential Access to a File
15.9 Random Input and Output Operations
15.10 Command-Line Arguments
Solved Programs
Summary
Practice Questions
Chapter 16: Standard Template Library
16.1 Introduction
16.2 Containers
16.3 Algorithms
16.4 Iterators
16.5 Functors
16.6 Application of Container Classes
16.7 Application of Algorithms
16.8 Application of Function Objects
Summary
Practice Questions
Bibliography
Index
Backcover
Recommend Papers

Object-Oriented Programming Using C++
 9789389447460

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

Object-Oriented Programming Using C++

Object-Oriented Programming Using C++

Kavita Pabreja

PhD (BITS, Pilani) Associate Professor Department of Computer Applications Maharaja Surajmal Institute GGS Indraprastha University, New Delhi

Neetu Narwal

PhD (Banasthali Vidyapeeth) Associate Professor Department of Computer Applications Maharaja Surajmal Institute GGS Indraprastha University, New Delhi

I.K. International Publishing House Pvt. Ltd. New Delhi

©Copyright 2019 I.K. International Pvt. Ltd., New Delhi-110002. This book may not be duplicated in any way without the express written consent of the publisher, except in the form of brief excerpts or quotations for the purposes of review. The information contained herein is for the personal use of the reader and may not be incorporated in any commercial programs, other books, databases, or any kind of software without written consent of the publisher. Making copies of this book or any portion for any purpose other than your own is a violation of copyright laws. Limits of Liability/disclaimer of Warranty: The author and publisher have used their best efforts in preparing this book. The author make no representation or warranties with respect to the accuracy or completeness of the contents of this book, and specifically disclaim any implied warranties of merchantability or fitness of any particular purpose. There are no warranties which extend beyond the descriptions contained in this paragraph. No warranty may be created or extended by sales representatives or written sales materials. The accuracy and completeness of the information provided herein and the opinions stated herein are not guaranteed or warranted to produce any particulars results, and the advice and strategies contained herein may not be suitable for every individual. Neither Dreamtech Press nor author shall be liable for any loss of profit or any other commercial damages, including but not limited to special, incidental, consequential, or other damages. Trademarks: All brand names and product names used in this book are trademarks, registered trademarks, or trade names of their respective holders. Dreamtech Press is not associated with any product or vendor mentioned in this book. ISBN: 978-93-89447-46-0

EISBN: 978-93-89698-95-4 Edition: 2019

Preface

Computer Science programming has come a long way since 1979 Bjarne Stroustrup,  employee of Bell AT & T, started working on language C with classes. Till 1983, it was called C with classes, and in 1983 it was named C++. Even today, C++ is one of the most popular object-oriented programming languages. It is a middle level language that is powerful, efficient and fast. It is used in operating systems, networking, embedded systems, and game development. In C++, lifetimes of all objects are exactly known, and (well-written) C++ programs guarantee that resources are acquired and released in fully deterministic manner. The objective of this book is to make you an excellent programmer. We assume that you have already completed a foundation course in C programming. This book takes you through a journey of the concepts of C++ language. All the topics have been covered in simple language. To make you understand each topic thoroughly, every topic has been explained first, followed by programs based on the same. You will find programs related to banking sector, educational institutes, sports and other real life domains. A sufficient number of diagrams have been added as a “picture is worth a thousand words”. It is our endeavour to make you a master of this popular language. This book has been designed for the needs of “object-oriented programming using C++” subject of Undergraduate courses like BCA; and CSE and IT branches of B.Tech of Guru Gobind Singh Indraprastha University, New Delhi, India. It covers each and every topic of these syllabi in detail with the help of programs. At the end of every chapter, solved programs for students to practise are given. Chapter 1 through Chapter 16 cover the entire syllabus of “Object-Oriented Programming” subject of B.Tech. program of GGSIP University whereas Chapter 1 through Chapter 15 are required to be studied for the students of BCA program of GGSIP University. The book is equally suitable for those who want to study on their own, be it student of any university pursuing undergraduate programmes like B.Sc(CS), B.Sc.(IT), BCA, B.Tech; post-graduate programs like MCA, M.Sc.(CS), M.Sc(IT), MCM and the like. The book is

vi Preface also very informative for those who wish to prepare for qualifying examinations for jobs in IT industry. All programs written in this book have been tested on the most commonly used compiler, i.e., Turbo C++ version 3.0 by Borland International Inc., unless specified otherwise. The programs can also be run on Dev C++, Codeblocks with just a small addition of “using namespace std” directive in the beginnning. This book is a result of our untiring efforts and we would like our readers to let us know about errors, if any and give valuable suggestions for improvement of this book. You may reach us at [email protected] and [email protected]  

Kavita Pabreja Neetu Narwal

Acknowledgements

First and foremost, I would like to thank God, the Almighty for giving me the strength, knowledge, ability and opportunity to undertake this project. I also wish to put on record special thanks to my parents who have been a guiding force for all my achievements. I am especially indebted to Prof.(Dr.) Rattan K. Datta who as my guide, has taught me more than I could ever give him credit for here. I owe a heartfelt thanks to my husband Manoj for standing beside me throughout my career and while writing this book. No set of thanks can ever be complete without acknowledging love and support of my dear kids Ujjawal and Nishtha. Thanks also go to all the faculty members and management of my place of work – Maharaja Surajmal Institute. Special gratitude goes to my students of BCA who have contributed towards proof reading of this book.  Kavita Pabreja I thank God for his blessings throughout the entire sphere of my life. Special gratitude goes to my husband, Dr. Sandeep Narwal and my sweet little daughter, Priyanka. No set of acknowledgements can ever be complete without thanking my parents who have always supported, helped and motivated me beyond measures.  Neetu Narwal

Contents

Prefacev Acknowledgementsvii 1. Different Programming Paradigms and C++ Environment 1 1.1 Introduction 1 1.2 Procedure-Oriented Programming 2 1.3 Object-Oriented Programming 3 1.4 Comparison between Object-Oriented and Procedure-Oriented Programming 3 1.5 Functional Decomposition 4 1.6 Data Decomposition 5 1.7 Basic Concepts of Object-Oriented Programming 5 1.8 Advantages and Disadvantages of Object-Oriented Programming 9 1.9 Application of OOPS 10 1.10 Comparison between C and C++ 11 1.11 C++ Program Development Environment 12 1.12 Program Compilation Process 15 1.13 The C++ Language Standards 17 1.14 C++ Standard Libraries 18 1.15 Introduction to Various C++ Compilers 20 Summary 21 Practice Questions 22 2. Moving from C to C++ 2.1 Introduction 2.2 Tokens 2.3 Keywords

23 23 24 24

x Contents 2.4 Identifiers and Constants 24 2.5 Data Types 25 2.6 Declaration of Variables 28 2.7 A Simple C++ Program 29 2.8 Dynamic Initialization of Variables 31 2.9 Operators and Manipulators 31 2.10 Expressions 34 2.11 Casts 35 2.12 Selection Statements 36 2.13 Iteration Statements 47 Solved Programs 55 Summary 60 Practice Questions 61 3. Functions 62 3.1 Functions 62 63 3.2 About int main() Function in C++ 3.3 Function Prototyping 63 64 3.4 Use of void 3.5 C++ Function Definition 64 3.6 C++ Function Prototype and Function Definition Example 65 3.7 Default Values of Arguments of Functions 67 3.8 const Function Arguments and Return Types 70 3.9 Recursion 71 3.10 Function Overloading 72 Solved Programs 74 Summary 80 Practice Questions 81 4. Classes and Objects–I 4.1 Encapsulation and Information Hiding 4.2 Data Abstraction 4.3 Abstract Data Types 4.4 Structures in C++ 4.5 Classes and Objects 4.6 Define C++ Objects 4.7 C++ Class Member Functions

82 82 83 84 84 86 88 89

Contents  xi

4.8 Inline and External Functions 92 4.9 Nesting of Member Functions 96 4.10 const Member Function 97 4.11 const Objects 98 4.12 Memory Allocation for Objects 99 4.13 Reference Variable 101 4.14 State, Identity and Behaviour of an Object 105 Solved Programs 105 Summary 110 Practice Questions 111 5. Classes and Objects-II 113 5.1 static Data Members 113 5.2 Static Member Functions 116 5.3 Using Array of Objects 118 5.4 Arrays as Class Members 120 5.5 Passing Objects as Parameters 122 5.6 Friend Functions 124 5.7 Member Function of One Class as Friend Function of Other Class 130 5.8 Friend Class 132 5.9 Functions Returning Objects 134 Some Miscellaneous Important Topics 136 Solved Programs 139 Summary 149 Practice Questions 150 6. Pointers and Dynamic Memory Allocation 6.1 Introduction to Dynamic Memory Management 6.2 new Operator 6.3 delete Operator 6.4 1-Dimensional Array Allocation using New and Deallocation using Delete 6.5 2-Dimensional Array as Array of Arrays 6.6 Pointer to Object 6.7 Dynamic Memory Allocation and Deallocation for an Array of Objects 6.8 this Pointer

151 151 152 153 154 155 159 161 164

xii Contents 6.9 void Pointer 167 6.10 Constant Pointer 168 6.11 Pointer to Constant 169 6.12 Comparison between Pointer to Constant and Constant Pointer 170 6.13 Pointer to Function 170 Summary 172 Practice Questions 173 7. Constructors and Destructors 175 7.1 Introduction 175 7.2 Constructor 176 7.3 Types of Constructors 177 7.4 Constructor Overloading 183 7.5 Dynamic Initialization of Objects 185 7.6 Dynamic Constructor 188 7.7 Destructor 190 Solved Programs 193 Summary 196 Practice Questions 196 8. Inheritance 8.1 Introduction 8.2 Types of Inheritance 8.3 Derived Class 8.4 Inheritance Visibility Modes 8.5 Access Control to Private, Protected and Public Members of the Class 8.6 Multilevel Inheritance 8.7 Ambiguity Resolution in Multilevel Inheritance (Function Overriding) 8.8 Multiple Inheritance 8.9 Ambiguity Resolution in Multiple Inheritance in C++ (Function Overriding) 8.10 Hierarchical Inheritance 8.11 Hybrid Inheritance 8.12 Multipath Inheritance and Virtual Base Class 8.13 Constructors in Derived Classes 8.14 Nested Classes/Object Composition/Containership/Aggregation/ has a Relationship

198 198 200 202 209 210 210 214 216 219 221 222 225 231 238

Contents  xiii

8.15 Symbols for Inheritance and Aggregation Techniques 242 Solved Programs 243 Summary 247 Practice Questions 248 9. Polymorphism 250 9.1 Introduction 250 9.2 Function Call Binding with Class Objects 251 9.3 Function Call Binding using Base Class Pointer 252 9.4 Virtual Functions 253 9.5 Using Virtual Keyword and Accessing Private Method of Derived Class 255 9.6 Mechanism of Late Binding 256 9.7 Virtual Functions in Multilevel Inheritance 258 9.8 Explicit Casting to Enable a Derived Class Pointer to Point to Base Class Object 259 9.9 Make a Virtual Function Inline 260 9.10 Pure Virtual Functions 260 9.11 Abstract Base Class 262 9.12 Object Slicing in C++ 262 Solved Programs 266 Summary 269 Practice Questions 270 10. Operator Overloading 10.1 Introduction 10.2 Advantage of Operator Overloading 10.3 Creating a Member Operator Function 10.4 Overloading Increment (++) Operator in C++ 10.5 Best Practices while using Operator Overloading 10.6 Nameless Temporary Object to be Returned 10.7 Creating Prefix and Postfix Forms of the Increment and Decrement Operators 10.8 Binary Operator Overloading 10.9 Overloading the Shorthand Operators 10.10 Operator Overloading using a Friend Function 10.11 Using a Friend to Overload ++ or – –

272 272 273 273 274 276 277 278 281 285 287 289

xiv Contents 10.12 Overloading Prefix and Postfix Decrement Operator (– –) using Friend Function 290 10.13 Advantage of using Friend Operator Functions 292 10.14 Overload “” for Index 294 10.15 Overloading new and delete 295 10.16 Overloading Some Special Operators 298 10.17 Overloading Comma Operator 305 10.18 Overloading “” 307 10.19 Data Conversion 309 Solved Programs 319 Summary 326 Practice Questions 327 11. Parametric Polymorphism: The Generic Programming 328 11.1 Introduction 328 11.2 Function Templates 329 11.3 Class Template 335 Solved Programs 343 Summary 350 Practice Questions 350 12. Namespace 352 12.1 Namespace 352 12.2 Definition of Namespace 353 12.3 Using a Namespace 354 12.4 Detailed Example Codes for Accessing Namespace Variables with Scope Resolution Operator / Using Directive / Using Declarative 358 12.5 Class Declaration Inside Namespace and Definition Outside Namespace 361 12.6 Define Methods of Class Outside the Namespace 362 12.7 Anonymous or Unnamed Namespaces 363 12.8 Discontiguous Namespaces 364 12.9 Nesting of Namespace 365 12.10 The std Namespace 367 Summary 370 Practice Questions 371

Contents  xv

13. Exception Handling 372 13.1 Introduction 372 13.2 Generation of an Exception 373 13.3 Mechanism of Exception Handling 374 13.4 Multiple Exceptions Handling using Multiple Catch Blocks 377 13.5 General-Purpose Catch Block for Handling All Exceptions 379 13.6 Exceptions Thrown by Functions Invoked from within Try Block 380 13.7 Rethrowing an Exception 382 13.8 Specifying Exceptions 383 Summary 385 Practice Questions 386 14. Managing Input and Output 387 14.1 Introduction 387 14.2 Stream I/O 387 14.3 C++ Stream Classes 389 14.4 I/O Library Header Files 390 14.5 Formatted Console I/O Operations 390 14.6 ios Class Functions and Flags 391 14.7 Using Manipulators 406 14.8 Unformatted Console I/O Operations 410 Solved Programs 417 Summary 421 Practice Questions 421 15. File Handling 15.1 Introduction 15.2 File Stream Classes: Hierarchy 15.3 Opening and Closing a File 15.4 Detecting End-of-File 15.5 File Modes 15.6 File Pointers 15.7 Error Handling During File Operations 15.8 Sequential Access to a File 15.9 Random Input and Output Operations 15.10 Command-Line Arguments

423 423 424 425 430 431 434 437 443 445 453

xvi Contents Solved Programs 457 Summary 463 Practice Questions 463 16. Standard Template Library 465 16.1 Introduction 465 16.2 Containers 466 16.3 Algorithms 467 16.4 Iterators 471 16.5 Functors 471 16.6 Application of Container Classes 473 16.7 Application of Algorithms 483 16.8 Application of Function Objects 486 Summary 487 Practice Questions 488 Bibliography 489 Index 491

1

Different Programming Paradigms and C++ Environment Concepts covered in this chapter LLProcedure-oriented programming LLObject-oriented programming LLFunctional decomposition LLData decomposition

LLBasic concepts of object-oriented programming LLBenefits of OOP

LLAdvantages and disadvantages of object-oriented programming LLApplications of OOP

LLDifference between C and C++

LLC++ Program development environment LLProgram compilation process LLC++ language standards LLC++ standard libraries

LLIntroduction to various C++ compilers

1.1 Introduction C++ was developed from the C programming language and it retains C as a subset. C++ emphasizes on modularity, strongly typed interfaces, and flexibility. It has as good a balance of facilities for writing large programs as any other language has. The C++ class concept has proven itself to be a powerful conceptual tool. In C++ several generalizations and some major extensions were added: these included multiple inheritance, static member functions, constant member functions, protected members, templates, exception handling, run-time type identification, and namespaces. During 1983-1985, C++ was written by Bjarne Stroustrup at Bell Labs in Murray Hill, New Jersey, USA. C++ is an extension of C. Before 1983, Bjarne Stroustrup added some features to C and he called “C with Classes”. He combined the Simula’s classes and objectoriented features with the power and efficiency of C. The term C++ was first used in 1983. The name signifies the evolutionary nature of the changes from C; ‘‘++’’ is the C increment operator.

2  Object-Oriented Programming Using C++ In June 1991, ANSI (American National Standard Institute) standardization of C++ became part of an ISO (International Standard Organization) standardization effort for C++. It is regarded as a middle-level language, as it comprises a combination of both highlevel and low-level language features. C++ is used by hundreds of thousands of programmers in essentially every application domain. Early application tended to have a strong systems programming flavour. For example, several major operating systems (Windows, Apple OS) have been written in C++. C++ is very efficient to write device drivers and other software that rely on direct manipulation of hardware under real-time constraints. Many machine learning algorithms are written in C++. Also it is the obvious choice for certain tasks in the game development pipeline. In such code, predictability of performance is at least as important as raw speed. Often, so is compactness of the resulting system. C++ was designed so that every language feature is usable in code under time and space constraints. C++ is widely used for teaching and research. C++ has maintained its place among top 5 programming languages worldwide. Evolution of C++ • During 1970 Dennis Ritchie created C Programming language. • During the creation of Ph.D. thesis, Bjarne Stroustrup worked with language called Simula. • Simula was the first language to support object-oriented programming paradigm. • Bjarne Stroustrup identified that this OOP feature can be included in the software development. • During 1979 Bjarne Stroustrup started working on the C language at Bell labs and added more OOP features to the classic C. • He added features in such a fashion that the basic flavour of C remained unaffected. The language was originally named C with Classes but later it was renamed C++ in 1983. • C++ includes some add-on features such as classes, basic inheritance, in-lining, default function arguments, and strong type checking.

1.2  Procedure-Oriented Programming Programs are made up of modules, which are parts of a program that can be coded and tested separately, and then assembled to form a complete program. In procedural languages (i.e., C) these modules are procedures, where a procedure is a sequence of statements. In C, for example, procedures are a sequence of imperative statements, such as assignments, tests, loops and invocations of sub-procedures. These procedures are functions, which map arguments to return statements. The design method used in procedural programming is called Top Down Design. It starts with a problem (procedure) and then systematically breaks down the problem into sub-problems (sub-procedures). This is also called as Functional Decomposition, which

Different Programming Paradigms and C++ Environment  3

continues until a sub-problem is straightforward enough to be solved by the corresponding sub-procedure. The difficulties with this type of programming is that software maintenance is difficult and time consuming. When changes are made to the main procedure (top), those changes can cascade to the sub-procedures of the main, and the sub-sub procedures and so on, where the change may impact all procedures in the pyramid.

1.3 Object-Oriented Programming One alternative to procedural programming is object-oriented programming. Objectoriented programming is meant to address the difficulties with procedural programming. In object-oriented programming, the main modules in a program are classes, rather than procedures. The object-oriented approach lets you create classes and objects that model real-world objects. Object-oriented programming is a programming paradigm that uses abstraction (in the form of classes and objects) to create models based on the real-world environment. An object-oriented application uses a collection of objects, which communicate by passing messages to request services. Objects are capable of passing messages, receiving messages, and processing data. The aim of object-oriented programming is to try to increase the flexibility and maintainability of programs. Because programs created using an OO language are modular, they are easier to develop, and simpler to understand after development.

1.4 Comparison Between Object-Oriented And Procedure   Oriented Programming There are many differences between object-oriented and procedure-oriented programming paradigms which are mentioned in Table 1.1. Table 1.1:  Comparison between object-oriented and procedure-oriented programming Procedural Programming

Object-oriented Programming

Program is divided into small parts called functions. Program is divided into small parts called objects. Importance is not given to data, but to functions Importance is given to the data rather than as well as sequence of actions to be done. functions, because it works as a real world. It follows bottom-up approach. Object 1

It follows top-down approach. Main function

Function 1

Function 2

Function 3

Object 2

Data member

Data member

Member function

Member function Object 3

Function 4

Function 5

Data member Member function

Contd...

4  Object-Oriented Programming Using C++ Procedural Programming

Object-oriented Programming

It doesn’t have any access specifiers.

It has access specifiers, i.e., public, private, protected.

Data can move freely from function to function in the system.

Objects can move and communicate with each other through member function.

It is not easy to add new data and functions.

It provides an easy way to add new data and functions.

It does not have any proper way for hiding data. It provides data hiding. So, it is more secure. So, it is less secure. Overloading is not possible.

Overloading is possible in the form of function overloading and operator overloading

Example: C, Visual Basic, Fortran, Pascal Structure of C program struct student {   int rno;   int name[20]; }; int main() {   struct student s;   printf(“Enter Roll No”);  scanf(“%d”,s.rno);   printf(“Enter Name”);  gets(s.name);   printf(“Roll No %d”, s.rno);   printf(“Name %s”, s.name);   return 0; }

Example: C++, Java, Visual Basic.NET, C#.NET. Structure of C++ program class student {   int rno;   char name[20]; public: void get() {  cin>>rno;  gets(name); } void put() {   cout