Object-Oriented Programming (often known by its acronym, OOP) concerns, as its name indicates, objects. A common purpose of a program is to provide a solution to real-world tasks , which involve persons, places, things, or concepts. Programs use objects to represent real-world persons, places, things, or concepts.
Real-world objects also have relationships with other real-world objects. These relationships are either an is a relationship or a has a relationship.
An example of an is a relationship is that a teacher is a person. This type of relationship is referred to as inheritance. Inheritance enables you to reuse existing code, such as the person class, when creating classes that represent more specialized objects, like teachers .
An example of a has a relationship is that a car has an engine. This type of relationship also is referred to as containership. In addition, containership enables you to reuse existing code, such as the engine class, when creating classes that represent the containing object, like cars .
Code reusability makes application development faster since you dont have to reinvent the wheel. Additionally, the applications you develop are less buggy since the code you are reusing already has been tested. Accordingly, OOP enables programmers to model programming objects after complex real-world objects, and reuse existing, tested code.
Objects generally are too complex to be described by a single variable. Additionally, the several variables necessary to describe an object may have different data types, so an array is not an option.
A solution is to use a structure to describe complex objects. A structure is a programmer-defined data type that enables you to package related variables together, even if the variables are of different data types. The variables that belong to a structure are called member variables.
This chapter showed you how to declare a structure. The declaration commences with struct, which is a keyword indicating that a structure is being declared. The struc t keyword is followed by a name that indicates what the structure represents.
Structures, like functions, have a body, enclosed in open and close curly braces. However, unlike functions, the close curly brace must be followed by a semicolon. The body of the structure contains the member variables of the structure.
The effect of declaring a structure is similar to declaring your own data type. Accordingly, you need to declare a structure variable to create an instance of a structure. If you have many structure instances, you may declare a separate variable for each instance, or instead declare an array of structure instances.
You use the dot operator (.) with the structure instance name to access the member variables, whether the access is to obtain the value of the variable or to assign a value to it.
There are two ways you may initialize a structure. The first way is to use an initialization list. The second way is to use a constructor. As you learned in Chapter 13, a constructor is a function that is automatically called when you attempt to create an instance of an object. You may create more than one constructor, such as one with no arguments and one with several arguments.
In OOP, one principle is to separate what a function does from how it does it. In this spirit, programmers often separate a constructors prototype from its implementation, implementing the constructor outside of the body of the structure. If you do this, then in the function header of the constructors implementation, you need to precede the constructor name with the structure name and the scope resolution operator (::) to tell the compiler that the implementation is of a structure member function.
A structure may be passed as a function argument. Unlike an array name, a structures value is not an address. So, to change the values of its member variables, a structure needs to be passed by reference or address. However, a structure often is passed by reference even if the function will not change the value of its member variables because less memory is required to pass the address of an object than the object itself, which may take up a lot of bytes. You precede the structure instance in the function argument list with the const keyword to prevent the function from inadvertently changing the values inside the structure instance.
You may nest a structure within another structure. Using the Person structure example, every person has a birthday. A birthday is a date, and a date, in turn , may be defined by a Date structure that contains three member variables which represent the month, day, and year of the particular date. You then could add to the Person structure declaration a member variable of the structure Date to represent the persons birthday. This is an example of the OOP concept of containership in that a person has a birthday.
Classes are quite similar to structures. You declare a class similarly to how you declare a structure, just substituting the class keyword for the struct keyword.
An important difference between a structure and a class is that member variables are, by default, public in a structure but private in a class. A public member variable may be accessed anywhere in the program. By contrast, a private member variable may be accessed only by a member function of the same class.
In classes, typically member functions are used to read from, and write to, the private member variables. These member functions are public so they can be accessed outside the class. One benefit of keeping the member variables private and using public member functions for read and write access is input validation. Keeping the member variables private is an example of the OOP concepts of encapsulation and information-hiding.
I hope you enjoyed this book as much as I enjoyed writing it, and I wish you the best of luck in your future programming endeavors.