Master of Computer Applications

Similar documents
SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

What are the characteristics of Object Oriented programming language?

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

1. Write two major differences between Object-oriented programming and procedural programming?

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Object Oriented Programming

RAJIV GANDHI COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF INFORMATION TECHNOLOGY OBJECT ORIENTED PROGRAMMING QUESTION BANK UNIT I 2 MARKS

Object Oriented Programming. C++ 6 th Sem, A Div Ms. Mouna M. Naravani

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

Lesson Plan. Subject: OBJECT ORIENTED PROGRAMMING USING C++ :15 weeks (From January, 2018 to April,2018)

B.C.A 2017 OBJECT ORIENTED PROGRAMMING USING C++ BCA303T MODULE SPECIFICATION SHEET

VALLIAMMAI ENGINEERING COLLEGE

Chapter 1: Object-Oriented Programming Using C++

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

SAURASHTRA UNIVERSITY

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Syllabus for Bachelor of Technology. Computer Engineering. Subject Code: 01CE1303. B.Tech. Year - II

Get Unique study materials from

Object Oriented Programming with c++ Question Bank

Absolute C++ Walter Savitch


Data Structures using OOP C++ Lecture 3

Proposed Syllabus for F.Y.B.Sc. (Information Technology) (w.e.f. June-2015)

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.

CHAPTER 5 GENERAL OOP CONCEPTS

Data Structures (list, dictionary, tuples, sets, strings)

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Object-Oriented Programming in C++/Handout 01 Page 1 of 8

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

What is a Programming Paradigm

S Y B Voc Software Development Syllabus

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

An Object Oriented Programming with C

Object oriented programming Concepts


B.Sc II Year Computer Science (Optional)

OBJECT ORIENTED PROGRAMMING

Sri Vidya College of Engineering & Technology

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

International Journal of Advance Research in Computer Science and Management Studies

B. V. Patel Institute of Business Management, Computer & Information Technology

Short Notes of CS201

Lecture Notes on Programming Languages

C++ Important Questions with Answers

CS201 - Introduction to Programming Glossary By

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

Unit 1 : Principles of object oriented programming

Problem Solving with C++

PROGRAMMING IN C++ COURSE CONTENT

Object-Oriented Programming (OOP) Fundamental Principles of OOP

STRUCTURING OF PROGRAM

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

END TERM EXAMINATION

Object-Oriented Programming

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Inheritance, and Polymorphism.

Overview. OOP: model, map, reuse, extend. Examples of objects. Introduction to Object Oriented Design

Introduction to Programming Using Java (98-388)

C++ (Non for C Programmer) (BT307) 40 Hours

Interview Questions of C++


Object-Oriented Design (OOD) and C++

Part 7 - Object Oriented Concepts Classes, Objects, Properties and Methods

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Chapter 5 Object-Oriented Programming

AN OVERVIEW OF C++ 1

DEPARTMENT OF COMPUTER APPLICATIONS B.C.A. - FIRST YEAR ( REGULATION) SECOND SEMESTER LESSON PLAN SRM INSTITUTE OF SCIENCE AND TECHNOLOGY

CS304 Object Oriented Programming Final Term

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Basics of Object Oriented Programming. Visit for more.

A STUDY OF OBJECT ORIENTED ANALYSIS AND DESIGN

Object Orientated Analysis and Design. Benjamin Kenwright

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

CS304- Object Oriented Programming LATEST SOLVED MCQS FROM FINALTERM PAPERS. MC

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

2. COURSE DESIGNATION: 3. COURSE DESCRIPTIONS:

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

LESSON PLAN B.C.A. - FIRST YEAR ( REGULATION) SECOND SEMESTER

Object-Oriented Programming Paradigm

Time : 3 hours. Full Marks : 75. Own words as far as practicable. The questions are of equal value. Answer any five questions.

Software Architecture (Lesson 2) Object-Oriented Paradigm (1)

CHOICE BASED CREDIT SYSTEM (With effect from )

CS304 Object Oriented Programming

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

Object- Oriented Design with UML and Java Part I: Fundamentals

Object-Oriented Programming

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. COURSE CURRICULUM COURSE TITLE: OBJECT ORINTED PROGRAMMING (Code: )

The Foundation of C++: The C Subset An Overview of C p. 3 The Origins and History of C p. 4 C Is a Middle-Level Language p. 5 C Is a Structured

Object-Oriented Programming

KLiC C++ Programming. (KLiC Certificate in C++ Programming)

+2 Volume II OBJECT TECHNOLOGY OBJECTIVE QUESTIONS R.Sreenivasan SanThome HSS, Chennai-4. Chapter -1

Object Oriented Programming

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

Partha Sarathi Mandal

Fast Introduction to Object Oriented Programming and C++

CSE 307: Principles of Programming Languages

Module 10 Inheritance, Virtual Functions, and Polymorphism

Transcription:

ALAGAPPA UNIVERSITY A [Accredited with A+ Grade by NAAC (CGPA:3.64) in the Third Cycle and Graded as Category I University by MHRD-UGC] KARAIKUDI 630 003 DIRECTORATE OF DISTANCE EDUCATION Master of Computer Applications 315 12 OBJECT ORIENTED PROGRAMMING AND C++ I - Semester

ALAGAPPA UNIVERSITY [Accredited with A+ Grade by NAAC (CGPA:3.64) in the Third Cycle and Graded as Category I University by MHRD-UGC] (A State University Established by the Government of Tamil Nadu) KARAIKUDI 630 003 Directorate of Distance Education Master of Computer Applications I - Semester 315 12 OBJECT ORIENTED PROGRAMMING AND C++

