Mastering C 2nd Edition Venugopal pdf download
Mastering C 2nd Edition Venugopal pdf download
https://ebookfinal.com/download/mastering-c-2nd-edition-
venugopal/
https://ebookfinal.com/download/mastering-c-database-programming-1st-
edition-jason-price/
https://ebookfinal.com/download/mastering-windows-sharepoint-
services-3-1-c-a-callahan/
https://ebookfinal.com/download/mastering-leadership-2nd-edition-
michael-williams/
https://ebookfinal.com/download/mastering-linux-2nd-edition-arman-
danesh/
Mastering Metasploit 2nd Edition Nipun Jaswal
https://ebookfinal.com/download/mastering-metasploit-2nd-edition-
nipun-jaswal/
https://ebookfinal.com/download/mastering-blender-2nd-edition-edition-
tony-mullen/
https://ebookfinal.com/download/ceramics-mastering-the-craft-2nd-
edition-richard-zakin/
https://ebookfinal.com/download/xslt-mastering-xml-
transformations-2nd-edition-doug-tidwell/
https://ebookfinal.com/download/mastering-regular-expressions-2nd-
edition-edition-jeffrey-e-f-friedl/
Mastering C 2nd Edition Venugopal Digital Instant
Download
Author(s): Venugopal
ISBN(s): 9781259029943, 1259029948
Edition: 2
File Details: PDF, 8.10 MB
Year: 2013
Language: english
Mastering C++
Mastering C++
K R VENUGOPAL
University Visvesvaraya College of Engineering
Bangalore University, Bangalore
India
RAJKUMAR BUYYA
The University of Melbourne and Manjrasoft Pvt. Ltd.,
Melbourne, Australia
Mastering C++, 2e
Copyright © 2013, 1999, by McGraw Hill Education (India) Private Limited.
No part of this publication can be reproduced or distributed in any form or by any means, electronic, mechanical,
photocopying, recording, or otherwise or stored in a database or retrieval system without the prior written
permission of the publishers. The program listings (if any) may be entered, stored and executed in a computer
system, but they may not be reproduced for publication.
Information contained in this work has been obtained by McGraw Hill Education (India), from sources
believed to be reliable. However, neither McGraw Hill Education (India) nor its authors guarantee the accuracy
or completeness of any information published herein, and neither McGraw Hill Education (India) nor its
authors shall be responsible for any errors, omissions, or damages arising out of use of this information. This
work is published with the understanding that McGraw Hill Education (India) and its authors are supplying
information but are not attempting to render engineering or other professional services. If such services are
required, the assistance of an appropriate professional should be sought.
Typeset at Print-O-World, 2579, Mandir Lane, Shadipur, New Delhi 110 008, and printed at
Cover Printer :
Dedicated
to
Tejaswi Venugopal
Contents
Foreword xvii
Preface xix
Acknowledgements xxiii
1. Object-Oriented Paradigm 1
1.1 Why New Programming Paradigms ? 1
1.2 OOPs ! a New Paradigm 2
1.3 Evolution of Programming Paradigms 5
1.4 Structured Versus Object-Oriented Development 8
1.5 Elements of Object-Oriented Programming 10
1.6 Objects 11
1.7 Classes 12
1.8 Multiple Views of the Same Object 13
1.9 Encapsulation and Data Abstraction 15
1.10 Inheritance 15
1.11 Delegation—Object Composition 17
1.12 Polymorphism 17
1.13 Message Communication 18
1.14 Popular OOP Languages 19
1.15 Merits and Demerits of OO Methodology 23
1.16 OO Learning Curve 24
1.17 Software Reuse 25
1.18 Objects Hold the Key 28
Review Questions 29
3. C++ at a Glance 76
3.1 Introduction 76
3.2 Data Encapsulation and Abstraction—Classes 77
3.3 Inheritance–Derived Classes 81
3.4 Polymorphism—Operator Overloading 85
3.5 friend Functions 88
3.6 Polymorphism—Virtual Functions 91
3.7 Generic Classes—Class Templates 94
3.8 Exception Handling 96
3.9 Streams Computation 99
Solved Problem 103
Review Questions 104
Index 878
Foreword
Object-oriented programming languages are playing an increasingly important role in computing sci-
ence and its applications. With the declining hardware costs, the cost of computing systems is now being
largely dominated by software. As new methodologies are being developed, some old ones are slowly
turning obsolete by the end of the day. Sometimes this results in considerable increase in development
cost plus time overrun and affects the quality of products. Object-oriented analysis and design is an
upcoming technology that software professionals have employed successfully in the development of
large software projects.
Programming, as every practitioner knows, is a delicate art, where the main problem is not only to
obtain a working program (which is mandatory of course), but to have a program designed in such a
way that it is not fragile, i.e., it can be modified/updated/debugged easily. In order to attain these goals,
programmers need tools.
Among the tools that allow a programmer to express ideas are, of course, the programming lan-
guages. One such programming language used popularly these days is the C++ language. This book by
K R Venugopal and Rajkumar Buyya is a timely and relevant publication.
This book is unique in many ways. The concepts such as programming paradigms, the need for
OOP technology, extending C, C++ at a glance, fundamental constructs of the C++ language, classes
and objects, inheritance, polymorphism, generic programming, stream computations, fault-tolerant
programming with exceptions are covered prominently. Every aspect is prominently illustrated with
figures and examples that are well tested, illustrative and have impressively designed solutions. The
authors, with their rich industrial and academic experience in Computer Science, have made their best
effort to bring out this book for the benefit of students, teachers, and software professionals.
This book, besides being illustrative, includes a wide array of typical programs, which help OOP
aspirants to grasp the fundamentals of the subject without external assistance. I am confident that this
book will serve the needs of all those who are serious about object-oriented technology.
In this book, the approach followed by the authors make the exploration of the OOP territory as easy
and interesting as possible, starting slowly and working up gradually to more challenging concepts. I
am positive that the reader will find the book an appealing vehicle for embarking into the challenging
world of Mastering C++. Good luck!
S Sasi Kumar
Director
Centre for Development of Advanced Computing
(A Scientific Society of Government of India)
Brunton Road, Bangalore 560 025
Karnataka, India
Preface
In the real world, everything (including you and me!) exist in the form of objects. These objects are
identified by the system analyst upon request of a customer (who actually uses services of objects)
and handed over to the designer. The designer in turn creates classes. which group all those objects
exhibiting similar characteristics and behaviors into a single unit. These units are then passed to
programmers, who implement the object’s framework given by the designer. Thus, objects move from
the customer to the programmer.
Programmers create objects using its framework. These objects work in a collaborative and cooperative
manner to produce the required output. These software objects now start moving from programmers
to test engineers, and finally to the customer, who is the actual user of these objects, to solve real-
world problems. To realize this effective migration of objects from one person to another, there must
be an effective means of communication among all those involved in the development of a software
project. They need to communicate their ideas in terms of objects. That is, the system analyst delivers
requirement specification in terms of objects, and the software designer delivers design specification
in terms of classes (object groups). And even programmers need to express their ideas or write code in
terms of objects. Hence, the demand for an object-oriented requirement specification (OORS), object-
oriented analysis (OOA), object-oriented design (OOD), and object-oriented programming (OOP) has
grown tremendously.
Currently, there is no standard method of OORS, OOA, and OOD available. But, there are many
standard programming languages available that support OOP, and one of the most popular OOP
languages is C++.
C++ is an object-oriented language that a C programmer can appreciate, especially who is an early-
age assembly-language programmer. C++ was first oriented towards execution performance and then
towards flexibility. Most of the features which C++ adds to C involve no runtime overhead; few that do
can be avoided by efficiency-conscious programmers.
Yet, C being a structured programming language, offered the ease of software development but
failed to support maintenance of large code. This has motivated the search for a new language as
efficient as C but simplifying the maintenance of large code. It is not enough to offer a language that
is just as good as C. If people are to switch, the replacement language must not only equal C in terms
of efficiency and code reuse, but it must also be a lot better in terms of productivity, maintenance, and
power. C++ meets these criteria, making it the first serious contender to challenge Fortran’s supremacy.
The last couple of years have seen a growing wave of enthusiasm for object-oriented approaches
to requirements analysis, application design, and programming. The same period has been marked by
the increasing popularity of the C++ language and its acceptance as a logical successor to C. Since
C++ is designed to support object-oriented development, it seems only natural to see a strong link
between C++ and OOP. Programmers who move to C++ will apparently adopt an object-oriented style
of programming.
With C++, it is much easier to build and maintain really big code. This is made possible with C++’s
enhancements to C and more importantly, its object-oriented support. Some of the most prominent
concepts of object-oriented programming are encapsulation, data abstraction, inheritance, delegation,
polymorphism, and streams. All these features are covered in this book with illustrative programs.
xx Preface
A few other reasons for the success of C++(unlike other OOP languages) are
A strong backing from world-class software organizations (such as AT&T, Borland, Microsoft,
Sun Microsystems Inc, etc.)
Maturity of language
Availability of programming environment (language-sensitive editors, compilers, tools, profilers,
code analyzers, etc.)
Availability on machines—from microcomputers to supercomputers
Salient Features
Explanation of Object Oriented Concepts with the help of well-designed programming
examples
In-depth coverage of topics like Data Type, Operators and Expressions, Classes and Objects,
Object Initialization and Cleanup, Operator Overloading, Inheritance, Generic Programming
with Templates, and Exception Handling
Clear explanation of the language constructs using syntax, illustrations, code segments and
simple examples
Dedicated chapter on Object Oriented Analysis, and Design and Development
Newly introduced Case Studies
Organization of the Book
This book discusses the C++ language and object-oriented concepts over twenty chapters. Each
chapter explains C++ constructs needed for object-oriented programming with numerous programming
solutions. The book is organized as follows:
Chapter 1 (Object-Oriented Paradigm) discusses the need for new programming paradigms and
various aspects of object-oriented programming. It covers the evolution of programming paradigms,
elements of OOP, popular OOP languages, OO learning curve, software reuse, and demonstrates how
objects hold the key in driving future technologies.
Chapter 2 (Moving from C to C++) starts with the Hello World program demonstrating various
elements of a C++ program. It also presents new features added to C++ (apart from OOP) such as
streams-based I/O, scope-resolution operator, inline functions, function overloading, enhancements to
C structures, function templates, and new and delete operators for runtime memory management.
Chapter 3 (C+ + at a Glance) illustrates the various features supported by C++ for object-oriented
programming. Both chapters include illustrative examples of complete programs, rather than isolated
fragments. It discusses classes, objects, derived classes, operator overloading, virtual functions, class
templates, exceptions handling, and streams.
Chapters 4 through 9 discuss various fundamental elements of C and C++. These chapters are
devised keeping in mind the readers who are not familiar to the C language. The readers with C back-
ground will also benefit from these chapters, since emphasis is placed on their (data types, functions,
pointers, etc.) availability in C++ in a powerful form. Chapter 4 deals with basic data types, operators,
and expressions. Chapter 5 explains control flow: if, if-else, switch, for, while,
break, etc. Chapter 6 covers Arrays and Strings. Chapter 7 describes modular programming with
functions. It presents techniques of managing large software system development by breaking it into
multiple functions and modules. Chapter 8 emphasizes on structures and unions. Chapter 9 deals
with runtime memory management using pointers, emphasizing new features of C++ for dynamic
memory management.
Discovering Diverse Content Through
Random Scribd Documents
travel is along the river. Throughout the province almost the only
highways are footpaths across the jungles. From Hanoi roads lead
north to China and south to Hué. The influence of Hanoi, through
Anam, is widespread as a centre of fashion as well as of authority. A
French writer calls it the “Paris of the Anamese empire.” What more
could he say?
SIGHT-SEEING IN BANGKOK.
T give you some idea of Bangkok, the capital city of Siam, I will
o
imagine myself once more a resident there, with you for a visitor,
and will invite you this fine morning to take a seat in our family boat,
which is at the landing, and we will go out upon the river.
It is a strange city, unlike any in the Western World, and if we
cannot “see the lions,” we may perhaps “see the elephants” and
many novel and interesting things. You have already become
somewhat familiar with the copper-colored complexion, the black
eyes, black hair and black teeth, the scanty clothing and shaven
heads of the people.
We will confine our excursion to-day to the Menam River, the
Broadway of Bangkok, while the hundreds of canals that intersect it
at every angle may be considered the less-important streets. You find
the river a busy scene, but need have no fears of a collision with any
of the innumerable boats of every size and description that pass, for
the Siamese are very skillful boatmen.
Your attention is already attracted by the beautiful wat, or temple,
with its surroundings, on our right. Is it not a beautiful spot, so
prettily laid out with fine shade trees, flowering plants and well-
swept walks? There are about two hundred wats in Bangkok
consecrated to the worship of Buddha. Some of them have groves
several acres in extent, containing pagodas, image-houses, priests’
dwellings and salas, or lounging-places. They occupy the pleasantest
parts of the city, and the deluded people spend vast sums on these
temples and their idols, expecting in this way to make merit for
themselves. You will not wonder that they are anxious to make all
the merit they can when their religion teaches them that at death
their soul enters the body of some animal—a bird, it may be, or a
snake, an elephant or a buffalo—unless they have made enough
merit to be born something better and higher.
But we must return to our boat and move on up the river, for I
hope to have time to visit the royal temple and perhaps some others.
Ah! there are some priests. I feared we should not meet any of
these yellow-robed gentry. How strange they look with shaven heads
and eyebrows! Such as these are the religious teachers of the
country. A few years ago there were ten thousand in Bangkok alone
and some thirty thousand in the kingdom—a perfect army (with few
exceptions) of self-conceited idlers; but I am happy to tell you that
their number has now greatly diminished. They live on the charity of
the people, going about every morning from house to house among
their parishioners, with their alms-bowl, and with a fruit-bag slung
over one shoulder. The old mother or grandmother is up at an early
hour to have rice cooked and ready for them. She puts a ladleful of
hot rice into the bowl of each as he passes, and a handful of fruit
into his fruit-bag. Do they thank her? By no means. She ought rather
to thank them, for they have given her an opportunity to make merit.
They collect sufficient for their morning and noonday meal. Their
religion forbids them to take food after midday.
Notice the boats. Some, used for trading, are loaded with rice,
sugar, salt, cotton, oil, dried fish, or dye-woods, as the case may be.
Some are at once boat, shop and dwelling. In the distance is a
nobleman’s boat, propelled by a dozen or two paddlers. What an odd
little house in the centre! Do you see how much at his ease His
Lordship is reclining, with two or three attendants down on their
elbows and knees before him? Look yonder at that small boat
paddled by a little child five or six years old. How unconcerned the
little fellow seems as he moves about entirely alone, his boat hardly
larger than himself, the edge scarcely two inches above the water!
Men, women and children in this country can swim; should this child
upset he would look out for himself and think very little of the matter.
Many smaller craft are market-boats, with fruits and vegetables
for sale. Notice some of the fruits as they pass. That one nearly as
large as a child’s head and resembling a huge orange is the shaddock
or pomelow. This large one, which smells so very disagreeably, and
which is so completely encased in spines as if to say, “Touch me not!”
is the far-famed durian, which the natives consider the king of fruits.
It weighs from five to ten pounds. This small round mahogany-
colored fruit is the delicious mangosteen—that golden one, the
luscious mango. Then there are the rich custard-apple, the refreshing
orange, the blushing rhambutan, the pineapple, the banana, etc.
You see the flags of many different nations flying from the ships,
of which none are more beautiful than the “star-spangled banner” of
our native land. There are also scores of steam-yachts on the rivers
of Siam now, owned by the natives, but when I first came here there
was not one to be seen. You ask what these strange-looking craft,
moored by immense ratan cables, are? They are Chinese junks, and
it would be hard to tell where the Chinese obtained their model. The
wonder is that such clumsy, unshapely, unsightly things can be made
to traverse the sea. And the glowing colors in which they are
painted, red always predominating! And don’t overlook the large eye
painted on each bow. The Chinese say, “No got eye, how can see?”
But you must not get so much interested in the boats and the
fruits as not to notice the homes of this people. Many of the princes
and nobles now have fine houses handsomely furnished. The
missionaries, foreign consuls, merchants and wealthy Chinese have
good, substantial dwellings. The homes of the common people, you
see, are small, of one story, and thatched with the leaves of the
attap palm. Most of them are neither painted nor whitewashed.
Those upon the land are placed on posts six feet high, and the sides
of many of them are made of bamboos split and woven together,
forming a kind of basket-work.
But thousands of the people live in floating houses, which you
have observed lining both banks of the river. Notice them particularly
now, for they are one of the peculiarities of this Eastern city. They
are but one story high, you see, and built of boards and placed on
rafts of large bamboos, which rise and fall with the tide, and hence
are called floating houses. These rafts must be renewed every two or
three years. The houses are kept in their place by large posts on
each side driven deep into the muddy bed of the river. They do
sometimes, however, get detached from their moorings, though
fastened to them by rings of ratan, and float up or down the river
with the tide. These houses have some advantages over all others,
for if neighbors are disagreeable or a fire breaks out the occupants
have only to move off with the tide, house, furniture and all, to some
other spot.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookfinal.com