0% found this document useful (0 votes)
7 views5 pages

Lab Manual 6 1 31102020 125421pm

solved oop lab manual

Uploaded by

ikhalil2718
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Lab Manual 6 1 31102020 125421pm

solved oop lab manual

Uploaded by

ikhalil2718
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab Manual 6

Object Composition

Objectives:

 To understand the concept of has-a relationship.


 To understand the design of a composite class.
 To learn the use of class member initializer syntax.
Object Composition:

In real-life, complex objects are often built from smaller, simpler objects. For
example, a car is built using a metal frame, an engine, some tires, a transmission,
a steering wheel, and a large number of other parts. A personal computer is built
from a CPU, a motherboard, some memory, etc… Even you are built from smaller
parts: you have a head, a body, some legs, arms, and so on. This process of
building complex objects from simpler ones is called object composition.

Broadly speaking, object composition models a “has-a” relationship between two


objects. A car “has-a” transmission. Your computer “has-a” CPU. You “have-a”
heart. The complex object is sometimes called the whole, or the parent. The
simpler object is often called the part, child, or component.

In C++, you’ve already seen that structs and classes can have data members of
various types (such as fundamental types or other classes). When we build classes
with data members, we’re essentially constructing a complex object from simpler
parts, which is object composition. For this reason, structs and classes are
sometimes referred to as composite types.

Object Composition is useful in a C++ context because it allows us to create


complex classes by combining simpler, more easily manageable parts. This
reduces complexity, and allows us to write code faster and with less errors
because we can reuse code that has already been written, tested, and verified as
working. An example is shown below
Example 6.1:
Example 6.2:
++++++++++++++++++++++++++++

You might also like