Classes and Objects

Classes and Objects

Copyright: © 2024 |Pages: 99
DOI: 10.4018/979-8-3693-2007-5.ch007
OnDemand:
(Individual Chapters)
Available
$37.50
No Current Special Offers
TOTAL SAVINGS: $37.50

Abstract

Chapter 7 offers a meticulous exploration of the cornerstone of C++: classes and objects, ushering readers into the object-oriented paradigm. It commences with foundational topics on classes, their data members, and member functions. A thorough introduction to object-oriented analysis and design (OOAD) and the historical development of the unified modeling language (UML) creates a framework for understanding modern software design. As readers progress, the chapter unfolds the nuances of defining classes, with a UML representation of a sample class 'GradeCourse'. Emphasis is placed on software engineering principles, encompassing set and get functions, constructors, and destructors. The crucial concept of encapsulation is elucidated with detailed discussions on header files, source files, and client files. This chapter delves deep into advanced topics such as constructors (default and overloaded), destructors, static members, operator overloading, and the significance of the 'this' pointer.
Chapter Preview
Top

7.1 Introduction

Human thought is object-based. People, animals, plants, automobiles, airplanes, buildings, computers, and so on may be observed everywhere in the actual world.

Occasionally, we separate items into two categories: living and nonliving. Animated objects are “living” in the sense that they move and perform actions. On the contrary, inanimate objects do not move on their own. Yet, both sorts of objects have some characteristics. They all possess characteristics (such as size, shape, color, and weight) and display behaviors (e.g., a ball rolls, bounces, inflates, and deflates; a baby cries, sleeps, crawls, walks, and blinks; a car accelerates, brakes, and turns; a towel absorbs water). We will investigate the properties and behavior of software objects.

Humans gain knowledge about existing items by analyzing their characteristics and watching their behavior. Various items might have similar characteristics and display comparable actions. For instance, comparisons may be drawn between infants and adults, as well as between humans and chimpanzees.

Object-oriented design (OOD) represents software in a language comparable to that used to describe physical things. It makes use of class associations, in which things of a specific class, such as a class of vehicles, share similar properties; for example, automobiles, trucks, little red wagons, and roller skates all share many similarities. OOD exploits inheritance relationships, in which new classes of objects are formed by absorbing properties of current classes and adding their own unique traits. Objects of the class “convertible” have all the features of the more general class “automobile,” but their roof may be raised and lowered.

Object-oriented design offers a natural and straightforward perspective on the software design process, namely modeling things by their properties, actions, and interrelationships, exactly as we describe real-world objects. OOD also simulates inter-object communication. Objects interact via messages in the same way that people do (e.g., a sergeant instructs a soldier to stand at attention). A bank account object may receive a message instructing it to reduce its balance by a specified amount due to a client withdrawal.

OOD encapsulates (wraps) properties and operations (behaviors) in objects. An object's properties and actions are linked in a way that can't be separated. Things have the property of concealing information. This implies that objects may know how to communicate with one another via well-defined interfaces, but they are often not permitted to know how other objects are implemented. Instead, implementation details are typically concealed within the objects themselves. As long as we know how to operate the accelerator pedal, the brake pedal, the steering wheel, etc., we can drive a car efficiently without understanding the internal workings of engines, gearboxes, brakes, and exhaust systems. As we shall see, information concealment is important for excellent software engineering.

Object-oriented languages like C++ are prevalent. Programming in such a language is referred to as object-oriented programming (OOP), and it allows computer programmers to implement an object-oriented design as a functional software system. On the other side, procedural languages like C tend to result in action-oriented programming. In C, the function is the fundamental programming unit. The programming unit in C++ is the class, from which objects are ultimately instantiated (an OOP term for “created”). Classes in C++ include data and methods that implement operations and characteristics, respectively.

The focus of C programmers is on writing functions. Programmers organize activities that execute a common job into functions and then organize functions into programs. Despite the significance of data in C, it is believed that data exists largely to assist the operations performed by functions. The verbs in a system specification assist the C programmer in determining the collection of functions that will collaborate to create the system.

Complete Chapter List

Search this Book:
Reset