Author Rohit Khurana, CEO, ITL Education Solutions Ltd Units (1-14) "The copyright shall be vested with Alagappa University" All rights reserved. No part of this publication which is material protected by this copyright notice may be reproduced or transmitted or utilized or stored in any form or by any means now known or hereinafter invented, electronic, digital or mechanical, including photocopying, scanning, recording or by any information storage or retrieval system, without prior written permission from the Alagappa University, Karaikudi, Tamil Nadu. Information contained in this book has been published by VIKAS Publishing House Pvt. Ltd. and has been obtained by its Authors from sources believed to be reliable and are correct to the best of their knowledge. However, the Alagappa University, Publisher and its Authors shall in no event be liable for any errors, omissions or damages arising out of use of this information and specifically disclaim any implied warranties or merchantability or fitness for any particular use. Vikas is the registered trademark of Vikas Publishing House Pvt. Ltd. VIKAS PUBLISHING HOUSE PVT. LTD. E-28, Sector-8, Noida - 201301 (UP) Phone: 0120-4078900 Fax: 0120-4078999 Regd. Office: 7361, Ravindra Mansion, Ram Nagar, New Delhi 110 055 Website: www.vikaspublishing.com Email: helpline@vikaspublishing.com Work Order No. AU/DDE/DE1-238/Preparation and Printing of Course Materials/2018 Dated 30.08.2018 Copies - 500

SYLLABI-BOOK MAPPING TABLE Object Oriented Programming and C++ BLOCK 1: INTRODUCTION UNIT - 1: Introduction and Features: Evolution of Object Oriented Language, Object Oriented Paradigm, Basic Concept of Object-Oriented Programming - Objects, Classes, Encapsulation and Data Abstraction, Inheritance, Polymorphism, Dynamic Binding, Message Passing UNIT - 2: Popular OOP languages: Moving from C to C++ Introduction - Predefined Console Streams, Hierarchy of Console Stream Classes UNIT - 3: I/O Operations: Unformatted I/O Operations, Formatted Console I/O Operations, Manipulators, Custom/user-defined Manipulators. BLOCK 2 : CLASSES AND OBJECTS UNIT - 4: Classes and Objects: Introduction, Class Specification, Class Objects, Accessing Class Members, Defining Member Functions, Accessing Member Functions within a Class, Outside Member Functions as Inline, Private Member Function UNIT - 5: Memory Allocation for Objects: Array of Objects, Function Prototype, Call by Reference, Return by Reference, Objects as Function Arguments, Inline Function, Friend Function, Constant Parameter and Member Function. UNIT - 6: Object Initialization: Introduction - Constructors, Default Constructor, Parameterized Constructors, Multiple Constructors in a Class, Dynamic Initialization Through Constructors, Copy Constructor, Dynamic Constructor, Destructor. Dynamic Objects: Introduction, Pointers to Objects, Array of Pointers to Objects, this Pointer. BLOCK 3 : INHERITANCE, POLYMORPHISM AND DATA CONVERSION UNIT - 7: Inheritance: Introduction, Derived Class Declaration, Forms of Inheritance, Inheritance and Member Accessibility, Multilevel Inheritance, Multiple Inheritance, Hierarchical Inheritance, Hybrid Inheritance. UNIT - 8: Polymorphism: Introduction, Function Overloading, Operator Overloading Introduction, Unary Operator Overloading, Binary Operator Overloading, Assignment Operator Overloading, Overloading with Friend Functions. UNIT - 9: Data conversion: Conversion between Basic Data Types, Conversion between Objects and Basic Types, Conversion between Objects of Different Classes. Virtual Function: Introduction, Need for Virtual Functions, Pure Virtual Functions, Abstract Classes. Unit 1: Introduction and Features (Pages 1-17) Unit 2: Popular OOP Languages (Pages 18-25) Unit 3: I/O Operations (Pages 26-45) Unit 4: Classes and Objects (Pages 46-53) Unit 5: Memory Allocation for Objects (Pages 54-72) Unit 6: Object Initialization (Pages 73-105) Unit 7: Inheritance (Pages 106-135) Unit 8: Polymorphism (Pages 136-158) Unit 9: Data Conversion (Pages 159-179)

BLOCK 4: TEMPLATES AND FILES UNIT - 10: Generic Programming with Templates: Introduction - Class Templates - Class Template with Multiple Arguments UNIT - 11: Function Template: Function Template with Multiple Arguments. Inheritance of Class Template. UNIT - 12: Streams with Files: Introduction, Hierarchy of File Stream Classes, Opening and Closing of Files, File Pointers and their Manipulators, Sequential Access to a File, File Input/Output with Stream Class, Random Access to a File. BLOCK 5: EXCEPTION HANDLING UNIT - 13: Exception Handling: Introduction - Basics of Exception Handling, Exception Handling Mechanism, Throwing Mechanism, Catching Mechanism. Exceptions in Constructors and Destructors UNIT - 14: Other Exception Handling Methods: Handling Uncaught Exceptions, Exceptions in Operator Overloaded Functions, Exception in Inheritance Tree, Exceptions in Class Templates, Memory Allocation Failure Exception. Unit 10: Generic Programming with Templates (Pages 180-201) Unit 11: Function Template (Pages 202-214) Unit 12: Streams with Files (Pages 215-246) Unit 13: Exception Handling (Pages 247-265) Unit 14: Other Exception Handling Methods (Pages 266-280)

CONTENTS INTRODUCTION BLOCK I: INTRODUCTION UNIT 1 INTRODUCTION AND FEATURES 1-17 1.0 Introduction 1.1 Objectives 1.2 Evolution of Programming Methodologies: Object Oriented Language 1.3 Procedural Approach versus Object-Oriented Approach 1.4 Basic Concepts of OOP 1.4.1 Objects 1.4.2 Classes 1.4.3 Data Abstraction 1.4.4 Encapsulation 1.4.5 Inheritance 1.4.6 Polymorphism and Overloading 1.4.7 Message Passing 1.5 Object Oriented Paradigm 1.6 Answers to Check Your Progress Questions 1.7 Summary 1.8 Key Words 1.9 Self Assessment Questions and Exercises 1.10 Further Readings UNIT 2 POPULAR OOP LANGUAGES 18-25 2.0 Introduction 2.1 Objectives 2.2 Moving from C to C++: Introduction 2.3 Basic Concept of Streams 2.4 Answers to Check Your Progress Questions 2.5 Summary 2.6 Key Words 2.7 Self Assessment Questions and Exercises 2.8 Further Readings

