01 - [Classes] C++ Intermediate Programming Tutorial

Nirex
26 Feb 201921:10

Summary

TLDRThis tutorial provides an introduction to classes in C++ programming. It explains what classes are, how to create them, and their key components such as attributes (like age, name) and methods (like walking or driving). The video walks through creating a simple 'Human' class with attributes and member functions, showing how to declare and define these. It also covers access modifiers (public, private, and protected) and their importance in controlling visibility. Finally, the instructor demonstrates object creation and method usage, helping beginners grasp the fundamentals of object-oriented programming in C++.

Takeaways

  • 📚 A class in C++ is essentially a structure used to store data, defined by a header and C++ file.
  • 📝 To create a class, you can either add a new item or class in Visual Studio, then name the class and its files.
  • 👥 Classes represent real-world or conceptual objects with attributes (like age or name) and methods (like walking or driving).
  • 🔧 Methods in a class are functions that define what actions an object can perform, and attributes define its properties.
  • ⚙️ Inside a class, there are access modifiers: public, private, and protected, which control access to attributes and methods.
  • 🚶 Methods in the class can be created within the header file or defined in a separate CPP file by including the class name with two colons (::).
  • 👤 Objects are instances of classes, meaning that a class defines the blueprint, and an object represents an actual instance (e.g., John is an object of the class Human).
  • 🛡️ By default, class members are private unless specified otherwise with access modifiers.
  • 📝 Methods inside a class are referred to as member functions, and attributes as member variables.
  • 💡 Classes are versatile and can represent anything, from real-world objects like humans to conceptual ideas like rocks or mathematical objects.

Q & A

  • What is a class in C++?

    -A class in C++ is a structure that represents data. It serves as a blueprint for objects, allowing you to define attributes (data) and methods (functions) that represent real-world entities or conceptual models.

  • How do you create a class in C++?

    -To create a class in C++, you can either manually add a header file and a C++ file with the same name or use Visual Studio to create a class through the 'Add Class' option. The class declaration starts with the 'class' keyword followed by the class name.

  • What are the attributes and methods of a class?

    -Attributes are the data or properties that define a class, such as age or name. Methods (or member functions) are the actions that objects of the class can perform, such as 'walk' or 'drive'.

  • What is the difference between functions and methods in C++?

    -In C++, functions that are declared outside of a class are called functions, whereas functions declared inside a class are called methods or member functions.

  • What is the purpose of access modifiers in a class?

    -Access modifiers in a class (public, private, and protected) determine the visibility and accessibility of the class members. Public members can be accessed by any code, private members can only be accessed within the class itself, and protected members can be accessed by the class and its derived classes.

  • How do you access the attributes or methods of a class object?

    -You can access the attributes or methods of a class object using the dot operator (.). For example, if you have an object 'John' of the class 'Human', you can access the age attribute using 'John.age' and call a method like 'John.walk()'.

  • What is the difference between a class and an object in C++?

    -A class is a blueprint or template that defines the structure and behavior of objects, while an object is an instance of a class. For example, 'Human' is a class, and 'John' is an object created from that class.

  • What are the default access levels for class members in C++?

    -By default, the members of a class in C++ are private. If no access modifier is specified, the members are treated as private, meaning they are only accessible from within the class.

  • How do you separate class declarations and definitions?

    -Class declarations (including the definition of member functions) are typically placed in a header file (.h), while the implementation or definition of member functions is done in a corresponding .cpp file. This separation improves code organization and maintainability.

  • What are the key things to remember when working with classes in C++?

    -Key points include understanding attributes and methods, using proper access modifiers (public, private, protected), recognizing the difference between classes and objects, and knowing how to initialize and use class objects.

Outlines

00:00

🛠️ Introduction to Creating Classes in C++

In this section, the speaker introduces the concept of classes in C++ and how to create them within a project. A class is described as a structured data type, with a focus on the practical steps of adding a class in an IDE. The speaker explains how to create a new class by either manually adding a .cpp and .h file or using IDE tools to generate them. Additionally, there's a mention of base classes and virtual destructors, which are ignored for simplicity. Finally, a brief troubleshooting guide for common errors, such as missing entry points, is provided.

