Hands-On Object-Oriented Programming Mastering OOP Features for Real-World Software Systems Development (Anil Kumar Rangisetti) (Z-Library)
商业Author:Anil Kumar Rangisetti
No description
Tags
Support Statistics
¥.00 ·
0times
Text Preview (First 20 pages)
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
Page
1
Hands-On Object-Oriented Programming Mastering OOP Features for Real-World Software Systems Development — Anil Kumar Rangisetti
Page
2
Hands-On Object-Oriented Programming Mastering OOP Features for Real-World Software Systems Development Anil Kumar Rangisetti
Page
3
Hands-On Object-Oriented Programming: Mastering OOP Features for Real-World Software Systems Development ISBN-13 (pbk): 979-8-8688-0523-3 ISBN-13 (electronic): 979-8-8688-0524-0 https://doi.org/10.1007/979-8-8688-0524-0 Copyright © 2024 by Anil Kumar Rangisetti This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Melissa Duffy Development Editor: Laura Berendson Coordinating Editor: Gryffin Winkler Copyeditor: Kim Burton Cover designed by eStudioCalamar Cover image by Vinicius “amnx” Amano on Unsplash (unsplash.com) Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY 10004, U.S.A. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail booktranslations@springernature.com; for reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub (https://github.com/Apress). For more detailed information, please visit https://www.apress.com/gp/services/source-code. If disposing of this product, please recycle the paper Anil Kumar Rangisetti Kurnool, Andhra Pradesh, India
Page
4
To my teachers, Dr. Bheemarjuna Reddy and Shri Badrinadh Garu, for identifying my strengths, giving me wonderful opportunities to work with them, and guiding me to achieve my goals. To my lovely wife, Sravani, for being a wonderful partner and supporting me in all situations. Without her love and support, I could not accomplish it.
Page
5
v About the Author �������������������������������������������������������������������������������xiii About the Technical Reviewer ������������������������������������������������������������xv Acknowledgments ����������������������������������������������������������������������������xvii Introduction ���������������������������������������������������������������������������������������xix Chapter 1: The Importance of Object-Oriented Programming ��������������1 Algorithms vs. Software ........................................................................................2 Algorithm Characteristics ................................................................................2 Write an Algorithm ...........................................................................................3 Software ..........................................................................................................5 Software Development Challenges..................................................................6 Introduction to OOP Concepts ...............................................................................7 Class ................................................................................................................7 Objects ..........................................................................................................13 Inheritance ....................................................................................................16 Polymorphism ................................................................................................18 How OOP Approaches Simplify the Software Complexity ...................................19 Systematically Modeling Real-World Entities into Software ...............................21 Hands-on Activity: Online Shopping ..............................................................23 Hands-on Activity: Simple Adventurous Game ..............................................33 Summary.............................................................................................................39 Practice: Hands-on Activities ..............................................................................40 Table of Contents
Page
6
vi Chapter 2: Start Learning OOP Using C++ ������������������������������������������43 C++ OOP Constructs ...........................................................................................44 C++ Specific Programming Constructs .........................................................45 Model Real-World Entities Using C++ Classes ...................................................64 Interacting with Objects ......................................................................................77 Object Access Control Modes..............................................................................87 Hands-on Activity: Smart Applications ..............................................................100 Summary...........................................................................................................109 Practice: Hands-On Activities ............................................................................110 Chapter 3: Systematically Starting and Stopping Software Objects ������������������������������������������������������������������������������113 Software Objects Startup and Shutdown Sequences .......................................114 Starting a Software Application ...................................................................115 Closing a Software Application ....................................................................118 Constructors for Handling Startup Sequences ..................................................120 Constructors in C++ ....................................................................................120 C++ Supporting Constructors .....................................................................123 C++ Compiler Providing Constructors .........................................................128 Hands-on Activities for Practicing Constructors ..........................................130 The Importance of Destructors for Doing Graceful Shutdowns .........................139 Destructors in C++ ......................................................................................139 Hands-on Destructors .................................................................................143 Hands-on Activity 1: Constructors .....................................................................150 Hands-on Activity 2: Destructors .......................................................................162 Summary...........................................................................................................166 Practice: Hands-on Activities ............................................................................167 Table of ConTenTs
Page
7
vii Chapter 4: Exploring Important C++ Features ����������������������������������169 C++ Friend Classes and Functions ...................................................................170 C++ Friend Functions ..................................................................................171 C++ Friend Class .........................................................................................174 Hands-on Activity: When to Use C++ Friend Concepts .....................................177 Best Practices in Passing Arguments ...............................................................188 Arguments Passing Activities ......................................................................190 Sharing Data of Objects Using C++ Static ........................................................200 Restricting Accidental Changes Using C++ const ............................................208 C++ Const and Pointer Usage Activities ......................................................212 Summary...........................................................................................................225 Practice: Hands-on Activities ............................................................................225 Chapter 5: Quickly and Systematically Model Real-World Problems into Software ��������������������������������������������������������������������227 Modeling Real-World Problems into Software Design ......................................228 A Simple Gaming Application ......................................................................229 Modeling Game World Entities Using C++ Classes ...........................................235 Game Implementation Using C++ Classes .......................................................244 Model Application Entities Using C++ Classes .................................................255 Basic Tasks Related to a Shopping Application ...........................................270 Basic Customer Interactions in a Shopping Application ..............................274 Basic Shopkeeper Interactions in a Shopping Application ..........................278 Simulating Shopping Application Tasks .......................................................282 Summary...........................................................................................................288 Practice: Hands-on Activities ............................................................................288 Table of ConTenTs
Page
8
viii Chapter 6: Quick Software Development Using OOP ������������������������291 The Importance of Inheritance ..........................................................................292 Inheritance Approaches ...............................................................................294 Issues in Combining Inheritance Approaches ..............................................299 Access Controls and Inheritance .................................................................301 Constructors and Destructors Working Order in Inheritance Context ..........306 Practicing the Reduce and Reuse Principle ......................................................311 Building New Software Building Blocks Versions Easily ...................................323 Combine or Connect Objects Wisely .................................................................335 Object Composition: Special Gaming Weapon .............................................336 Object Composition and Aggregation ..........................................................342 Hands-on Activity: Inheritance and Object Association ...............................349 Summary...........................................................................................................359 Practice: Hands-on Activities ............................................................................359 Chapter 7: Easy-to-Use Software Development Using OOP ��������������361 The Importance of Polymorphism .....................................................................362 Function Overloading ...................................................................................363 Function Overriding .....................................................................................367 Overloading Operators to Deal with Complex Objects Computations ...............371 How to Overload Operators..........................................................................372 Practice Operator Overloading Usage ..........................................................374 Generic Functions and Data Structures ............................................................381 Practice with Generic Functions ..................................................................383 Generic Data Structures ..............................................................................388 Practice Implementing a Generic Data Structure ........................................389 Using Dynamic Polymorphism for Offering Common Interfaces .......................395 Table of ConTenTs
Page
9
ix The Importance of Virtual Functions ............................................................396 The Importance of Pure Virtual Functions and Abstract Classes .................401 Practice with Dynamic Polymorphism .........................................................403 Summary...........................................................................................................408 Practice: Hands-on Activities ............................................................................409 Chapter 8: Design Patterns ���������������������������������������������������������������411 Introduction to Design Patterns ........................................................................412 Creational Patterns ......................................................................................412 Structural Patterns ......................................................................................413 Behavioral Patterns .....................................................................................414 Learning Creational Design Patterns ................................................................415 The Factory Method .....................................................................................418 The Singleton Pattern ..................................................................................423 Structural Design Patterns ................................................................................428 The Facade Pattern .....................................................................................432 The Proxy Server Pattern .............................................................................440 Behavioral Design Patterns ...............................................................................445 The Chain of Responsibility Pattern .............................................................450 The Template Method ..................................................................................458 Summary...........................................................................................................465 Chapter 9: Event-Driven Programming ���������������������������������������������467 The Importance of Event-Driven Programming .................................................468 Key Concepts ...............................................................................................469 Advantages and Use Cases .........................................................................473 Structure ...........................................................................................................474 Using C++ for Events and Event Handlers ..................................................476 Implementing Application Events and Subscribing to Classes ....................480 Table of ConTenTs
Page
10
x Quick Practice ...................................................................................................483 Hands-on Activity: Design a Simulator ..............................................................491 IoTSensorsHandler Events ...........................................................................492 SmartVehiclesHandler Custom Events ........................................................497 SmartApplication Simulation .......................................................................501 Summary...........................................................................................................504 Practice: Hands-on Activities ............................................................................504 Chapter 10: A Brief Introduction to OOP in Python and Solidity �������507 Other Important OOP Languages .......................................................................508 The Importance of Python Programming .....................................................508 The Importance of Solidity Programming ....................................................510 Python Basic Programming Constructs for OOP ...............................................511 Python Basic Programming Constructs .......................................................511 Python OOP Constructs ................................................................................515 Python OOP Constructs for Inheritance .......................................................519 Python OOP Constructs for Polymorphism ..................................................521 Practicing OOP in Python ..................................................................................526 Using Python for Encapsulation and Data- Hiding Features .........................526 Using Python to Implement Inheritance ......................................................532 Using Python for Polymorphism ..................................................................538 Solidity Basic Programming Constructs for OOP...............................................541 Solidity Basics .............................................................................................541 Solidity Inheritance Programming ...............................................................546 Solidity Polymorphism Programming ..........................................................549 Practicing OOP in Solidity .................................................................................552 Table of ConTenTs
Page
11
xi Using the Remix Editor for Practicing Solidity .............................................553 Practicing with Smart Contracts ..................................................................556 Extending Smart Contracts Using Inheritance .............................................562 Using Solidity for Polymorphism .................................................................568 Summary...........................................................................................................573 Index �������������������������������������������������������������������������������������������������575 Table of ConTenTs
Page
12
xiii About the Author Dr. Anil Kumar Rangisetti received his PhD in computer science and engineering from the Indian Institute of Technology (IIT) Hyderabad. He has nearly 10 years of teaching and research experience in computer science and engineering. During his career, he worked at prestigious Indian institutions such as IIIT Dharwad, SRM-AP, and GMR, and at software development and research labs such as Aricent. Currently, he is an assistant professor in the CSE Department at IIITDM, Kurnool. He trains students in OOP languages and how to use advanced simulators (NS-3), Docker, and networking tools for developing applications, and he has guided many undergraduate and postgraduate students in their projects. Broadly, Dr. Rangisetti’s research interests include Wi-Fi technologies, next-generation mobile networks, software-defined networking (SDN), network functions virtualization (NFV), and cloud computing. He also writes and reviews books on computer science technologies and programming languages, and he is the author of Advanced Network Simulations Simplified (Packt, 2023).
Page
13
xv Saravan Nanduri is a seasoned senior full- stack web developer with nearly two decades of experience in the information technology sector, specializing in developing object- oriented applications. Having worked with prestigious companies such as Tech Mahindra and Accenture, Saravan brings expertise to every project he undertakes. After graduating with computer science and engineering degrees in 2005, Saravan embarked on a remarkable journey leading him to the United States in 2015, where he worked for government agencies before assuming the role of senior software engineer at SS&C Innovest in 2019. With a solid foundation in computer science, Saravan is adept at architecting and implementing both client and web-based enterprise applications. His proficiency spans a wide spectrum of technologies, including C++ and Microsoft .NET frameworks, such as C# and MVC. Beyond his professional endeavors, Saravan values relationships and camaraderie. About the Technical Reviewer
Page
14
xvii Acknowledgments First, I would like to thank Apress for accepting my book idea and giving me this wonderful opportunity. I would especially like to thank Melissa Duffy for keenly going through the book proposal and suggesting to me how to make the book proposal interesting and perfect. Melissa’s support and encouragement throughout the book-writing process is highly helpful. Melissa’s simple suggestions improved the quality of the book’s content. I want to thank Nirmal Selvaraj for his continuous support in the entire review and the book contents finalization process. His timely help and support helped me to finish on time. I thank the book’s technical reviewer, Sarvan Nanduri, for his valuable time and suggestions in all hands-on activities and technical concepts. His keen observations helped me correct all kinds of errors and incorporate necessary topics to improve the book’s quality tremendously. I would like to thank every member of the Apress for supporting me in writing this book. I would love to work with the Apress team again. I give my heartfelt thanks to all my students for their interest in attending my lectures and working with me. All my students’ curiosity, comments, and suggestions helped me to write this book. Finally, I thank all my family members, friends, and colleagues for their love and support.
Page
15
xix Introduction Object-oriented programming (OOP) is an essential skill for implementing extendible, reusable, and easy-to-use software systems. To develop any application software or system software, learning OOP concepts and programming is necessary. OOP basic principles help in easily handling a wide variety of real-world software systems (games, application software, novel systems) implementations. This book blends OOP concepts and programming activities for active learning. All hands-on activities and real-time scenarios are described with step-by-step procedures in terms of designing, programming, and evaluations. You will learn OOP features through real-world examples and practice through C++ programming hands-on activities. You will also learn advanced design and development skills, such as design patterns and event-driven programming for handling novel systems design and development. Finally, you are briefly introduced to OOP features practice through other important OOP languages: Python and Solidity. This book is organized into three parts. In Part 1 (Chapters 1–4), you learn and practice OOP concepts using C++ for solving real-world software development problems. Part 2 (Chapters 5–7) explains how to model real-world problems into reusable, extendible, and easy-to-use software development blocks using OOP concepts such as inheritance, object associations, and polymorphism.
Page
16
xx In Part 3 (Chapters 8–10), you learn how to use design patterns and event-driven programming for handling complex software system object creation, behavior, and interactions. Finally, you are introduced to OOP using Python and Solidity. By the end of this book, you will have learned how to design and implement a variety of real-world software systems from scratch using OOP principles, design patterns, and event-driven programming skills. InTroduCTIon
Page
17
1© Anil Kumar Rangisetti 2024 A. K. Rangisetti, Hands-On Object-Oriented Programming, https://doi.org/10.1007/979-8-8688-0524-0_1 CHAPTER 1 The Importance of Object-Oriented Programming Object-oriented programming (OOP) is essential for handling challenges in developing flexible, extendible, reusable, and easy-to-use software systems. OOP approaches simplify the complexity of modeling real-world application concepts into software building blocks. OOP offers powerful programming constructs and principles to deal with the complexity of software development. OOP constructs such as classes help you to systematically map real-world entities, and it helps in hiding the implementation details of the entities, controlling their data access, and simplifying the software system interactions, activities, and tasks. Moreover, OOP principles such as inheritance and polymorphism help you to develop reusable and easy-to-use software systems. Learning OOP helps you deal with the complexity of any software, such as e-commerce applications, system software (e.g., device drivers, compilers, operating systems, databases), next-generation applications such as IoT, industrial IoT (IIoT), smart applications, and many more. To appreciate the importance of learning OOP, this chapter discusses the following topics.
Page
18
2 • Algorithms vs. software • Software development challenges • Introduction to OOP concepts • How OOP approaches simplify the software complexity • Systematically modeling real-world entities into software Algorithms vs. Software Before exploring software, you should know how to start writing a program for solving well-defined problems, such as mathematical, computational, searching, and sorting problems. Solving these problems through a program involves considering all necessary inputs and defining a logical sequence of computational steps to get the desired results. Formally, it is known as writing an algorithm. This section briefly introduces the following topics. • Algorithm characteristics • Writing an algorithm • Software characteristics • Software development challenges Algorithm Characteristics An algorithm defines a logical sequence of instructions or commands to solve a problem. For instance, algorithms are highly suitable for implementing programs to solve specific problems such as searching, sorting, data structures accessing problems, computational problems, and Chapter 1 the ImportanCe of objeCt-orIented programmIng
Page
19
3 many mathematical problems. Algorithms can be easily converted into computer programs using basic programming constructs such as data structures, conditional statements, loops, and functions. • Simple modeling approaches such as flow charts are helpful to write algorithms. • Algorithms’ logical sequence of steps can be converted into programs using procedural program languages such as C. • An algorithm’s success mainly depends on its performance. Algorithm performance is usually defined in terms of space and time complexity. • Developing efficient algorithms is all about reducing space and time complexity. For example, many sorting algorithms have evolved to reduce time complexity from bubble sort (O(n^2)) to quick sort (O(logn)). Here, the time complexity is represented in Big O notation to represent the upper bounds of algorithms. • Algorithms can be developed into programs with smaller teams or individuals. • Procedural-oriented programming languages (e.g., C) are sufficient to convert algorithms into programs. Next, let’s look at how to write an algorithm and convert it into a program using procedural programming language constructs. Write an Algorithm Let’s solve a problem related to searching for an element from any given list of elements. Chapter 1 the ImportanCe of objeCt-orIented programmIng
Page
20
4 • Input: List of elements (list [0 to n]) and a searching element (key) • Output: Element found (True), Element not found (False) 1. Index=0 2. Traverse through the list of elements until the list ends. Check the following conditions: In case key presents in the list: return True otherwise Go to 2: 3. If list ends: return False Now it can be easily converted into any procedural-oriented program constructs such as if-else, for loop, and functions (). For example, let’s write a C function to solve the search problem. int search(int list [], int n, int key) { int i=0; for (i=0;i<n;i++) { if (list[i] == key) return 1; } return 0; } Chapter 1 the ImportanCe of objeCt-orIented programmIng
Comments 0
Loading comments...
Reply to Comment
Edit Comment