UNIT 3 I/O OPERATIONS 26-45 3.0 Introduction 3.1 Objectives 3.2 Types of I/O Operations 3.2.1 Formatted I/O Operations 3.2.2 Formatting Using Manipulators 3.2.3 User-Defined Manipulators 3.2.4 Unformatted I/O Operations 3.3 Answers to Check Your Progress Questions 3.4 Summary 3.5 Key Words 3.6 Self Assessment Questions and Exercises 3.7 Further Readings BLOCK II: CLASSES AND OBJECTS UNIT 4 CLASSES AND OBJECTS 46-53 4.0 Introduction 4.1 Objectives 4.2 Defining a Class 4.2.1 Access Specifiers 4.2.2 Defining Member Functions 4.3 Creating Objects 4.3.1 Memory Allocation for Objects 4.3.2 Accessing Members of a Class 4.4 Answers to Check Your Progress Questions 4.5 Summary 4.6 Key Words 4.7 Self Assessment Questions and Exercises 4.8 Further Readings UNIT 5 MEMORY ALLOCATION FOR OBJECTS 54-72 5.0 Introduction 5.1 Objectives 5.2 Arrays as Class Members 5.2.1 Array of Objects 5.2.2 Objects as Function Arguments 5.2.3 Returning Objets from Function 5.2.4 const Member Functions

5.3 Friends to a Class 5.3.1 Friend Functions 5.3.2 Friend Class 5.3.3 Static Members of a Class 5.3.4 Inline Functions 5.4 Answers to Check Your Progress Questions 5.5 Summary 5.6 Key Words 5.7 Self Assessment Questions and Exercises 5.8 Further Readings UNIT 6 OBJECT INITIALIZATION 73-105 6.0 Introduction 6.1 Objectives 6.2 Constructors and Destructors 6.3 Introduction to Pointers 6.3.1 Pointers to Objects 6.3.2 Array of Pointers to Objects 6.3.3 this Pointer 6.4 Answers to Check Your Progress Questions 6.5 Summary 6.6 Key Words 6.7 Self Assessment Questions and Exercises 6.8 Further Readings BLOCK III: INHERITANCE, POLYMORPHISM AND DATA CONVERSION UNIT 7 INHERITANCE 106-135 7.0 Introduction 7.1 Objectives 7.2 Basic Concepts 7.3 Reusability and Extensibility 7.4 Defining a Derived Class 7.5 Accessibility of Base Class Members in Derived Class 7.5.1 Protected Access Specifier in Base Class 7.5.2 Making a Private Member Inheritable 7.6 Access Specifier of the Base Class 7.6.1 Publicly-Inherited Base Class 7.6.2 Privately-Inherited Base Class

7.6.3 Protectedly-Inherited Base Class 7.6.4 Restoring the Access Specifier of the Base Class Members 7.6.5 Allocating Memory to the Objects of Base Class and Derived Class 7.7 Types of Inheritance 7.7.1 Single Inheritance 7.7.2 Multilevel Inheritance 7.7.3 Hierarchical Inheritance 7.7.4 Multiple Inheritance 7.7.5 Hybrid Inheritance 7.8 Answers to Check Your Progress Questions 7.9 Summary 7.10 Key Words 7.11 Self Assessment Questions and Exercises 7.12 Further Readings UNIT 8 POLYMORPHISM 136-158 8.0 Introduction 8.1 Objectives 8.2 Function Overloading 8.3 Operator Overloading 8.3.1 Defining Operator Function 8.3.2 Rules for Overloading Operators 8.4 Overloading Unary Operators 8.4.1 Overloading Increment and Decrement Operators 8.5 Overloading Binary Operators 8.5.1 Overloading Arithmetic Assignment Operators 8.6 Operator Overloading Using Friend Function 8.7 Answers to Check Your Progress Questions 8.8 Summary 8.9 Key Words 8.10 Self Assessment Questions and Exercises 8.11 Further Readings

UNIT 9 DATA CONVERSION 159-179 9.0 Introduction 9.1 Objectives 9.2 Type Conversion 9.2.1 Conversions between Objects and Basic Types 9.2.2 Advanced Type Casting 9.3 Virtual Functions 9.3.1 Pure Virtual Function 9.3.2 Abstract Classes 9.4 Answers to Check Your Progress Questions 9.5 Summary 9.6 Key Words 9.7 Self Assessment Questions and Exercises 9.8 Further Readings BLOCK IV: TEMPLATES AND FILES UNIT 10 GENERIC PROGRAMMING WITH TEMPLATES 180-201 10.0 Introduction 10.1 Objectives 10.2 Class Template 10.3 Answers to Check Your Progress Questions 10.4 Summary 10.5 Key Words 10.6 Self Assessment Questions and Exercises 10.7 Further Readings UNIT 11 FUNCTION TEMPLATE 202-214 11.0 Introduction 11.1 Objectives 11.2 Function Template 11.3 Answers to Check Your Progress Questions 11.4 Summary 11.5 Key Words 11.6 Self Assessment Questions and Exercises 11.7 Further Readings

UNIT 12 STREAMS WITH FILES 215-246 12.0 Introduction 12.1 Objectives 12.2 File Streams 12.2.1 Classes for File Stream Operations 12.3 Opening and closing a File 12.3.1 Opening File using the Constructor 12.3.2 Opening File using the open()function 12.3.3 File Modes 12.3.4 Closing a File 12.4 File Input/Output with Stream Class 12.4.1 Sequential Input and Output Operations 12.5 Detecting End-of-File 12.6 File Pointers and their Manipulations 12.6.1 Specifying the Position 12.6.2 Specifying the Offset 12.7 Updating a File: Random Access 12.8 Answers to Check Your Progress Questions 12.9 Summary 12.10 Key Words 12.11 Self Assessment Questions and Exercises 12.12 Further Readings BLOCK V: EXCEPTION HANDLING UNIT 13 EXCEPTION HANDLING 247-265 13.0 Introduction 13.1 Objectives 13.2 Basics and Mechanism of Exception Handling 13.3 Throw Statement 13.4 Catching Mechanism 13.5 Answers to Check Your Progress Questions 13.6 Summary 13.7 Key Words 13.8 Self Assessment Questions and Exercises 13.9 Further Readings