05:03

👤 Defining Class Attributes and Methods

This paragraph explores how classes represent real-world or conceptual entities through attributes (data) and methods (functions). Using the example of a 'Human' class, attributes like age, name, and surname are introduced, emphasizing how these traits are common to all humans. The speaker clarifies the difference between attributes (such as age) and methods (like walking or driving), explaining that methods define what an object can do. The explanation also highlights C++ syntax, distinguishing between functions outside of a class (functions) and those inside a class (methods or member functions).

10:04

🔒 Understanding Public, Private, and Protected Access Modifiers

In this section, the concept of access control within a class is introduced. The speaker explains the importance of the public, private, and protected segments of a class, comparing them to 'private parts' of a human that are not visible to others unless specifically shared. Public members are accessible to other classes and objects, while private members are hidden. The speaker also touches on the 'friend' keyword and protected sections, which allow visibility under inheritance or among friends. The paragraph sets the stage for understanding encapsulation and access modifiers, which are vital for class design.

15:07

📋 Class vs. Object and Initializing Objects

This part of the tutorial dives into the difference between a class and an object, explaining that a class is essentially a blueprint, while an object is an instance of that blueprint. Using the 'Human' class example, the speaker demonstrates how to create and initialize an object, assigning values to its attributes and calling its methods. It covers basic object initialization and the dot operator for accessing attributes and methods. The speaker also emphasizes that classes are data types, much like primitive types such as int, and objects are instances of these types.

20:07

🧠 Recap: Classes as Real-World Representations

The final paragraph wraps up the tutorial, reinforcing the idea that classes in C++ are representations of real-world entities or conceptual ideas. The speaker highlights the flexibility and possibilities that classes offer, noting that any object or concept we can imagine can be represented using a class. They briefly mention advanced concepts like constructors, destructors, operator overloading, and the 'Rule of Three/Five,' which will be discussed in future tutorials. The tutorial concludes with a call to action for viewers to ask questions, like the video, and subscribe for future content.

Mindmap

Keywords

💡Class

A class in C++ is a blueprint or template for creating objects. It defines the structure of objects, including attributes (data) and methods (functions). In the video, the instructor explains that a class is a representation of something in the real world, like a human or a conceptual thing, and shows how to create one by defining attributes like age and methods like walk.

💡Attributes

Attributes are data members or variables that define the properties of a class. In the context of a 'Human' class, attributes could include age, name, or surname. These are specific to each instance of the class, representing unique characteristics of an object. In the video, the instructor uses examples like int age, string name, and string surname to define the attributes of the Human class.

💡Methods

Methods, also called member functions, are actions or behaviors associated with a class. In C++, they are functions defined inside the class. For example, in the 'Human' class, methods like 'walk' and 'drive' represent actions that a human can perform. The instructor demonstrates how methods are written within a class and are invoked using an instance of that class.

💡Public

Public is one of the access modifiers in C++ classes that determines the visibility of class members. Public members can be accessed from outside the class. The video highlights the importance of public access in allowing other classes or code to interact with certain methods or attributes of a class, such as accessing the age of a human or invoking the walk method.

💡Private

Private is another access modifier in C++, which restricts access to class members. Private members can only be accessed from within the class itself, not from outside. The instructor explains that just as humans have private parts that are not shown to everyone, classes can have private sections, which are hidden from external access.

💡Object

An object is an instance of a class, created based on the class's blueprint. In the video, the instructor refers to 'John' as an object of the Human class. The object holds the actual data for its attributes (like age = 24 for John) and can perform actions using the methods defined in the class (like John.walk()).

💡Constructor

A constructor is a special method in a class that is automatically called when an object is created. While not covered in detail, the video hints at the importance of constructors in initializing class objects. Constructors are typically used to set default values or perform setup actions for an object when it is instantiated.

💡Encapsulation

Encapsulation is the concept of bundling data (attributes) and methods (functions) that operate on the data into a single unit, which is the class. The video emphasizes this idea by showing how a class can hide its internal data (using private attributes) while exposing certain functionalities (public methods) to the outside world.

