1 00:00:05,300 --> 00:00:08,600 In this video, we'll learn a little bit more about classes and objects. 2 00:00:09,200 --> 00:00:13,200 You can think of classes as blueprints from which objects are created. 3 00:00:13,700 --> 00:00:15,800 Classes are user-defined types. 4 00:00:15,800 --> 00:00:18,360 And one of the goals in object-oriented programming 5 00:00:18,360 --> 00:00:22,460 is to make the user-defined types feel like they're part of the programming language. 6 00:00:22,820 --> 00:00:24,520 So when we create our classes, 7 00:00:24,520 --> 00:00:28,420 we want to be able to use them, just like we use integers, doubles and so forth, 8 00:00:28,420 --> 00:00:29,420 easy and simple. 9 00:00:30,620 --> 00:00:32,820 Classes have attributes which are data. 10 00:00:33,180 --> 00:00:35,680 And they also have functions, which are called methods. 11 00:00:36,480 --> 00:00:38,580 As mentioned in the previous videos, 12 00:00:38,580 --> 00:00:42,980 classes can hide data and methods that are only used internally by the class. 13 00:00:43,380 --> 00:00:46,980 This is done using the private and public access modifiers. 14 00:00:47,580 --> 00:00:51,480 The goal of a class is to provide a well-defined public interface 15 00:00:51,480 --> 00:00:54,780 that the user of the class can easily use to solve their problem. 16 00:00:55,580 --> 00:00:57,580 Here are some example classes: 17 00:00:57,580 --> 00:01:01,580 account, employee, image, standard vector and standard string. 18 00:01:01,780 --> 00:01:04,379 These are all classes that model real-world entities. 19 00:01:04,980 --> 00:01:08,580 Vector and string are part of the c++ standard library. 20 00:01:08,830 --> 00:01:13,430 Account, employee and image might be classes that we would create in our applications. 21 00:01:15,230 --> 00:01:18,030 Okay. So now that we know what a class is, what's an object? 22 00:01:18,330 --> 00:01:22,330 Well, objects are created from classes and represent a specific 23 00:01:22,330 --> 00:01:24,530 instance of the class they're created from. 24 00:01:25,080 --> 00:01:28,980 So if I have an account class, I can create a Frank's account object 25 00:01:28,980 --> 00:01:33,970 that's a specific instance of an account that models Frank's account information. 26 00:01:34,770 --> 00:01:37,130 We can have as many objects as we need. 27 00:01:37,130 --> 00:01:39,730 If we were modeling a real banking application, 28 00:01:39,730 --> 00:01:42,530 we could have hundreds of thousands of account objects, 29 00:01:42,530 --> 00:01:45,530 each representing an individual instance of an account. 30 00:01:46,300 --> 00:01:48,560 Each object has its own identity, 31 00:01:48,560 --> 00:01:51,160 and each can use the methods defined in the class. 32 00:01:51,960 --> 00:01:53,320 Let's see an example. 33 00:01:53,820 --> 00:01:57,480 We know that ints and doubles are primitive types in c++. 34 00:01:57,480 --> 00:02:00,680 They're not classes, but this analogy will help make a point. 35 00:02:01,380 --> 00:02:05,370 In the first two lines, we define two integers, high score and low score. 36 00:02:05,730 --> 00:02:10,610 What does that tell us? Well, it tells us what the valid values are for those variables, 37 00:02:10,810 --> 00:02:14,510 and it also tells us the operations we can perform on those variables, 38 00:02:14,510 --> 00:02:18,110 for example, addition, subtraction, multiplication and so forth. 39 00:02:18,610 --> 00:02:21,110 Again, integers are not classes. 40 00:02:21,110 --> 00:02:23,770 But just for a moment, think of int as the class 41 00:02:23,770 --> 00:02:27,870 and low score and high score as objects created from the int class. 42 00:02:29,070 --> 00:02:32,370 Notice that low score and high score are instances of integer. 43 00:02:32,810 --> 00:02:35,470 They each have a value, and they each have an identity. 44 00:02:36,170 --> 00:02:37,970 Now look at the next two lines. 45 00:02:37,970 --> 00:02:42,740 Let's assume that we've already written the account class, and it's a true c++ class. 46 00:02:43,340 --> 00:02:47,540 Notice the syntax and how similar it is to the syntax for the integers above. 47 00:02:48,240 --> 00:02:50,840 Account is a user-defined type. 48 00:02:50,840 --> 00:02:55,400 So Frank account and Jim account are instances of the account class, 49 00:02:55,600 --> 00:02:56,700 they are objects. 50 00:02:57,300 --> 00:03:01,300 Each of these objects has an identity and values associated with them, 51 00:03:01,300 --> 00:03:03,100 just like high and low score did. 52 00:03:03,900 --> 00:03:07,500 This should look familiar since we've already been using objects all along. 53 00:03:07,500 --> 00:03:09,760 The last two lines create a scores object 54 00:03:09,760 --> 00:03:12,020 that's an instance of a vector of integers 55 00:03:12,520 --> 00:03:15,020 and a name object that's an instance of string. 56 00:03:15,720 --> 00:03:19,020 Okay. So now that we know a little bit more about classes and objects, 57 00:03:19,020 --> 00:03:23,120 let's see how we can declare a class in c++ in the next video.