UNIT 14 OTHER EXCEPTION HANDLING METHODS 266-280 14.0 Introduction 14.1 Objectives 14.2 Exception Handling 14.3 Exception in Class Template 14.4 Memory Allocation Failure Exception 14.5 Answers to Check Your Progress Questions 14.6 Summary 14.7 Key Words 14.8 Self Assessment Questions and Exercises 14.9 Further Readings

Introduction INTRODUCTION Object-oriented Programming (OOP) is one of the most impressive programming paradigms in software development. Today, C++ has become one of the most popular OOP languages used for developing real-world applications. C++ is a programming language that extended from the ubiquitous C language. It treats data as a crucial element not allowing it to move freely around the system. Therefore, the main emphasis in C is on data and not on the procedure. You can design programs around the data being operated upon in C++. An object-oriented language helps in combining data and functions that operate on data into a single unit known as object. C++ is used for developing different types of applications, such as real-time systems, simulation modelling, expert systems. It also provides flexibility to a user to introduce new types of objects in his programming on the basis of the requirement of the application. This book is divided into fourteen units. They include an introduction to object-oriented programming, objects and classes, arrays and strings, constructors and destructors, pointers in C++, polymorphism, inheritance, virtual functions, console and disk I/O operations, templates and exception handling. This book, Object Oriented Programming and C++, follows the SIM format or the self-instructional mode wherein each Unit begins with an Introduction to the topic followed by an outline of the Objectives. The detailed content is then presented in a simple and organized manner, interspersed with Check Your Progress questions to test the understanding of the students. A Summary along with a list of Key Words and a set of Self Assessment Questions and Exercises is also provided at the end of each unit for effective recapitulation. 12 Material

BLOCK - I INTRODUCTION Introduction and Features UNIT 1 INTRODUCTION AND FEATURES Structure 1.0 Introduction 1.1 Objectives 1.2 Evolution of Programming Methodologies: Object Oriented Language 1.3 Procedural Approach versus Object-Oriented Approach 1.4 Basic Concepts of OOP 1.4.1 Objects 1.4.2 Classes 1.4.3 Data Abstraction 1.4.4 Encapsulation 1.4.5 Inheritance 1.4.6 Polymorphism and Overloading 1.4.7 Message Passing 1.5 Object Oriented Paradigm 1.6 Answers to Check Your Progress Questions 1.7 Summary 1.8 Key Words 1.9 Self Assessment Questions and Exercises 1.10 Further Readings 1.0 INTRODUCTION The goal of programmers is to develop software that are reliable and maintainable, and satisfy all user requirements. Software development is not a static process. The software needs to be modified or redesigned according to changes in user requirements, business rules and strategies. In addition, the complexity of the software also increases. To cope with the dynamic nature and complexity of the software, different approaches of programming were developed since the invention of the computer. These approaches are known as programming paradigms. A programming paradigm (also known as programming methodology) describes the structure of a program. In other words, it determines how the instructions are placed in a program. Each programming language follows one or the other programming paradigm. There is no specific rule to decide which programming paradigm is to be followed as different paradigms can be used to develop different software. However, whatever paradigm is followed, some of the Material 1

Introduction and Features quality issues of software must be kept in mind. This includes correctness, usefulness, robustness, maintainability, reusability, portability, reliability and interoperability of software. 1.1 OBJECTIVES After going through this unit, you will be able to: Understand the evolution of various programming methodologies Explain the procedural programming paradigm and its limitations Describe the object-oriented programming paradigm and its fundamental concepts 1.2 EVOLUTION OF PROGRAMMING METHODOLOGIES: OBJECT ORIENTED LANGUAGE Initially, when the computers were invented, programs were written in the binary language. However, as programs grew in size, it became difficult to write programs using binary language. The assembly language was invented to write large programs was not very user-friendly. With the change in the user requirements, the size and the complexity of the programs continued to grow, which led to the development of high-level languages such as BASIC and FORTRAN. However, these languages provided an unstructured way of writing programs. In unstructured programming paradigm, all the instructions of a program were written one after the other in a single function and hence, were suitable for writing only small and simple programs. For large and complex programs, it became difficult to trace and debug errors. To overcome the limitations of unstructured programming paradigm, other programming paradigms, namely, procedural and object-oriented programming paradigms were developed, which helped the programmers to develop the programs in a structured way. 1.3 PROCEDURAL APPROACH VERSUS OBJECT- ORIENTED APPROACH 2 Material In the late 1960s, the high-level languages such as C and Pascal were developed, which provided a structured way of writing programs. Structured programming (also known as procedural programming) was a powerful and an easy approach of writing complex programs. In procedural programming, programs are divided into different procedures (also known as functions, routines or subroutines) and each procedure contains a set of instructions that performs a specific task. This

approach follows the top-down approach for designing a program. That is, first the entire program is divided into a number of subroutines and these subroutines are again divided into small subroutines and so on until each subroutine becomes an indivisible unit (see Figure 1.1). Main Program Introduction and Features subroutine-1 subroutine-2 subroutine-3 subroutine-4 subroutine-5 subroutine-6 subroutine-7 subroutine-8 subroutine-9 Fig. 1.1 Top-down Approach Programs in the procedural approach consist of a controlling procedure known as the main, which controls the execution of other procedures. When a call to a procedure is made, the program control is passed to that procedure and all the instructions in that procedure are executed one after another. After executing all the instructions, the program control returns to the procedure from where the call is made. For example, to write a program that can add, modify, find and delete students records from a database using structured programming, the entire program can be divided into four different procedures, namely add, find, delete and modify (see Figure 1.2). In addition to these procedures, a program consists of a main procedure. If a user wants to add a record of a student in a database, the control is passed from the main procedure to the add procedure. Once all the instructions are executed in the add procedure, the control is transferred back to the main procedure. Same steps are followed while calling other procedures. main() add(); find(); delete(); modify(); control passes from main to add() add()... control passes from add() to main find()... Fig. 1.2 Procedural Programming Material 3

