Programming With C 3rd Edition Ravichandran instant download
Programming With C 3rd Edition Ravichandran instant download
download
https://ebookname.com/product/programming-with-c-3rd-edition-
ravichandran/
https://ebookname.com/product/programming-problem-solving-
abstraction-with-c-moffat/
https://ebookname.com/product/windows-forms-programming-
with-c-1st-edition-erik-brown/
https://ebookname.com/product/object-oriented-programming-
with-c-4th-edition-e-balagurusamy/
https://ebookname.com/product/storytown-winning-catch-
grade-4-reader-1st-edition-harcourt-school-publishers/
The State of Southern Illinois An Illustrated History
1st Edition Herbert K. Russell
https://ebookname.com/product/the-state-of-southern-illinois-an-
illustrated-history-1st-edition-herbert-k-russell/
https://ebookname.com/product/latin-american-novels-of-the-
conquest-reinventing-the-new-world-1st-edition-kimberle-s-lopez/
https://ebookname.com/product/surface-and-interface-science-
volume-3-and-4-volume-3-properties-of-composite-surfaces-
volume-4-solid-solid-interfaces-and-thin-films-1st-edition-klaus-
wandelt/
https://ebookname.com/product/pro-html5-and-css3-design-
patterns-1st-edition-michael-bowers/
https://ebookname.com/product/scanner-data-and-price-indexes-
robert-c-feenstra/
Introduction to 3G Mobile Communications 2nd ed Edition
Juha Korhonen
https://ebookname.com/product/introduction-to-3g-mobile-
communications-2nd-ed-edition-juha-korhonen/
Programming with
C++
Third Edition
About the Author
D Ravichandran is currently based in Hyderabad and is a corporate trainer in software engineering, data
structures and algorithms, and programming languages. He was earlier a senior faculty in the Department
of Computing, Middle East College of Information Technology, Muscat, Sultanate of Oman. He was also
a faculty member of Department of Computer Science and Engineering, Pondicherry Engineering College,
Pondicherry, for more than 15 years. He is an expert in several computer programming languages and has
more than two decades of professional programming experience. A prolific writer, he has already published
many books in the field of computer science and information technology. His affiliations include a life
membership of the Indian Society for Technical Education and a membership of the Computer Society of
India.
Programming with
C++
Third Edition
D Ravichandran
Corporate Trainer in Software Engineering
Data Structures and Algorithms and Programming Languages
Hyderabad
Copyright © 2011, 2003, 1996, by Tata McGraw Hill Education Private Limited.
No part of this publication may 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 listing
(if any) may be entered, stored and executed in a computer system, but they may not be
reproduced for publication.
Vice President and Managing Director—McGraw-Hill Education: Asia Pacific Region: Ajay Shukla
Information contained in this work has been obtained by Tata McGraw-Hill, from sources believed to be reliable.
However, neither Tata McGraw-Hill nor its authors guarantee the accuracy or completeness of any information
published herein, and neither Tata McGraw-Hill 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 Tata
McGraw-Hill 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 Bukprint India, B-180A, Guru Nanak Pura, Laxmi Nagar-110 092 and printed at
Avon Printers, Plot No. 16, Main Loni Road, Jawahar Nagar, Industrial Area, Shahdara, Delhi 110 094
RQXCRRQZDLCZL
Dedicated to my son
Suseekaran
for his love and support
Contents
7. Arrays 248
7.1 Introduction 248
7.2 Array Notation 249
7.3 Array Declaration 249
7.4 Array Initialisation 250
7.5 Processing with Arrays 252
7.6 Arrays and Functions 259
7.7 Multidimensional Arrays 266
7.8 Character Array 276
Review Questions 285
Concept Review Problems 286
Programming Exercises 291
Appendix 820
Bibliography 836
Index 838
Preface to the Third Edition
The book not only discusses the issues concerning the mystery of ANSI C++ but also makes a conscious
effort to relate those insights to contemporary programming. This timeless and enlightening information
is presented in a clear and concise manner. The new edition offers a fresh perspective of what ANSI C++
means and where ANSI C++ fits into the scheme of software life cycles. Thus, readers can gain requisite
expertise by acquiring ANSI C++ programming skills and design ideas.
Users
The target audience for this book is two-fold—(i) computer novices who do not have any prior programming
knowledge, and (ii) experienced C++ developers who seek a guide for enhancing their design and
programming proficiency. Specifically, it can be used by undergraduate students of CSE, IT, ECE, EEE,
Electronics and Instrumentation Engineering, BCA/MCA, and BSc/MSc (Computer Science/IT). Moreover, it
would be an ideal reference for students of diploma and DOEACC courses in computer science and computer
training institutes.
Salient Features
The revised edition has been thoroughly updated with ANSI/ISO C++ syntax. This text offers one of
the best reviews of ANSI C++ since it gives access to the most important concepts in object-oriented
programming found anywhere. It introduces the syntax and features of C++ programming languages in
a simple manner. The concepts are very well exemplified with program codes containing the inputs and
outputs of the sample programs. It first explains the basic concepts (like functions, arrays, pointers and
structures) and then progresses with the discussion on OOP concepts (like classes, objects, inheritance,
polymorphism and templates) which will be helpful for the beginners in better understanding of the
implementation and applications of the C++ language.
The book is impregnated with the following salient features:
∑ Offers a concise introduction to C++ and Object-Oriented Programming (OOP).
∑ Emphasises the use of software tools and covers the software engineering topics in detail.
∑ Provides pictorial representation in the form of syntax diagrams, flowcharts and Object Modeling
Technique (OMT) class notation diagrams given.
∑ Elucidates the language features through executable codes which are tested on various compilers such
as Linux GNU C++ and .Net Microsoft Visual C++.
∑ Facilitates the readers with simple and easy-to-understand format of the program execution (i.e.,
sample input and output).
∑ Explains how to avoid and correct typical errors.
∑ Describes concept review problems to test programming proficiency of readers on various ANSI C++
topics in a special section. Interactive exercises using the computer make learning fun.
∑ Refreshed and enhanced pedagogy includes Programming Examples (359), Review Questions
(439), Concept Review Problems (380) and Programming Exercises (197). Answers to the
Concept Review Problems are included in the Appendix.
The pedagogical features and their benefits are explained below:
Organisation
This book consists of nineteen chapters which are as follows:
Chapter 1 presents the concepts and features of Object-Oriented Programming (OOP) and highlights
some of the key terms of the OOP paradigm which are extensively used in this book.
Chapter 2 gives an overview of the latest addenda to ANSI/ISO C++ compiler and also suggests how
to build an ANSI C++ program under various platforms, namely, GNU C++ for Linux and .Net VC++ for
Windows.
Chapter 3 introduces the fundamentals of C++ programming language and summarises the most
significant data types, operators and expressions used in ANSI C++.
Chapter 4 focuses on developing simple C++ programs with emphasis on the Input and Output Streams
<iostream> and highlights the features of manipulator functions and Input and Output (I/O) stream flags.
Chapter 5 describes the principles and guidelines in the design and evolution of C++ through control
statements which has become the standard for any programming language.
Chapter 6 deals with user-defined functions and program structures and stresses on how to define
and use the different types of arguments (namely, actual, formal, local and global variables); how to use the
recursive functions, nested functions and preprocessors.
xviii Preface to the Third Edition
Chapter 7 explains the importance of array data types in C++. It describes how to define, declare and
use single dimensional, multidimensional and character arrays. Array notation, array initialisation and types
of data storage such as static, automatic, and free store are also dealt with numerous examples.
Chapter 8 delves on the syntax and semantics of pointer data type which is one of the strengths of the
C++ language. In addition, it demonstrates the use of strings and advanced memory management techniques
using complex pointer data types and also guides the user how to avoid common pointer related errors.
Chapter 9 deals with functional characteristics of structure and union data types. It also describes how
to declare, define and use the array of structure, structure within structure, pointer to structure, union tags
and bit fields.
Chapter 10 elucidates the salient features of object-oriented programming and explains how classes and
objects can be defined, declared and used in C++. Special attention is given for defining the various types
of class declarations.
Chapter 11 covers the syntax and semantics of the special member functions such as constructors,
destructors, inline member functions, static class members and friend functions as well as their role in
class design. It also demonstrates several techniques and guidelines for an effective usage of these special
member functions.
Chapter 12 discusses one of the most important features of the OOP, namely, inheritance. Single and
multiple inheritance, types of derivation, public inheritance, private inheritance, protected inheritance,
container classes and member access control are explained with suitable number of examples.
Chapter 13 exemplifies the concepts of function and operator overloading, and explores the benefits
as well as the potential problems of operator overloading. It discusses the restrictions that apply to operator
overloading and also explains how to avoid the common errors while using operator and function overloading.
Chapter 14 narrates the central attraction of the OOP—polymorphism with pointers and virtual
functions. Early binding, virtual functions, late binding, pure virtual functions, abstract base classes,
constructors under inheritance, destructors under inheritance, virtual destructors and virtual base classes are
presented, with well-graded examples.
Chapter 15 presents the various aspects of designing and implementing templates, including class
templates, function templates, and template issues that are of special concern. This chapter describes the
standard exception handling using the keywords—try, catch and throw. It also elucidates the rationale
behind the addition of namespaces to the language and the problems that namespaces solve. Furthermore,
how to declare, define and use the namespace alias, nested namespace, unnamed namespace and namespace
std, are covered in this chapter.
Chapter 16 gives the data file operations in C++ and focuses on how to read and write a class of
objects from the files of secondary storage devices. The ANSI-ISO C++ streams and file processing
commands are dealt with suitable illustrations.
Chapters 17–19 provide coverage on introduction to the Standard Template Library (STL) and
generic programming in general. It discusses the principles of generic programming, focusing on STL
as an exemplary framework of generic programming. These chapters also demonstrate the use of STL
components such as containers, algorithms, iterators, allocators, adapters, binders, and function objects.
Acknowledgements
I am grateful to Dr T Sundararajan, Professor, Department of Civil Engineering, Pondicherry Engineering
College, for his timely support, encouragement, valuable comments, suggestions and many innovative ideas
in carrying out this project. I am indebted to my teachers, mentors and professors who taught me the art of
computer programming during my studentship at Indian Institute of Technology, Kharagpur, especially, to
Prof. Swapna Banerjee, Prof. N B Chakaraborthy and Prof. J C Biswas.
I extend my appreciation to Mr Christian Wolff, Heidelberg, Germany, for his continuous motivation, love
and advice in my life. I would like to express my gratitude towards Mr Arun, Mr Walid, Mr Shariq Ali and
Dr. Gulam Ahmed, Middle East College of Information Technology, Muscat, Sultanate of Oman, for their
technical comments and suggestions. I am thankful to my students [in India and abroad] who have helped
me a lot in bringing out this edition and would like to specially acknowledge the efforts of Mr Al Walid
Al Busaidi, Muscat; Mr Ashwin Kumar Chummun, UK; Mr Gowathaman, France; Mr Sampath Reddy,
US; Mr Sudheer Reddy, US; Mr Tushar Ranjan Sahoo and Dr Ram Niranjan Sahoo JIPMER, Pondicherry.
My earnest thanks are also due to the editorial and publishing professionals at Tata McGraw-Hill for
their keen interest and support in bringing out this book in record time. There have been several professors
who have participated in the review process of this book. I would like to sincerely acknowledge them for
their valuable suggestions and encouragement.
Akshay Girdhar
Guru Nanak Dev Engineering College, Ludhiana, Punjab
Amit Jain
Bharat Institute of Technology, Meerut, Uttar Pradesh
Harish Kumar
Panjab University, Chandigarh, Punjab
Prashant Sharma
Anand Engineering College, Agra, Uttar Pradesh
Dinesh Kumar Tyagi
Birla Institute of Technology and Science, Pilani, Rajasthan
Md Tanwiruddin Haider
National Institute of Technology, Patna, Bihar
Mahua Banerjee
Xavier Institute of Social Service, Ranchi, Jharkhand
xx Acknowledgements
N K Kamila
C V Raman College of Engineering, Bhubaneswar, Orissa
Pranam Paul
Dr B C Roy Engineering College, Kolkata, West Bengal
Sajal Mukhopadhya
National Institute of Technology, Durgapur, West Bengal
Kanhaiya Lal
Birla Institute of Technology, Patna, Bihar
Poornachandra Sarang
University of Mumbai, Mumbai, Maharashtra
Manisha J Somavanshi
Indira Institute of Management, Pune, Maharashtra
T V Gopal
Anna University, Chennai, Tamil Nadu
N Shanthi
K S Rangasamy college of Technology, Tiruchengode, Tamil Nadu
Annappa
National Institute of Technology, Surathkal, Karnataka
CH V K N S N Moorthy
R K Institute of Science and Technology, Hyderabad, Andhra Pradesh
M M Naidu
S V University College of Engineering, Tirupati, Andhra Pradesh
Finally, I thank my parents, son and wife for the love, encouragement and comfort they have
extended to me throughout my career.
D Ravichandran
Feedback
The readers of the book are encouraged to send their comments, queries and suggestions at
the following email id—tmh.csefeedback@gmail.com, mentioning the title and author name in the
subject line. Also, please report to us any piracy of the book spo ed by you.
Chapter
Introduction to Object
Oriented Programming 1
This chapter focuses on the definitions, basic concepts and salient features
of Object Oriented Programming (OOP). The pros and cons of Structured
Procedural Programming (SPP) with Object Oriented Programming (OOP) are
also summarised. Major applications of OOP are also highlighted in this chapter.
It also describes how C++ can be used to improve productivity and so ware
quality by offering features such as classes, objects, data hiding, encapsulation,
inheritance, polymorphism and templates.
1.1 INTRODUCTION
A major challenge for software engineering today is to improve the software programming process as
modern software life cycle has been changing very dramatically since the late nineties wherein the code
re-usability, reliability and maintainability are the key features. The very aim of using an object oriented
programming language is to handle a complex software design in a very easy, simple and efficient manner.
Redesigning and maintaining the source code costs much more than the reusability of the source code. The
turnover time and software cost are drastically brought down. The main aim of designing the C++ language
is to support both a procedure oriented style and an object oriented programming paradigm. In that sense,
C++ is a hybrid language which supporzts both the procedural as well as object oriented programming
styles.
Softwares designed using object oriented technology can meet up the challenges of large real world
systems by enhancing the ability to produce reliable and maintainable code. Through object oriented
programming and design, such software can naturally evolve to meet changing needs. To effectively
accomplish this, one must learn new ways of thinking about programming and problem solving.
Therefore, Object Technology (OT) is drawing attention and consideration in many areas of computing,
such as
∑ programming
∑ data bases
2 Programming with C++
Object oriented programming, or OOP, is a software development philosophy based on the following
central ideas:
∑ encapsulation
∑ inheritance
∑ information hiding
∑ data abstraction and
∑ polymorphism
Object Oriented Programming has revolutionised the very art and practice of writing computer
applications. Object is the basic unit of object oriented programming. Designing an object-oriented model
involves defining a set of classes. A class is a template from which objects are created. The template, or
blueprint, provided by a class specifies a set of data and methods that all objects created according to its
specifications will contain.
Hence, the object oriented programming approach has the advantage of producing more reliable
softwares for complex and large-scale systems.
It is well known that ‘C’ is widely accepted as a well structured programming language for a variety of
applications. It has many advantages over other high level programming languages. But it has flaws and
limitations that has made it unsuitable for complex programming projects.
Following are the major characteristics for considering any programming languages to be object oriented:
∑ objects
∑ classes
∑ data abstraction
∑ data encapsulation
∑ information hiding
∑ message passing
∑ inheritance
∑ dynamic binding
∑ polymorphism, and
∑ overloading
1.5.1 Objects
In Object Oriented Programming (OOP) paradigm, objects are the fundamental building blocks for
designing a software. In other words, an object is a collection of data members and the associated member
functions are known as methods. Objects are identified by its unique name (Fig. 1.3(b)). An object
represents a particular instance of a class. There can be more than one instance of an object. Each instance
of an object can hold its own relevant data.
Random documents with unrelated
content Scribd suggests to you:
The good-natured cousin said she would look after the baby,
while Francesca rode off down the street on her father's shoulder.
The donkeys had been put in the stable for their night's rest.
The party soon reached the Corso, which had been cleared of
carriages. Both sides were lined by an ever-growing crowd.
Just at sunset a gun was fired. Instantly a number of beautiful
horses were freed. They wore fine trappings and were without riders
or drivers. Down the Corso they raced from one end to the other. It
seemed as though they passed by like lightning.
"Good, good," shouted Beppo, as the first horse reached the goal.
This one was the winner of the race, of course.
"We can see this sport every night of the carnival," his father told
him, as they walked slowly homeward, looking at the sights on the
way.
Early the next morning Tessa and Beppo started off on their
donkeys once more. They did not wish to lose a moment of the day's
fun. They had many a mock battle with the children whom they met,
but the fighting was all good-natured, and the only weapons used
were handfuls of confetti.
In the afternoon they found themselves near Arthur and Lucy,
who were in an open carriage. They did not have their masks on, so
their friends spied them out very quickly. They were very glad to see
each other, but the crowd was so great they did not have a chance
to say much.
"Tessa," whispered Lucy, "father has something to ask your
parents. He was going to write to them if he did not see them before
the end of the carnival. Be sure to tell them. It is about you."
That was all she had a chance to say before the driver started up
the horses and she had passed on. Tessa wondered what it could be
about, but her mind was soon busy with the gay sights, and she
forgot all about it till she reached home.
The last night of the carnival was the gayest time of all. As soon
as it was dark, Tessa went with her father and mother and Beppo
out into the streets. Every one carried a torch and tried to keep it
lighted. At the same time he must try to put out as many other
torches as possible.
How the lights danced up and down the streets! What a puffing
and blowing there was all the time. Tessa no sooner got her torch
lighted than some one came up from behind and put it out. Then
she would cry, "Senza moccolo, senza moccolo." That means,
"Without light, without light."
After a while, Beppo fastened his torch to the end of a long pole.
He thought he was safe at last. But, no! a moment afterward some
one came along with a pole longer than his own and dashed it
down. The fun was all the greater for such little things as this.
The city looked wonderfully pretty with the lights dancing about
the windows and balconies and streets.
After an hour or two the crowds began to thin out. Every one was
tired. Tessa and Beppo turned homeward with their father and
mother, calling out:
"The carnival is dead. The carnival is dead."
Soon afterward they tumbled into bed, half asleep, still repeating
the words they heard echoing through the streets:
"The carnival is dead!"
CHAPTER IX.
THE BURIED CITY
When at last Mrs. Gray said it was time to go, they all felt sorry.
The drive down the mountain was quite easy. When they reached
the hotel the children went straight to bed to dream of the pleasure
to-morrow,—for they were to visit the buried city, Pompeii.
The next day was bright and clear. Although every one felt a little
tired after the excitement of the night before, they were all ready for
the day's trip.
It was a strange place, this city with no one living in it. There
were streets all laid out and the walls of houses standing. The roofs
were gone, however.
Mr. Gray explained to the children that the city was buried under
the terrible shower of ashes which settled down over it. The roofs
had been burnt or broken down by the weight above them. After a
while, soil formed above the ashes, grass began to grow, and the
rest of the world forgot about the city, once so beautiful, with its
stately palaces and grand buildings.
Most of the people had time to flee before their homes were
destroyed. But some of them stayed too long. Their skeletons were
found when the city was unearthed.
The children went into a cellar where there were marks on the
walls. The guide told them that these showed where people were
pressed against them. They must have fled to that place for safety,
but it had been of no use. They stood here prisoners until kind death
freed them from their suffering.
They saw many marble ornaments. There were ducks and geese,
rabbits and lambs, made long ago.
"All this makes me feel queer, Lucy," whispered Tessa. "I will be
glad to get back to a live city again." Lucy felt so, too. It was
interesting, of course, but it was very strange.
After the visit to Pompeii, Mr. Gray told the children that his
vacation was over and they must all go back to Rome.
"But we will not return by train," he said. "We will take a sailing
vessel, as I think you will enjoy a trip on the water."
They did enjoy it greatly. The only trouble was that it seemed too
short.
"When June comes it will be quite hot in Rome, you know," Lucy
said to Tessa. The two girls were in the bow of the boat, looking
over the edge into the water below.
"We are going then on a journey to the north of Italy, and you are
to come, too, Tessa. Father says so. We will visit Venice and sail in
boats through its streets. It seems as though I could hardly wait for
the time to come. Just think of a great city built on little islands, and
when you go to the door of your house you find yourself on the
water's edge. It must be lovely."
"Tessa," she went on, putting her arm around the little Italian's
waist, "father says that he is going to manage next winter so that
you shall stay with us and we can have lessons together with my
governess."
Tessa bent forward and kissed both of Lucy's hands. She was so
happy she could not speak.
THE END
Being three "Little Colonel" stories in the Cosy Corner Series, "The
Little Colonel," "Two Little Knights of Kentucky," and "The Giant
Scissors," put into a single volume.
BIG BROTHER
Special Holiday Editions
Each one volume, cloth decorative, small quarto, $1.25
New plates, handsomely illustrated with eight
full-page drawings in color, and many marginal
sketches.
IN THE DESERT OF WAITING: The Legend of Camelback
Mountain.
THE THREE WEAVERS: A Fairy Tale for Fathers and Mothers
as Well as for Their Daughters.
KEEPING TRYST
SWEET NANCY
The Further Adventures of the Doctor's Little Girl. By Marion Ames
Taggart.
One vol., library, 12mo, illustrated $1.50
In the new book, the author tells how Nancy becomes in fact "the
doctor's assistant," and continues to shed happiness around her.
CARLOTA
A Story of the San Gabriel Mission. By Frances Margaret Fox.
Square 12mo, cloth decorative, illustrated and decorated in colors by
Ethelind Ridgway $1.00
"It is a pleasure to recommend this little story as an entertaining
contribution to juvenile literature."—The New York Sun.
PUSSY-CAT TOWN
By Marion Ames Taggart.
Small quarto, cloth decorative, illustrated and decorated in colors
$1.00
"Anything more interesting than the doings of the cats in this
story, their humor, their wisdom, their patriotism, would be hard to
imagine."—Chicago Post.
O-HEART-SAN
The Story of a Japanese Girl. By Helen Eggleston Haskell.
Small quarto, cloth decorative, illustrated and decorated in colors by
Frank P. Fairbanks $1.00
"The story comes straight from the heart of Japan. The shadow of
Fujiyama lies across it and from every page breathes the fragrance
of tea leaves, cherry blossoms and chrysanthemums."—The Chicago
Inter-Ocean.
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.
ebookname.com