💡Inheritance

Inheritance is an advanced object-oriented concept where a class (child class) inherits properties and behaviors (attributes and methods) from another class (parent class). Though inheritance is mentioned briefly in the video as a topic for future videos, the instructor refers to the concept when talking about classes being 'extended' with new features.

💡Access Modifiers

Access modifiers control the visibility of class members in C++. The three main access modifiers are public, private, and protected. In the video, the instructor explains how public members are visible to everyone, private members are hidden, and protected members are reserved for derived classes. These modifiers help implement encapsulation and control how data and functions are accessed.

Highlights

Introduction to C++ classes: a class is a structure in a C++ file that holds data.

The process of creating a class in C++ involves either adding a new item or right-clicking the project to add a class.

Classes represent real-world objects or conceptual things inside a program, allowing you to work with them efficiently.

Attributes (e.g., age, name, surname) and methods (e.g., walk, drive) are two core components of a class.

The difference between functions and methods: inside a class, functions are called methods, which can interact with class attributes.

Use of access modifiers in C++: public, private, and protected segments control visibility and access to class members.

Private members are hidden from outside classes, and public members are accessible, which allows control over class structure and behavior.

Objects are instances of classes, and creating an object (e.g., John) from a class (e.g., Human) enables access to class attributes and methods.

Object initialization involves creating variables using class types, similar to creating data types like integers.

C++ classes use the dot operator to access attributes and call methods on objects.

Access modifiers and their importance: public methods are accessible everywhere, private parts are hidden, and protected parts can only be accessed by child classes or friends.

Basic concepts of inheritance and friend functions in classes: topics to be covered in future tutorials.

Introduction to encapsulation: keeping class attributes private to prevent outside access, ensuring better data security and structure.

Classes are templates for creating objects, but the class itself does not hold any data until an object is instantiated.

Final remarks on the importance of C++ classes for representing real-world concepts and the possibilities they provide in programming.

Transcripts

play00:00

hey guys and gals this mean your ex and

play00:02

welcome to intermediate C++ programming

play00:05

tutorial so in today's video we'll be

play00:08

talking a little bit about classes and

play00:10

what they are and what they were present

play00:12

and how we can work with them the

play00:14

entirety of it okay so before we get

play00:17

right into it

play00:20

let's just open our project up and I

play00:22

want to show you something so the way

play00:24

you create a class and basically what a

play00:27

class is it's just a header on a C++

play00:30

file that's all it is it's a structure

play00:33

that data is stored in no special

play00:36

structure that you make so the way to

play00:39

create a class is either you go by add

play00:42

new item added 800 CPP have them have

play00:47

the same name or easier than that just

play00:50

right-click on any of these folders or

play00:52

the project and add new item or add

play00:57

class down below and then you can just

play00:59

click on this class name it here or

play01:02

don't it doesn't matter and then you can

play01:04

name it here as well so I can name it

play01:07

something like our first class and as

play01:12

you can see I'm doing an in camel case

play01:15

weight but the camel case is a bit

play01:16

different I'm using capital in the first

play01:19

letter now there is a base class here

play01:22

that and we don't give a damn about

play01:23

there's a virtual destructure and inline

play01:26

thing you also don't care about that so

play01:29

what I'm going to say in line there's no

play01:31

CPP there shoulders structure is for you

play01:33

know the ones that usually have usually

play01:37

are to be inherited from but we don't we

play01:39

don't care about that so just yet and

play01:42

when you do that you can then this tab

play01:44

gets open and you know you can do

play01:46

whatever you want we still don't care

play01:48

about that all right so we have the name

play01:51

about name of our class here let me hit

play01:54

OK as you can see I have two files but

play01:57

if I try to build my application I'm

play01:59

definitely going to get an error the

play02:01

reason to that is I don't have an entry

play02:03

point as you can see come on dude just

play02:07

doing two files so as you can see it's

play02:10

giving me the error that saying entry

play02:12

point must be defined so

play02:13

I'm just gonna go ahead and create yet

play02:15

another C++ file source let CP be big

play02:19

enough and do void main or intimate

play02:22