Introduction and Features Though writing programs using the procedural approach is a simple and easy task, there are some limitations, which are as follows: The emphasis is on the functionality of the software rather than the data used by the functions. The procedural approach allows data to move freely from one function to another without any security. That is, the data to be shared among many functions is declared globally so that all the functions can easily access it. However, this makes the global data liable to change as any function can modify its value. In case the need arises to modify the format of the data, all the functions accessing the data need to be modified as well. This causes an extra overhead and makes the programs error-prone. The procedural approach does not model the real world very well as any real-world entity is characterized not only by the functions it performs but also the properties (or data) it possesses. However, in the procedural approach, the data and the associated functions are loosely related. A large program developed using this approach is difficult to maintain, debug and extend. The basic idea behind the procedural approach is shown in Figure 1.3. Global data Function 1 Local data Function 2 Local data Function n Local data Fig. 1.3 Accessing Global Data To overcome the limitations of the procedural approach, the object-oriented programming (OOP) paradigm was developed. This revolutionized the process of software development. It not only included the best features of structured programming, but also introduced some new and advanced features that procedural programming lacked. The most important feature is that unlike the procedural approach in which a program is divided into a number of functions, OOP divides the program into a number of objects. An object is a unit of structural and behavioural modularity that contains a set of properties (or data) as well as the associated functions. In addition, programmers can create relationships between one object and another. 4 Material

The functions of the object (also known as member functions) provide the only way to access the object s data. If the user wants to read or manipulate any data item, then it is possible only if the member function to do the same is available in the object. Therefore, data is hidden from the outside world and hence is safe from accidental modifications. The basic idea behind OOP is shown in Figure 1.4. Introduction and Features Object 1 Object 2 Data Functions communicating with other using functions Data Functions communicating Object 3 Data Functions communicating Fig. 1.4 Data and Functions in OOP Some of the other features of OOP are as follows: OOP emphasizes data rather than functions or procedures. OOP models the real world very well by binding the data and associated functions together under a single unit and thus prevents the free movement of data from one function to another. The data of one object can be accessed by the associated functions of that object only. Other functions are not allowed to access that data. In other words, data is hidden from the outside world. However, the functions of one object can access the functions of another object. The objects of the entire system can interact with each other by sending messages to each other. The programs written in OOP are easy to maintain and extend as new objects can be easily added to the existing system whenever required without modifying the other objects. OOP follows the bottom-up approach for designing programs. That is, first the objects are designed and then these objects are combined to form the entire program. These new OOP features have tremendously helped in the development of well-designed high-quality software. Material 5

Introduction and Features Check Your Progress 1. What was developed to overcome the limitations of the procedural approach? 2. What provides a way to access the object s data? 3. What do programs in the procedural approach consist of? 1.4 BASIC CONCEPTS OF OOP To understand the concept of object-oriented programming, it is necessary to know the fundamental terms and concepts of this approach. These include objects, classes, data abstraction, encapsulation, inheritance, polymorphism and message passing. 1.4.1 Objects Objects are the small, self-contained and modular units with a well-defined boundary. An object consists of a state and behaviour. The state of an object is one of the possible conditions that an object can exist in and is represented by its characteristics or attributes or data. The behaviour of an object determines how an object acts or behaves and is represented by the operations that it can perform. In OOP, the attributes of an object are represented by the variables and the operations are represented by the functions. For example, an object Biscuit may consist of the data product code P001, product name Britannia Biscuits, price 20 and quantity in hand 50. These data values specify the attributes or features of the object. Similarly, consider another object Maggi with product code P002, product name Maggi Noodles, price 10 and quantity in hand 20 (see Figure 1.5). In addition, the data in the object can be used by the functions such as check_qty() and display_product(). These functions specify the actions that can be performed on data. Objects are what actually runs in the computer and thus are the basic runtime entities in object-oriented systems. They are the building blocks of objectoriented programming. Although two or more objects can have the same attributes, they are separate and independent objects with their own identity. In other words, all the objects in a system take a separate space in the memory independent of each other. Note that the main objective of breaking down complex software projects into objects is that changes made to one part of the software should not adversely affect the other parts. 1.4.2 Classes A class is defined as a user-defined data type, which contains the entire set of similar data and the functions that an object possesses. In other words, a class in 6 Material

OOP represents a group of similar objects. As stated earlier, in the real world, millions of objects exist and each of them has its own identity. However, each of them can be categorized under different groups depending on the common properties they possess and the functions they perform. For example, cars, scooters, motorbikes, buses, etc. all can be grouped under the category vehicles. Similarly, dogs, cats, horses, etc. can be grouped under the category animals. Thus, vehicles and animals can be considered as classes. A class serves as a blueprint or template for its objects. That is, once a class has been defined, any number of objects belonging to that class can be created. The objects of a class are also known as the instances or the variables of that class and the process of creating objects from a class is known as instantiation. Note that a class does not represent an object; rather, it represents the data and functions that an object will have. For example, a class Product consists of data such as p_code, p_name, p_price and qty_in_hand, which specify the attributes or features of the objects of the Product class. In addition, it consists of functions such as display_product() and check_qty() that specify the actions that can be performed on data (see Figure 1.5). Introduction and Features Class: Product Data: p_code p_name p_price qty_in_hand Functions: display_product() check_qty() Object: Biscuits Data: P001 Britania biscuits 20 50 Object: Maggi Data: P002 Maggi Noodles 10 20 Fig. 1.5 Class and its Objects Note that the data belonging to a particular class is known as its data members and the functions of the class are known as the member functions and both collectively are known as the members of the class. Material 7

