0% found this document useful (0 votes)
41 views1 page

6.2 Composition

This document discusses composition as an alternative to inheritance in object-oriented programming. It provides examples to distinguish between "composition" and "inheritance", explaining that with composition, an object "has a" relationship with other objects that comprise it, rather than "is a" relationship as in inheritance. The document then presents an exercise to practice working with composition in C++ by creating a Circle class that is composed of a LineSegment class to represent the circle's radius, rather than inheriting from LineSegment.

Uploaded by

Divyanshu Rawat
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)
41 views1 page

6.2 Composition

This document discusses composition as an alternative to inheritance in object-oriented programming. It provides examples to distinguish between "composition" and "inheritance", explaining that with composition, an object "has a" relationship with other objects that comprise it, rather than "is a" relationship as in inheritance. The document then presents an exercise to practice working with composition in C++ by creating a Circle class that is composed of a LineSegment class to represent the circle's radius, rather than inheriting from LineSegment.

Uploaded by

Divyanshu Rawat
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/ 1

Composition

classroom.udacity.com/nanodegrees/nd213/parts/f9fffe8e-1984-4045-92b6-
64854de4df2b/modules/70db33ed-8e7e-45e9-ab1d-3d0b257fc196/lessons/15cd39a7-3fda-495d-af24-
c5ccd45826a8/concepts/6935d783-5125-4d5c-bdd9-9957fda9e101

Composition is a closely related alternative to inheritance. Composition involves


constructing ("composing") classes from other classes, instead of inheriting traits from a
parent class.

A common way to distinguish "composition" from "inheritance" is to think about what an


object can do, rather than what it is. This is often expressed as "has a" versus "is a".

From the standpoint of composition, a cat "has a" head and "has a" set of paws and "has
a" tail.

From the standpoint of inheritance, a cat "is a" mammal.

There is no hard and fast rule about when to prefer composition over inheritance. In
general, if a class needs only extend a small amount of functionality beyond what is
already offered by another class, it makes sense to inherit from that other class.
However, if a class needs to contain functionality from a variety of otherwise unrelated
classes, it makes sense to compose the class from those other classes.

In this example, you'll practice working with composition in C++.

Instructions
In this exercise, you will start with a LineSegment class and create a Circle class.

Note that you will compose Circle from LineSegment , instead of inheriting Circle
from LineSegment . Specifically, the length attribute from LineSegment will become
the circle's radius.

1. Create a class LineSegment .


2. Declare an attribute length in class LineSegment .
3. Define pi (3.14159) with a macro.
4. Create a class Circle , composed of a LineSegment that represent's the circle's
radius. Use this radius to calculate the area of the circle (area of a circle = πr2\pi
r^2πr2).
5. Verify the behavior of Circle in main() .

1/1

You might also like