doesn't doesn't really matter so just

play02:27

some simple like that all right run

play02:29

it as you can see one succeeded no all

play02:32

good all dandy so now let's get into our

play02:35

class I'm just gonna do it I'm sorry

play02:38

double tap like this right here I'm

play02:42

sorry for the coughing I've caught a

play02:43

cold so yeah right so this is our class

play02:48

this is the entirety of our class now a

play02:51

class is nothing but a representation of

play02:55

something that exists in the real world

play02:58

so whatever we want to represent

play03:00

something that exists in the real world

play03:02

inside our program we use a class either

play03:06

something that exists in the real world

play03:08

or a conceptual thing all right so our

play03:12

first class I'm just gonna go ahead and

play03:14

delete this and this and then I'm going

play03:17

to be renaming this our first class

play03:19

class so the way you create a class is

play03:24

quite simple all you need to do is write

play03:27

class and then now there are two ways of

play03:30

course you can use a snippet class tab

play03:33

and it'll generate the entire thing for

play03:35

you or we can just write a name I'm

play03:37

going to write human class human so

play03:42

human is a is an ideal thing in our

play03:47

world we are all humans we have

play03:49

different attributes but we are all

play03:51

human right now every class can have two

play03:57

things classes can have attributes and

play04:01

members they can have I'm sorry

play04:03

attribute some methods so attributes are

play04:07

what define us okay like eye color age

play04:10

or you know hair color or nationality or

play04:13

whatever the hell and methods or what we

play04:16

are capable of doing driving a car for

play04:20

for instance so I can have an attribute

play04:23

let's say I have an int named H

play04:27

just like that I have another 8 or

play04:29

rather a yeah it's why not I have

play04:32

another aunts name mm-hmm what do I have

play04:36

yeah let's say I have a C type string

play04:40

name has a C type string surname another

play04:44

C type string I don't know that's a SAN

play04:47

whatever the hell

play04:48

alright so H name surname that's the son

play04:51

whatever you can think of so these are

play04:53

what I have these are different every

play04:56

human has these all right unless you

play04:59

live and like you know in jungle or

play05:02

something you don't really have SSN then

play05:05

but let's just actually do you know take

play05:08

that out so every human has an age every

play05:11

human has a name every human has a

play05:13

surname that's that's a general thing

play05:15

everybody has this name the entire world

play05:18

Oh No one way or another they all have

play05:22

these they all have things that can be

play05:23

placed into these places or can be

play05:27

defined as name and surname now this is

play05:29

a basic human all right this is a schema

play05:32

for the human all right

play05:33

but it's not any human now we will get

play05:39

to that sound so it's just a scheme for

play05:40

a human or any human that you want to

play05:42

have will be obeying this structure this

play05:46

this data structure now it can also the

play05:49

class can also have functions or methods

play05:53

rather so functions outside a class are

play05:57

called functions is an important note so

play06:00

this is a function but inside the class

play06:02

we call them methods all right so method

play06:04

is here function is there an attribute

play06:07

is here all right so these are

play06:09

attributes these guys inside this guy is

play06:12

method or member functions all right so

play06:18

they're they're either called methods or

play06:20

member functions and and we're going to

play06:21

learn about you know pointers to members

play06:23

functions and pointers to functions and

play06:25

all that good stuff

play06:26

okay so methods now every human can also

play06:30

do things all right for instance we can

play06:35

do something like what every human can

play06:39

walk

play06:39

void let's say drive now every human can

play06:44

learn to drive

play06:45

alright so every human can walk every

play06:47

human can drive a sexually returned a

play06:48

bull to see if driving was successful if

play06:51

it was successful I will return the

play06:53

truth or if walking is successful if

play06:56

you're like one-year-old you probably

play06:57

can't walk alright

play06:59

so easy peasy just like that we create a

play07:02

class it has attributes

play07:04

it has functions no methods or member

play07:07

functions now the way you create a

play07:10

function as you can see it's already

play07:12

empty alright so you can either do it

play07:14

like this or in a better way you can

play07:17

bring the definition to the CPP class so

play07:21

the dot H classes as usual for

play07:23