Introduction and Features 1.4.3 Data Abstraction Abstraction is a mechanism to hide irrelevant details and represent only the essential features so that one can focus on important things at a time. It allows managing complex systems by concentrating on the essential features only. For example, while driving a car, a driver only knows the essential features to drive a car such as how to use clutch, brake, accelerator, gears, steering, etc. and least bothers about the internal details of the car like motor, engine, wiring, etc. Abstraction can be of two types, namely, data abstraction and control abstraction. Data abstraction (also known as data hiding) means hiding the details about the data and control abstraction means hiding the implementation details. In the object-oriented approach, one can abstract both data and functions. However, generally, the classes in OOP are defined in such a way that the data is hidden from the outside world and the functions form the public interface. That is, the functions of the class can be directly accessed by other functions outside the class, and the hidden data can be accessed indirectly with the help of these functions. Note that the values of the hidden data members cannot be passed to the outside world unless the functions are written to pass that information outside the class. Since the internal details of the class are hidden from the outside world, data abstraction ensures security of data by preventing it from accidental changes or manipulations by other parts of the program. Note: Classes in the object-oriented programming are also known as Abstract Data Types (ADT) as they use the concept of abstraction. 1.4.4 Encapsulation Encapsulation is the technique of binding or keeping the data and functions (that operate on them) together in a single unit called class. Encapsulation is the way to implement data abstraction. A well-encapsulated object acts as a black box for other parts of the program. That is, it provides services to the external functions or other objects that interact with it. However, these external functions or the objects do not need to know its internal details. For example, in Figure 1.5 the data p_code, p_name, p_price and qty_in_hand and the functions display_product() and check_qty are encapsulated in a class Product. 1.4.5 Inheritance Inheritance can be defined as the process whereby an object of a class acquires characteristics from the object of another class. As stated earlier, all objects of similar type are grouped together to form a class. However, sometimes a situation arises when different objects cannot be combined together under a single group as they share only some common characteristics. In this situation, the classes are defined in such a way that the common features are combined to form a generalized class and the specific features are combined to form a specialized class. The 8 Material

specialized class is defined in such a way that in addition to the individual characteristics and functions, it also inherits all the properties and the functions of its generalized class. For example, in the real world, not all the vehicles can be automobiles; some of them are pulled-vehicles also. Thus, car and scooter both are vehicles that come under the category of automobiles. Similarly, rickshaw and bicycle are the vehicles that come under the category of pulled-vehicles. Thus, automobiles and pulled-vehicles inherit the common properties of the vehicle class and also have some other properties that are not common and differentiate them. Thus, the vehicles class is the generalization of automobiles and the pulled-vehicles class, and automobiles and pulled-vehicles classes are the specialized versions of the vehicles class. Note that while inheriting the vehicle class, the automobiles and pulled-vehicles do not modify the properties of the vehicle class; however, they can add new properties that are exclusive for them (see Figure 1.6). Introduction and Features Vehicles Automobiles Pulled-vehicles Car Scooter Rickshaw Bicycle Fig. 1.6 Inheritance In the same way, OOP allows one class to inherit the properties of another class or classes. The class, which is inherited by the other classes, is known as superclass or base class or parent class and the class, which inherits the properties of the base class, is called subclass or derived class or child class. The subclass can further be inherited to form other derived classes. For example, in Figure 1.6, car and scooter are the derived classes of automobiles and rickshaw and bicycle are the derived classes of pulled-vehicles. Inheritance can be of two types, single inheritance and multiple inheritance. If a class acquires properties from a single class, it is termed as single inheritance and if it acquires characteristics from two or more classes, it is known as multiple inheritance. The main advantage of inheritance is reusability. The existing classes can be simply reused in new software instead of writing a new code. Moreover, new features can be added without altering or modifying the features of the existing class. Reusability and extensibility Inheritance allows code reusability, that is, it facilitates classes to reuse the existing code. It is useful when several classes having similar features are to be created. In Material 9

Introduction and Features such a case, one class is created having common features of all the classes, which is used as the base class. Whenever a new class is to be generated, it inherits this base class and only the unique features of the new class are added, thereby avoiding repetition of code. The new class acquires the members of the old class that are already tested and debugged. The base classes having common features can also be stored in a reservoir so that they can be used by any programmer. These classes stored in the reservoir form part of general-purpose programming tools and new classes generated on the basis of these classes become their specialized versions. Hence, inheritance allows extending and reusing already existing classes, thereby saving time as well as increasing the reliability. For example, a common class employee can be created having some basic features, which can be used by any program requiring classes (like, clerk, manager, part_time_employee, full-time_employee, etc.) to be generated having similar features. These features of inheritance play an important role in the program development. Abstract classes and concrete classes While inheriting a base class, a derived class not only inherits the data and functions of its base class, but can also provide a different implementation (definition) for the functions of the base class. In such a case, the base class may or may not provide an implementation for its function. It only provides the interface for the functions. A class, which provides only the interface of one or more functions and not their implementations, is known as abstract class. An abstract class only specifies what the function does, what all it requires, etc., however, it does not specify how the function works. Implementations of such functions are provided in the classes that inherit the abstract class. Note that the instances (objects) of an abstract class cannot be created. This is because it does not provide the implementation of the functions. The class that provides an implementation for all its functions is known as concrete class. Concrete classes can have one or more objects. Note that derived classes that provide implementation of all the functions that have not been implemented in the abstract class are also considered as concrete classes. 1.4.6 Polymorphism and Overloading Polymorphism (a Greek word meaning having multiple forms) is the ability of an entity, such as a function or a message, to be processed in more than one form. It can also be defined as the property of an object belonging to the same or a different class to respond to the same message or function in a different way. For example, if a message change_gear is passed to all the vehicles, then the automobiles will respond to the message appropriately; however, the pulled-vehicles will not respond. The concept of polymorphism plays an important role in OOP as it allows an entity to be represented in various forms. 10 Material

