1 00:00:05,600 --> 00:00:08,740 In this video, we'll look at how we can use both inheritance 2 00:00:08,780 --> 00:00:12,480 and composition to reuse existing code in our projects. 3 00:00:13,509 --> 00:00:16,769 We've already seen what public inheritance is and how it can help us 4 00:00:16,770 --> 00:00:21,050 create class hierarchies that provide general and specialized functionality. 5 00:00:22,100 --> 00:00:25,130 Public inheritance creates an is a relationship between the 6 00:00:25,130 --> 00:00:26,769 derived and based classes. 7 00:00:27,150 --> 00:00:30,800 And with inheritance, we automatically inherit all of our base classes 8 00:00:30,840 --> 00:00:32,259 attributes and operations. 9 00:00:32,910 --> 00:00:36,000 And we can see examples like an employee is a person, 10 00:00:36,299 --> 00:00:39,209 a checking account is an account, a circle is a shape. 11 00:00:40,000 --> 00:00:43,739 However, sometimes we want to reuse existing classes, but the is a 12 00:00:43,740 --> 00:00:45,580 relationship really doesn't apply. 13 00:00:46,160 --> 00:00:49,349 For example, suppose that all persons have an account. 14 00:00:50,019 --> 00:00:53,380 I can't really say that a person is an account or an account is a 15 00:00:53,380 --> 00:00:55,529 person since that doesn't make sense. 16 00:00:56,220 --> 00:00:59,269 Instead, we can use composition to model this relationship. 17 00:01:00,340 --> 00:01:03,970 Composition creates a has a relationship between classes. 18 00:01:04,269 --> 00:01:08,840 For example, a person has an account, a player has a special 19 00:01:08,840 --> 00:01:11,439 attack, a circle has a location. 20 00:01:12,070 --> 00:01:13,200 You get the idea. 21 00:01:13,380 --> 00:01:17,429 Using a combination of inheritance and composition, we can express 22 00:01:17,460 --> 00:01:21,520 complex relationships between classes and leverage code reuse. 23 00:01:21,900 --> 00:01:22,910 Let's see an example. 24 00:01:24,639 --> 00:01:28,039 In this example, we have the person hierarchy we saw earlier. 25 00:01:28,799 --> 00:01:32,316 Note the inheritance relationships are modeled with the triangle pointing to 26 00:01:32,316 --> 00:01:34,496 the base class of the relationship. 27 00:01:34,790 --> 00:01:39,010 As we mentioned in the last slide, it wouldn't make sense to introduce another inheritance 28 00:01:39,010 --> 00:01:43,880 relationship between person and account since the is a relationship just doesn't apply. 29 00:01:44,260 --> 00:01:46,483 So we use composition instead. 30 00:01:46,483 --> 00:01:49,539 Notice the association between person and account. 31 00:01:50,580 --> 00:01:53,980 It's modeled as a solid line, no inheritance triangle this time. 32 00:01:55,010 --> 00:01:58,249 Uml provides several different types of notations to model 33 00:01:58,250 --> 00:02:01,680 associated classes that don't have an inheritance relationship. 34 00:02:02,200 --> 00:02:05,259 These are aggregation, association and composition. 35 00:02:05,619 --> 00:02:09,020 You can learn more about these in an object-oriented design course, but 36 00:02:09,020 --> 00:02:13,160 we'll use the term composition to simply mean it has a relationship, 37 00:02:13,570 --> 00:02:16,600 and we won't be concerned about whether the account object can 38 00:02:16,610 --> 00:02:19,840 logically exist without being associated with a person object. 39 00:02:21,130 --> 00:02:24,319 So what we're describing here is that a person has an account, 40 00:02:24,360 --> 00:02:25,769 that's exactly what we want. 41 00:02:26,400 --> 00:02:28,280 So does a student also have an account. 42 00:02:28,950 --> 00:02:31,260 Yes, because a student is a person. 43 00:02:31,720 --> 00:02:33,020 What about a faculty member. 44 00:02:33,490 --> 00:02:35,800 Yes, because a faculty member is a person. 45 00:02:36,309 --> 00:02:39,530 Notice that we create the association at the most general 46 00:02:39,530 --> 00:02:42,750 class in the hierarchy where it applies since it will also 47 00:02:42,750 --> 00:02:44,510 apply to its derived classes. 48 00:02:45,660 --> 00:02:47,910 We've actually been using composition all along. 49 00:02:48,130 --> 00:02:51,020 When we model class data members, we're using composition. 50 00:02:51,460 --> 00:02:54,660 Many times the instance variables are primitive types, so we don't 51 00:02:54,660 --> 00:02:57,659 include them in class diagrams, but the concept is the same. 52 00:02:58,639 --> 00:03:01,500 So when do you choose inheritance over composition. 53 00:03:02,000 --> 00:03:06,370 Many times it's easy to decide if the is a relationship doesn't make sense, 54 00:03:06,400 --> 00:03:07,990 then don't use public inheritance. 55 00:03:08,639 --> 00:03:10,089 Other times it's not so clear. 56 00:03:10,740 --> 00:03:13,660 A rule of thumb when using inheritance is to step back look 57 00:03:13,660 --> 00:03:16,909 at your design and be sure that the inheritance is appropriate. 58 00:03:17,779 --> 00:03:21,660 If you can model a relationship with composition, then you should consider 59 00:03:21,660 --> 00:03:25,429 doing that first since inheritance adds more complexity to your design. 60 00:03:25,980 --> 00:03:29,130 In this case, an inheritance relationship between student 61 00:03:29,160 --> 00:03:32,600 and account simply doesn't make sense, so we use composition. 62 00:03:35,199 --> 00:03:38,450 Here's how the class person might be declared using composition. 63 00:03:39,030 --> 00:03:40,930 Notice that it's pretty much what we've been doing 64 00:03:40,930 --> 00:03:42,120 so far in this course. 65 00:03:42,810 --> 00:03:46,919 Composition is a common design pattern for reuse and you'll see it used much 66 00:03:46,950 --> 00:03:48,459 more frequently than inheritance. 67 00:03:48,910 --> 00:03:52,290 But we can use both inheritance and composition together to 68 00:03:52,290 --> 00:03:55,740 create powerful frameworks that allow us to reuse existing code. 69 00:03:56,510 --> 00:03:59,120 Okay,o in the next video, we'll see what the syntax of 70 00:03:59,120 --> 00:04:01,350 inheritance looks like in c++.