declaration and the dot CPP is for

play07:26

definition the way you do that turn two

play07:28

ways the easy way and the hard way

play07:30

Rho the kind of hard right son sorry

play07:33

it's not really that hard so what you do

play07:36

that is you first include this guy you

play07:38

have to and then you do return type name

play07:43

of the function just how you do it a

play07:46

normal way but the difference is you

play07:49

need to add the name of the class with

play07:51

two columns before the name of the

play07:53

function and that's it there you have it

play07:55

you have your entire thing up and

play07:58

running as you can see the green zigzag

play08:02

line is gone it's no longer there it's

play08:05

still here because we haven't

play08:06

implemented drive yet but I yeah so

play08:08

let's just return true just to avoid any

play08:11

errors now that was the slightly harder

play08:15

way which is really not that hard

play08:17

the easier way if you're using visual

play08:19

studio is to just hold ctrl press dot

play08:23

and then enter and there you have it

play08:25

already there for you and this is how

play08:29

you create functions and then thing you

play08:31

can call those functions which we will

play08:32

get to pretty soon right but before we

play08:34

get to any functions what I want to talk

play08:36

to you about or three things all right

play08:39

private protect it

play08:45

and public all right so these are

play08:49

sections or rather segments of our class

play08:53

that we determine what to be Oh

play08:56

should they be visible - or whose whom

play09:00

should they be visible - for instance

play09:02

every class has a public section or a

play09:05

you know yeah public section has a

play09:07

protected section in the private section

play09:08

just like us humans who have public

play09:11

parts and private parts we don't really

play09:13

show our private parts to any everybody

play09:15

you only show them to friends which

play09:18

we'll talk to well which we will talk

play09:21

about pretty soon you can use the

play09:23

keyword friend I've used it so in

play09:26

several of my projects not really I

play09:28

don't really I'm not really a fond of it

play09:30

I'm not really fond of using that

play09:32

keyword but you got to do what you got

play09:34

to do boy so yeah anyhow public section

play09:38

we have public we have private and we

play09:40

have you know protect it so protected

play09:45

right now the access these determine who

play09:50

can access whatever comes under it all

play09:52

right so all these guys all of these

play09:55

guys are private I'm sorry I'm public

play09:58

because they're determined they're

play10:00

defined under a public segment if they

play10:04

were to take these guys out put them

play10:06

here which is a part of my standard

play10:08

public should always come after private

play10:10

now everybody in C sharp everybody who

play10:12

has coding in C sharp is like yeah we

play10:14

determine the privates first then public

play10:17

comes but honestly if someone is reading

play10:20

your code didn't really give a damn

play10:21

about your private things in the class

play10:23

no they really don't I want to see what

play10:25

they can access and we'll get there

play10:27

don't worry about it

play10:28

so public is what others can see and

play10:33

what others means I mean other classes

play10:36

than human right outside of human what

play10:39

can see these everybody because it's

play10:41

public what the children can see now I'm

play10:47

just gonna hold pull this star here

play10:49

we're gonna talk about this guy any

play10:50

inheritance don't worry about it

play10:52

this is a to do video alright this is

play10:54

not not not for now this is for later

play10:56

and then there's the private part

play10:59

what no one can see alright nobody

play11:02

nobody's gonna see your private parts

play11:04

right almost our friend France can see

play11:09

which we will talk about in another

play11:11

video not just yet so this will be for

play11:14

another video as well as a to do to do

play11:18

video alright so friends or Frankie wart

play11:22

is also a to do video children is for a

play11:28

to do videos for the inheritance so this

play11:30

guy it's gonna be for the front Huard or

play11:33

rather friend keyboard and this guy is

play11:36

gonna be for in hairy tendons topic

play11:41

alright so no it's not like that alright

play11:44

so these are the these are what we call

play11:46

access modifiers and by default classes

play11:51

don't have access modifier this guy

play11:54

right here means it's private so if you

play11:56

start a class by private you basically

play11:58

I've done nothing to change anything

play12:00

by default whatever you put under this

play12:03

guy right here means it's private

play12:05

yeah just like that easy peasy alright

play12:09

so this was a super basic introduction