In C++, polymorphism can be achieved either at compile-time or at runtime. At compile-time, polymorphism is implemented using operator overloading and function overloading. However, at run-time, it is implemented using virtual functions (see Figure 1.7). Polymorphism Introduction and Features Compile-time Run-time Operator Overloading Function Overloading Virtual Functions Fig. 1.7 Different Ways of implementing Polymorphism Operator overloading is the process that enables an operator to exhibit different behaviour, depending on the data provided. For example, when the + operator is used with two numbers, it adds the two numbers and produces the sum. However, if it is used with two strings, it concatenates the two strings and produces the third concatenated string (see Figure 1.8). Operator overloading is discussed in detail in Unit 08. Operator '+' 2 + 3 = 5 'MNO' + 'PQR' = 'MNOPQR' Fig. 1.8 Operator Overloading Similarly, a single function can behave differently depending on the type of data provided. For example, in Figure 1.9, the function Add can be used to add two integers and two floating-point numbers. This form of polymorphism is known as function overloading (discussed in Unit 08). Add(int a, int b)... Add(float a, float b)... Fig. 1.9 Function Overloading Note: Compile-time polymorphism is also known as static binding as the linking of function call to the actual code of the function is done at compiletime itself. Material 11

Introduction and Features Consider another example in which three different classes, square, rectangle and circle are derived from the base class geometrical_shapes. The function area() of the base class is implemented in different ways in all its derived classes and a call to a particular function is determined at run-time. This form of polymorphism is called run-time polymorphism (see Figure 1.10). geometrical_shape area() square area() implementation rectangle area() implementation circle area() implementation Fig. 1.10 Run-time Polymorphism 1.4.7 Message Passing Message passing is the process of interacting between different objects in a program. As discussed earlier, a program following the object-oriented paradigm comprises a set of objects each with a set of data and functions. When the program is executed, these objects interact or communicate with each other by sending and receiving messages. The messages are exchanged by calling the member functions of the classes. An object of a class that wants to communicate with the object of another class requests the object to invoke the required member function of its class. This function call is different from the normal function call as in this case, the sending object is sending a request for the execution of the function. However, the receiving object may or may not accept the request depending on whether the function forms the public interface or it is hidden from the outside world. Thus, this form of communication is called message sending and not an ordinary function call. For example, consider two classes Product and Order. The object of the Product class can communicate with the object of the Order class by sending a request for placing order (see Figure 1.11). 12 Material

order1.place_order(qty_in_hand); message passing Introduction and Features object_name function or message information (optional) Fig. 1.11 Message Passing 1.5 OBJECT ORIENTED PARADIGM The object-oriented programming paradigm came into use as it overcomes certain limitations of structured and unstructured programming paradigms. The new and advanced features of OOP, such as encapsulation, abstraction, inheritance and polymorphism, help in developing high-quality software. High-quality software can be developed due to its certain advantages. Some of the advantages of OOP are as follows: In OOP, writing programs with the help of objects is similar to working with real-world objects. That is, real-world objects can be conveniently represented in a program, which reduces its complexity and makes its structure clear. In object-oriented programs, each object is an independent and separate entity, which makes modifying, locating and fixing problems in a program an easy task. In addition, any changes made inside the class do not affect the other parts of a program. Thus, object-oriented programs are easy to write and easy to maintain. In object-oriented programming, data integrity and data security is high as it focuses on the data and its protection from manipulation by different parts of the program. As a result, object-oriented programs are less error-prone, more reliable and secure. Object-oriented programs are easy to extend as new features in a program can be added easily by introducing a few new objects without modifying the existing ones. Object-oriented programming allows reusability of code. That is, the objects created in one program can be reused in other programs. In addition, new classes can be created with the help of existing ones using inheritance. It leads to faster software development and high-quality programs. Object-oriented programs are easier to adapt and scale. A large system can be created by assembling reusable subsystems. Material 13

Introduction and Features Check Your Progress 4. How can a large system be created? 5. What is message passing? 6. What does an object of a class that wants to communicate with the object of another class do? 1.6 ANSWERS TO CHECK YOUR PROGRESS QUESTIONS 1. To overcome the limitations of the procedural approach, the object-oriented programming (OOP) paradigm was developed. 2. The functions of the object (also known as member functions) provide the only way to access the object s data. 3. Programs in the procedural approach consist of a controlling procedure. 4. A large system can be created by assembling reusable subsystems. 5. Message passing is the process of interacting between different objects in a program. 6. An object of a class that wants to communicate with the object of another class requests the object to invoke the required member function of its class. 1.7 SUMMARY 14 Material Structured programming (also known as procedural programming) was a powerful and an easy approach of writing complex programs. In procedural programming, programs are divided into different procedures (also known as functions, routines or subroutines) and each procedure contains a set of instructions that performs a specific task. Programs in the procedural approach consist of a controlling procedure known as the main, which controls the execution of other procedures. After executing all the instructions, the program control returns to the procedure from where the call is made. To overcome the limitations of the procedural approach, the object-oriented programming (OOP) paradigm was developed. It not only included the best features of structured programming, but also introduced some new and advanced features that procedural programming lacked.

