Object-Oriented Programming ConceptsAn object is a software bundle of related state (data members or properties) and behavior (member functions or methods). Software objects are often used to model the real-world objects that you find in everyday life. Show
A class is a blueprint or prototype from which objects are created. A class is an abstract definition that is made concrete at run-time when objects based upon the class are created. Encapsulation, also known as data hiding, is the act of concealing the functionality of a class so that the internal operations are hidden, and irrelevant, to the programmer. With correct encapsulation, the developer does not need to understand how the class actually operates in order to communicate with it via its publicly available member functions and data members, known as its public interface. Encapsulation is essential to creating maintainable object-oriented programs. When the interaction with an object uses only the publicly available interface of member functions and properties, the class of the object becomes a correctly isolated unit. This unit can then be replaced independently to fix bugs, to change internal behavior or to improve functionality or performance. Encapsulation also promotes data integrity by allowing public "set" member functions to validate new values that are to be assigned to private data members. Message passing, also known as interfacing, describes the communication between objects using their public interfaces. The primary way of passing a message to an object in C++ is to call a member function for that object. Abstraction is the process of representing simplified versions of real-world objects in your classes and objects. A Object RelationshipsObjects can work together in many ways within a system. In some situations, classes and objects can be tightly coupled together to provide more complex functionality. This "has-a" relationship is known as composition. For example, modeling a car might involve creating individual classes such as Other types of relationships may be modeled. A class may simply "use" an object of another class (perhaps creating the object as a local variable in one of its member functions). A class may also "know" about an object of another class without owning it (in C++, this association relationship might be modeled using a pointer or reference to the object). Inheritance is an object-oriented programming concept used to model an "is-a" relationship between two classes. It allows one class (the derived class or subclass) to be based upon another (the base class or superclass) and inherit all of its functionality automatically. Additional code may then be added to create a more specialized version of the base class. IneritanceA derived class is more specific than its base class and represents a smaller group of objects. A direct base class is the base class from which a derived class explicitly inherits. An indirect base class is inherited from two or more levels up the class hierarchy. In the case of single inheritance, a class is derived from one base class. C++ also supports multiple inheritance, in which a derived class inherits from multiple (possibly unrelated) classes. Single inheritance is straightforward. Multiple inheritance can be complex and error prone. Single-inheritance relationships form tree-like hierarchical structures - a base class exists in a hierarchical relationship with its derived classes. C++ offers three kinds of inheritance - With
Inheritance and Member Access
A base class's A base class's A base class's The use of Declaring data members When a base class member function is inappropriate for a derived class, that member function can be redefined in the derived class with an appropriate implementation. This is called overriding the base class member function. When a derived class member function overrides a base class member function, the base class member function can still be accessed from the derived class by preceding the base class member function name with the base class name and the scope
resolution operator ( When an object of a derived class is created, the base class's constructor is called immediately (either explicitly or implicitly) to initialize the base class data members in the derived class object (before the derived class data members are initialized). Explicitly calling a base class constructor requires using the same special "member initialization list syntax" used with composition and const data members. When a derived class object is destroyed, the destructors are called in the reverse order of the constructors - first the derived class destructor is called, then the base class destructor is called. Inheritance SyntaxTo declare a derived class:
A constructor initialization list can be used to pass arguments from a derived class constructor to a base class constructor:
A derived class member function that overrides a base class member function can call the base class version of the function to do part of its work:
Upcasting and DowncastingUpcasting is converting a derived class pointer (or reference) to a pointer (or reference) of the derived class's base class. In other words, upcasting allows us to treat a derived type as though it were its base type. It is always allowed for
The Although there's no need to perform an explicit type cast on an upcast, you can do so if you want to:
The opposite process, converting a base class pointer (or reference) to a derived class pointer (or reference) is called downcasting. Downcasting is not allowed without an explicit type cast. The reason for this restriction is that the "is-a" relationship is
not always symmetric. A
The code shown above works, because the object pointed to by
C++ provides a special explicit cast called
Use of the Subtype PolymorphismThe term binding means matching a function or member function call to a function or member function definition. In C++, binding normally takes place when the program is compiled and linked. This is referred to as early binding or static binding. In object-oriented programming, subtype polymorphism refers to the ability of objects belonging to different types to respond to member function calls of the same name, each one according to an appropriate type-specific behavior. The calling code does not have to know the exact type of the called object; which member function definition is called is determined at run-time (this is called late binding or dynamic binding). In order for dynamic binding to take place in C++, several conditions must be met:
With dynamic binding, C++ distinguishes between a static type and a dynamic type of a variable. The static type is determined at compile time. It's the type specified in the pointer declaration. For example, the static type of Dynamic binding exacts a toll. Resolving the dynamic type of an object takes place at runtime and therefore incurs performance overhead. However, this penalty is negligible in most cases. One of the most common runtime techniques for implementing virtual dispatch is a virtual member function table, or v-table. A v-table is simply an array of pointers to member functions. Each class that contains virtual member functions has a v-table. Each object that is an instance of a class with virtual member functions contains, as a hidden field, a pointer to the class's v-table. The compiler encodes a member function call as an offset into a v-table, and the appropriate v-table is used with that offset at runtime to access the correct member function. Declaring Virtual Member FunctionsTo make a member function virtual, put the keyword
A member function in a derived class that overrides a virtual member function in a base class is automatically virtual as well. Destructors may also be virtual. You should make the destructor for your class virtual if it contains any virtual member functions. Abstract or Pure virtual Member FunctionsAn abstract member
function is a member function that has a special prototype, but no definition. C++ refers to abstract member functions as pure
Since a pure Abstract ClassesA class that contains one or more pure You cannot create an object of an abstract class. However, an abstract class can be used as a base class for inheritance purposes. A class derived from an abstract class must provide definitions for any pure You can also declare a pointer (or a reference) of an abstract class type. Such a pointer (or reference) would typically be used to point to a derived class object. Interface InheritanceInterface inheritance allows a derived class to inherit a base class's data type (which can be useful for subtype polymorphism) without actually inheriting any of the base class's implementation (member function definitions, etc.). An
interface can be defined in C++ as an abstract class that contains only pure virtual member functions and symbolic constants (public data members that are When a class contains an object of another class?When a class uses object of another class this is called composition.
What is the relationship between a class and an object a class?an object is an element (or instance) of a class; objects have the behaviors of their class. The object is the actual component of programs, while the class specifies how instances are created and how they behave. method: a method is an action which an object is able to perform.
When a class owns an object of another class then it is which class relationship?Dependency: An object of one class might use an object of another class in the code of a method. If the object is not stored in any field, then this is modeled as a dependency relationship.
What is the relationship between objects called?Association is a relationship between two objects. In other words, association defines the multiplicity between objects. You may be aware of one-to-one, one-to-many, many-to-one, many-to-many all these words define an association between objects. Aggregation is a special form of association.
|