play12:13

to class right we will be talking about

play12:14

constructors destructors

play12:16

copy constructors semantics

play12:20

you know operator overloading you know

play12:22

all that goes to the rule of all the BS

play12:24

rule of 3 or 5 rai you know all that

play12:28

good stuff

play12:29

soon enough not just yet all I want you

play12:32

to know is that you have classes all

play12:36

right in classes represent things that

play12:38

we think that are human or I mean death

play12:41

damned things that can be found in the

play12:44

real world

play12:44

alright so if you find the rock you can

play12:46

probably make a class of rock like this

play12:49

so plus rock I'm not doing Johnson I'm

play12:53

sorry

play12:53

so we have public public thank you any

play12:58

problem has like unit weight and another

play13:01

and maybe yes I don't know all right if

play13:06

you really do

play13:08

physics yeah you can do something like

play13:10

that but it doesn't really matter and um

play13:12

yeah we'll get to encapsulation where we

play13:16

talk about that these guys these members

play13:18

they should always all the damn time be

play13:22

on a private just don't don't worry

play13:24

about it all right

play13:25

so there was a reason I put them here

play13:27

okay so public protected private you

play13:30

know even just like us humans this human

play13:33

also has public and private parts and we

play13:35

put whatever reason Friday first like

play13:39

like the length Heights yeah yeah I just

play13:44

wrote that more simpler hi all right I

play13:48

just private just doesn't a length of

play13:50

thing length of the height so yeah

play13:54

whatever so yeah we have privates we

play13:57

have protected and protected in public

play13:59

we I think I'm over explained it in a

play14:02

lot alright so how the hell do we use a

play14:05

class first off we need to include it we

play14:08

need to include the header that is

play14:09

defined in so just like I included

play14:13

something like string which is also kind

play14:16

of a class all right those string is

play14:20

this kind if a class is a kind of a

play14:22

Microsoft class see it's it's a using

play14:25

basis string and basis change of peek it

play14:27

you know class spaces string so yeah

play14:29

it's kind of a class all right now it's

play14:31

a very different kind of class the

play14:33

template passed but we don't worry about

play14:35

that but yeah so string is basically a

play14:38

class alright and just as we use the

play14:41

header to use a class we include our

play14:45

first header I'm sorry our first header

play14:48

like this to be able to use whatever is

play14:52

inside it all right easy peasy quite you

play14:56

know simple now the way we use it is if

play14:59

you remembered if you remember we want

play15:01

to create variables let's say I want to

play15:03

create an int I say int I equals five

play15:06

all right simple easy peasy or just

play15:09

easier than that and I I have created an

play15:12

integer after I I mean I'm half the

play15:15

neighbor the name I all right create an

play15:17

integer named I simple and easy

play15:21

same thing with a class so we create a

play15:25

class you are creating nothing more than

play15:27

a data type this guy right here is

play15:31

nothing more than a goddamn data type so

play15:34

I can just say human age and that's it

play15:37

and I hum an H all we have to do is that

play15:41

all right we don't care about this guy

play15:43

right here we don't care about the equal

play15:45

or the assignments I don't care about

play15:48

the initialization just yet we will talk

play15:51

about that in another video for now all

play15:53

we care about is this guy

play15:55

all right so human H whatever and I fold

play15:59

F so just as we create some of these

play16:01

guys we can create one of these guys and

play16:04

do you make notes classes starts with or

play16:08

at least in Maya

play16:09

standard classes start with a capital

play16:11

letter camel case and capital letters

play16:14

and that's how we create a class now how

play16:17

do we use a class so as you may have

play16:21

already noticed classes have methods and

play16:24

members now just for the sake of this

play16:26

tutorial I want to put them here okay

play16:28

just to make it simpler and I'm gonna

play16:31

take out wall a drive away so it's just

play16:34

gonna have an age and it's just gonna

play16:36

have one method walk all right

play16:40

one method one member and this guy's

play16:43

gonna return the ball so the way you do

play16:46

it is hit H dot and with the dot

play16:50

operator you can see these two guys

play16:52

right here and yes dot is an operator so

play16:56

