C++ [01]

There is a significant difference between C and C++ for functions with empty argument lists. In C, the declaration:
int func2();
means “a function with any number and type of argument.” This prevents type-checking, so in C++ it means “a function with no arguments.”
If you create an object on the stack or in static storage, the compiler determines how long the object lasts and can automatically destroy it. However, if you create it on the heap, the compiler has no knowledge of its lifetime. In C++, the programmer must determine programmatically when to destroy the object, and then perform the destruction using the delete keyword.
Because the storage is managed dynamically at runtime, the amount of time required to allocate storage on the heap is significantly longer than the time to create storage on the stack. (Creating storage on the stack is often a single microprocessor instruction to move the stack pointer down, and another to move it back up.)
You state that you want a function to have the flexibility of late-binding properties using the keyword virtual.
The simplest way to reuse a class is to just use an object of that class directly, but you can also place an object of that class inside a new class. We call this “creating a member object.” Your new class can be made up of any number and type of other objects, in any combination that you need to achieve the functionality desired in your new class. Because you are composing a new class from existing classes, this concept is called composition (or more generally, aggregation). Composition is often referred to as a “has-a” relationship, as in “a car has an engine.”
Because inheritance is so important in object-oriented programming it is often highly emphasized, and the new programmer can get the idea that inheritance should be used everywhere. This can result in awkward and overly-complicated designs. Instead, you should first look to composition when creating new classes, since it is simpler and more flexible. If you take this approach, your designs will stay cleaner. Once you’ve had some experience, it will be reasonably obvious when you need inheritance.
Each object can satisfy only certain requests. The requests you can make of an object are defined by its interface, and the type is what determines the interface.
Since a class describes a set of objects that have identical characteristics (data elements) and behaviors (functionality), a class is really a data type[!?] because a floating point number, for example, also has a set of characteristics and behaviors. The difference is that a programmer defines a class to fit a problem rather than being forced to use an existing data type that was designed to represent a unit of storage in a machine. You extend the programming language by adding new data types specific to your needs. The programming system welcomes the new classes and gives them all the care and type-checking that it gives to built-in types.
Notes:
***. C++ Network Programming:
(2016-02-08)

No comments:

Post a Comment