The functions of the object (also known as member functions) provide the only way to access the object s data. To understand the concept of object-oriented programming, it is necessary to know the fundamental terms and concepts of this approach. Objects are the small, self-contained and modular units with a well-defined boundary. A class is defined as a user-defined data type, which contains the entire set of similar data and the functions that an object possesses. Abstraction is a mechanism to hide irrelevant details and represent only the essential features so that one can focus on important things at a time. Abstraction can be of two types, namely, data abstraction and control abstraction. Encapsulation is the technique of binding or keeping the data and functions (that operate on them) together in a single unit called class. Inheritance can be defined as the process whereby an object of a class acquires characteristics from the object of another class. The base classes having common features can also be stored in a reservoir so that they can be used by any programmer. While inheriting a base class, a derived class not only inherits the data and functions of its base class, but can also provide a different implementation (definition) for the functions of the base class. An object of a class that wants to communicate with the object of another class requests the object to invoke the required member function of its class. The object-oriented programming paradigm came into use as it overcomes certain limitations of structured and unstructured programming paradigms. Inheritance allows code reusability, that is, it facilitates classes to reuse the existing code. Introduction and Features 1.8 KEY WORDS Instantiation: The process of creating objects from a class. Abstraction: A mechanism to hide irrelevant details and represent only the essential features so that one can focus on important things. Data Abstraction: Hiding the details about the data. Encapsulation: The technique of binding or keeping the data and functions (that operate on them) together in a single unit. Inheritance: The process whereby an object of a class acquires characteristics from the object of another class. Material 15

Introduction and Features Polymorphism: The ability of an entity, such as a function or a message, to be processed in more than one form. Operator Overloading: The process that enables an operator to exhibit different behaviour, depending on the data provided. Message Passing: The process of interacting between different objects in a program. 1.9 SELF ASSESSMENT QUESTIONS AND EXERCISES Short-Answer Questions 1. Write a short note on the evolution of various programming paradigms. 2. List some of the limitations of the procedural programming paradigm. 3. Differentiate between a class and an object. 4. Write a short note on message passing. 5. List some advantages of object-oriented programming. 6. Differentiate between compile-time and run-time polymorphism. Long-Answer Questions 1. What are the differences between object-oriented programming paradigm and procedural programming paradigm? 2. What do you understand by function overloading? Give an example. 3. Define the following terms: a. Concrete class b. Derived class c. Abstract class d. Parent class 4. Discuss the various OOP concepts in detail. 1.10 FURTHER READINGS Blaha, Michael R. and James R. Rumbaugh. 2004. Object-Oriented Modeling and Design with UML, 2nd Edition, New Jersey: Prentice-Hall. Balagurusamy, E. 2008. Object Oriented Programming with C++, 4th Edition. New Delhi: Tata McGraw-Hill. 16 Material

Kanetkar, Yashavant. 2002. Let Us C++. New Delhi: BPB Publications. Shukla, Rajesh K. 2008. Object-Oriented Programming in C++. New Delhi: Wiley India Pvt. Ltd. Venugopal, K.R., Rajkumar and T. Ravishankar. 1999. Mastering C++. New Delhi: Tata McGraw-Hill. Hubbard, John R. 2000. Schaum s Outline of Programming with C++. New York: McGraw-Hill. Introduction and Features Material 17

Popular OOP Languages UNIT 2 Structure POPULAR OOP LANGUAGES 2.0 Introduction 2.1 Objectives 2.2 Moving from C to C++: Introduction 2.3 Basic Concept of Streams 2.4 Answers to Check Your Progress Questions 2.5 Summary 2.6 Key Words 2.7 Self Assessment Questions and Exercises 2.8 Further Readings 2.0 INTRODUCTION Object-Oriented Programming (OOP) is the term used to describe a programming approach based on objects and classes. The object-oriented paradigm allows us to organize software as a collection of objects that consist of both data and behavior. This is in contrast to conventional functional programming practice that only loosely connects data and behavior. However, the definition of an OOP language is not exactly the same as that of a programming language with object-oriented features. Example, C++ is considered to be a multi-paradigm language that includes OOP. It however is less object-oriented as compared to a few other languages such as Python and Ruby. In this unit you will learn about popular OOP languages. 2.1 OBJECTIVES After going through this unit, you will be able to: Understand how to move from C to C++ Discuss the basic concept of streams Analyze the hierarchy of console stream classes 2.2 MOVING FROM C TO C++: INTRODUCTION C is a procedure-based language. Once you write a program in C, you must run it through a C compiler to turn your program into one that a computer can run (execute). C allows the input and output control in which a user can input the value to get the desired result. This language makes hardware devices easy to access 18 Material

and is used to manipulate individual bits in hardware registers. C++ is an objectoriented language that uses various concepts in handling of programs, such as virtual functions, multiple inheritance, exception handling, and polymorphism. Its object-oriented structure allows the code that can be reused and hence it cuts down the development time. C++ provides comprehensive coverage of abstract classes as interfaces, regular error handling, standard strings, I/O streams, etc. Table 2.1 explains the difference between C and C++ computer languages: Popular OOP Languages C C is procedure and function-oriented language and gives importance to procedure (functions) rather than data. C provides scanf() function to input the values and printf() function to display the result. C does not follow the class and object concept. Table 2.1 Differences between C and C++ C++ C++ is object-oriented language and gives importance to data. C++ provides cin object of class istream to input the values and cout object to display the result. C++ supports object and class for data encapsulation, data abstraction and polymorphism concept. In C, macros are used. C supports pointers that basically refer to record and track the memory address or location of function. It is used especially in game programming and faster than C++. It is structured language and use extensively pointers for memory, array, structures and functions. It has no runtime checking and is case sensitive language. C does not support new and delete keywords. The memory operations to free or allocate memory in C are carried out by malloc() and free()functions. In C, malloc() function is used to allocate the memory. You can use this function to allocate memory in the following way: int *x = malloc( sizeof(int) ); int *x_array = malloc( sizeof(int) * 10 ); Following code is used to release the memory with the help of free() function: free( x ); free( x_array ); In C++, an inline function is used instead of macros. This language supports GUI programming feature on a computer. It is well-suited to platform dependent applications. It is complex for very large high level programs and difficult to debug the web applications. It is also case sensitive language. C++ supports new and delete keywords for memory management. In C++, memory allocation for arrays is different for single objects. Following code is used to new keyword: int *x = new int; int *x_array = new int[10]; Following code is used to use delete keyword: delete x; delete[] x; Material 19