H dot you can do H equals ten now eight

play17:00

age in our well it's bad naming so let's

play17:04

just do human with capital e so let's

play17:07

just make it a better one

play17:08

so John all right John today H is like

play17:13

24 all right John is 24 years old John

play17:18

can also walk simple as that

play17:23

so this is how we assign data to this

play17:27

guy and this is how we use functions and

play17:29

will rather call functions we can also

play17:31

use that so I'll just say anti or rather

play17:34

but I already have my so our equals John

play17:37

dot H I'm sorry

play17:41

quite simple quite easy so I'm just

play17:43

using an attribute inside John all right

play17:47

that came out pretty bad and that I can

play17:49

call a function or a rather member

play17:53

function within the class of John within

play17:55

the class of human but an instance of a

play17:58

human plat of our human class John okay

play18:02

so here's the thing that a lot of people

play18:06

make mistakes when they're first

play18:07

learning about classes so there are two

play18:08

things there is a class and there's an

play18:10

object all right this guy right here is

play18:14

what we call a class it's like an empty

play18:16

form that we need to fill in okay it's

play18:19

like a scheme it's like a blueprint like

play18:22

okay if you want to have a human you're

play18:24

human needs to have these two right

play18:26

that's basically it

play18:27

now I want to create a human so I create

play18:30

a human and the name of the human is

play18:32

John now John is an object of the class

play18:36

type human just like int is an object of

play18:38

type int well not really but they are

play18:42

kind of the same thing all right I'm

play18:44

sorry if I say like this these are

play18:46

teaching mistakes I have to make them

play18:48

just so you can understand it easier but

play18:51

I don't worry about it

play18:52

they're all just teaching mistakes I am

play18:54

intentionally sometimes making mistakes

play18:56

I like in the case of pointers the key

play18:58

thing is it's not really a bug in the

play19:00

box but I explain it like that so you

play19:02

understand it better any help so this is

play19:06

a class this is an object all right a

play19:09

class you can't really you can't really

play19:11

do something like this just yeah

play19:13

generally do dot dot walk it's just

play19:16

gonna give an error you can't even do

play19:17

something like that it's still gonna

play19:19

give you an error there are reason to it

play19:21

there's a reason to it and I will talk

play19:23

to you about that a bit more is a gap we

play19:26

will talk about that operator soon

play19:28

enough not just yet so yeah this is our

play19:32

object this is a class quite easy quite

play19:35

simple and the way we initialize classes

play19:37

is kind of different couldn't do

play19:40

something like this and then we have

play19:44

initialized our class

play19:46

so if I just run it I'm not gonna posit

play19:49

or anything you can just see that it

play19:50

builds correctly and it goes through

play19:52

with ease come on dude thank you my dude

play19:58

oh there we go

play20:00

so as you can see there is no error

play20:01

there everything is good everything is

play20:03

easy we just have created a value type

play20:06

of John you're gonna use that pretty

play20:09

goddamn clean all right and that's

play20:13

basically it for this video I hope you

play20:16

understood what classes are all about

play20:18

pretty goddamn easy and simple they're

play20:21

just a representation of something in

play20:24

the real world or some conceptual thing

play20:27

that we want to talk about or rather we

play20:29

want to bring in our code like say if we

play20:32

have an object class and that object

play20:35

class has a math or I'm sorry in mass or

play20:39

a density integer or a calculate density

play20:43

function or whatever so possibilities

play20:46

are endless if we can't imagine it you

play20:49

can put it in a class simple as that and

play20:51

you can just represent it like this

play20:53

anyhow that's been it for this video

play20:56

guys I hope you enjoyed if you have any

play20:57

questions do ask them in the comments

play21:00

below if you have if you enjoyed this

play21:03

video do leave a like and maybe even

play21:05

subscribe that would help a lot thank

play21:07

you very much I will see you in the next

play21:09

one

Rate This

5.0 / 5 (0 votes)

相关标签
C++ tutorialObject-oriented programmingClasses and objectsCoding basicsMethods and attributesProgramming tipsAccess modifiersBeginner codingVisual StudioCode structure
您是否需要英文摘要?