01 - [Classes] C++ Intermediate Programming Tutorial
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
🛠️ 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.
👤 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).
🔒 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.
📋 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.
🧠 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
💡Attributes
💡Methods
💡Public
💡Private
💡Object
💡Constructor
💡Encapsulation
💡Inheritance
💡Access Modifiers
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
hey guys and gals this mean your ex and
welcome to intermediate C++ programming
tutorial so in today's video we'll be
talking a little bit about classes and
what they are and what they were present
and how we can work with them the
entirety of it okay so before we get
right into it
let's just open our project up and I
want to show you something so the way
you create a class and basically what a
class is it's just a header on a C++
file that's all it is it's a structure
that data is stored in no special
structure that you make so the way to
create a class is either you go by add
new item added 800 CPP have them have
the same name or easier than that just
right-click on any of these folders or
the project and add new item or add
class down below and then you can just
click on this class name it here or
don't it doesn't matter and then you can
name it here as well so I can name it
something like our first class and as
you can see I'm doing an in camel case
weight but the camel case is a bit
different I'm using capital in the first
letter now there is a base class here
that and we don't give a damn about
there's a virtual destructure and inline
thing you also don't care about that so
what I'm going to say in line there's no
CPP there shoulders structure is for you
know the ones that usually have usually
are to be inherited from but we don't we
don't care about that so just yet and
when you do that you can then this tab
gets open and you know you can do
whatever you want we still don't care
about that all right so we have the name
about name of our class here let me hit
OK as you can see I have two files but
if I try to build my application I'm
definitely going to get an error the
reason to that is I don't have an entry
point as you can see come on dude just
doing two files so as you can see it's
giving me the error that saying entry
point must be defined so
I'm just gonna go ahead and create yet
another C++ file source let CP be big
enough and do void main or intimate
doesn't doesn't really matter so just
some simple like that all right run
it as you can see one succeeded no all
good all dandy so now let's get into our
class I'm just gonna do it I'm sorry
double tap like this right here I'm
sorry for the coughing I've caught a
cold so yeah right so this is our class
this is the entirety of our class now a
class is nothing but a representation of
something that exists in the real world
so whatever we want to represent
something that exists in the real world
inside our program we use a class either
something that exists in the real world
or a conceptual thing all right so our
first class I'm just gonna go ahead and
delete this and this and then I'm going
to be renaming this our first class
class so the way you create a class is
quite simple all you need to do is write
class and then now there are two ways of
course you can use a snippet class tab
and it'll generate the entire thing for
you or we can just write a name I'm
going to write human class human so
human is a is an ideal thing in our
world we are all humans we have
different attributes but we are all
human right now every class can have two
things classes can have attributes and
members they can have I'm sorry
attribute some methods so attributes are
what define us okay like eye color age
or you know hair color or nationality or
whatever the hell and methods or what we
are capable of doing driving a car for
for instance so I can have an attribute
let's say I have an int named H
just like that I have another 8 or
rather a yeah it's why not I have
another aunts name mm-hmm what do I have
yeah let's say I have a C type string
name has a C type string surname another
C type string I don't know that's a SAN
whatever the hell
alright so H name surname that's the son
whatever you can think of so these are
what I have these are different every
human has these all right unless you
live and like you know in jungle or
something you don't really have SSN then
but let's just actually do you know take
that out so every human has an age every
human has a name every human has a
surname that's that's a general thing
everybody has this name the entire world
Oh No one way or another they all have
these they all have things that can be
placed into these places or can be
defined as name and surname now this is
a basic human all right this is a schema
for the human all right
but it's not any human now we will get
to that sound so it's just a scheme for
a human or any human that you want to
have will be obeying this structure this
this data structure now it can also the
class can also have functions or methods
rather so functions outside a class are
called functions is an important note so
this is a function but inside the class
we call them methods all right so method
is here function is there an attribute
is here all right so these are
attributes these guys inside this guy is
method or member functions all right so
they're they're either called methods or
member functions and and we're going to
learn about you know pointers to members
functions and pointers to functions and
all that good stuff
okay so methods now every human can also
do things all right for instance we can
do something like what every human can
walk
void let's say drive now every human can
learn to drive
alright so every human can walk every
human can drive a sexually returned a
bull to see if driving was successful if
it was successful I will return the
truth or if walking is successful if
you're like one-year-old you probably
can't walk alright
so easy peasy just like that we create a
class it has attributes
it has functions no methods or member
functions now the way you create a
function as you can see it's already
empty alright so you can either do it
like this or in a better way you can
bring the definition to the CPP class so
the dot H classes as usual for
declaration and the dot CPP is for
definition the way you do that turn two
ways the easy way and the hard way
Rho the kind of hard right son sorry
it's not really that hard so what you do
that is you first include this guy you
have to and then you do return type name
of the function just how you do it a
normal way but the difference is you
need to add the name of the class with
two columns before the name of the
function and that's it there you have it
you have your entire thing up and
running as you can see the green zigzag
line is gone it's no longer there it's
still here because we haven't
implemented drive yet but I yeah so
let's just return true just to avoid any
errors now that was the slightly harder
way which is really not that hard
the easier way if you're using visual
studio is to just hold ctrl press dot
and then enter and there you have it
already there for you and this is how
you create functions and then thing you
can call those functions which we will
get to pretty soon right but before we
get to any functions what I want to talk
to you about or three things all right
private protect it
and public all right so these are
sections or rather segments of our class
that we determine what to be Oh
should they be visible - or whose whom
should they be visible - for instance
every class has a public section or a
you know yeah public section has a
protected section in the private section
just like us humans who have public
parts and private parts we don't really
show our private parts to any everybody
you only show them to friends which
we'll talk to well which we will talk
about pretty soon you can use the
keyword friend I've used it so in
several of my projects not really I
don't really I'm not really a fond of it
I'm not really fond of using that
keyword but you got to do what you got
to do boy so yeah anyhow public section
we have public we have private and we
have you know protect it so protected
right now the access these determine who
can access whatever comes under it all
right so all these guys all of these
guys are private I'm sorry I'm public
because they're determined they're
defined under a public segment if they
were to take these guys out put them
here which is a part of my standard
public should always come after private
now everybody in C sharp everybody who
has coding in C sharp is like yeah we
determine the privates first then public
comes but honestly if someone is reading
your code didn't really give a damn
about your private things in the class
no they really don't I want to see what
they can access and we'll get there
don't worry about it
so public is what others can see and
what others means I mean other classes
than human right outside of human what
can see these everybody because it's
public what the children can see now I'm
just gonna hold pull this star here
we're gonna talk about this guy any
inheritance don't worry about it
this is a to do video alright this is
not not not for now this is for later
and then there's the private part
what no one can see alright nobody
nobody's gonna see your private parts
right almost our friend France can see
which we will talk about in another
video not just yet so this will be for
another video as well as a to do to do
video alright so friends or Frankie wart
is also a to do video children is for a
to do videos for the inheritance so this
guy it's gonna be for the front Huard or
rather friend keyboard and this guy is
gonna be for in hairy tendons topic
alright so no it's not like that alright
so these are the these are what we call
access modifiers and by default classes
don't have access modifier this guy
right here means it's private so if you
start a class by private you basically
I've done nothing to change anything
by default whatever you put under this
guy right here means it's private
yeah just like that easy peasy alright
so this was a super basic introduction
to class right we will be talking about
constructors destructors
copy constructors semantics
you know operator overloading you know
all that goes to the rule of all the BS
rule of 3 or 5 rai you know all that
good stuff
soon enough not just yet all I want you
to know is that you have classes all
right in classes represent things that
we think that are human or I mean death
damned things that can be found in the
real world
alright so if you find the rock you can
probably make a class of rock like this
so plus rock I'm not doing Johnson I'm
sorry
so we have public public thank you any
problem has like unit weight and another
and maybe yes I don't know all right if
you really do
physics yeah you can do something like
that but it doesn't really matter and um
yeah we'll get to encapsulation where we
talk about that these guys these members
they should always all the damn time be
on a private just don't don't worry
about it all right
so there was a reason I put them here
okay so public protected private you
know even just like us humans this human
also has public and private parts and we
put whatever reason Friday first like
like the length Heights yeah yeah I just
wrote that more simpler hi all right I
just private just doesn't a length of
thing length of the height so yeah
whatever so yeah we have privates we
have protected and protected in public
we I think I'm over explained it in a
lot alright so how the hell do we use a
class first off we need to include it we
need to include the header that is
defined in so just like I included
something like string which is also kind
of a class all right those string is
this kind if a class is a kind of a
Microsoft class see it's it's a using
basis string and basis change of peek it
you know class spaces string so yeah
it's kind of a class all right now it's
a very different kind of class the
template passed but we don't worry about
that but yeah so string is basically a
class alright and just as we use the
header to use a class we include our
first header I'm sorry our first header
like this to be able to use whatever is
inside it all right easy peasy quite you
know simple now the way we use it is if
you remembered if you remember we want
to create variables let's say I want to
create an int I say int I equals five
all right simple easy peasy or just
easier than that and I I have created an
integer after I I mean I'm half the
neighbor the name I all right create an
integer named I simple and easy
same thing with a class so we create a
class you are creating nothing more than
a data type this guy right here is
nothing more than a goddamn data type so
I can just say human age and that's it
and I hum an H all we have to do is that
all right we don't care about this guy
right here we don't care about the equal
or the assignments I don't care about
the initialization just yet we will talk
about that in another video for now all
we care about is this guy
all right so human H whatever and I fold
F so just as we create some of these
guys we can create one of these guys and
do you make notes classes starts with or
at least in Maya
standard classes start with a capital
letter camel case and capital letters
and that's how we create a class now how
do we use a class so as you may have
already noticed classes have methods and
members now just for the sake of this
tutorial I want to put them here okay
just to make it simpler and I'm gonna
take out wall a drive away so it's just
gonna have an age and it's just gonna
have one method walk all right
one method one member and this guy's
gonna return the ball so the way you do
it is hit H dot and with the dot
operator you can see these two guys
right here and yes dot is an operator so
H dot you can do H equals ten now eight
age in our well it's bad naming so let's
just do human with capital e so let's
just make it a better one
so John all right John today H is like
24 all right John is 24 years old John
can also walk simple as that
so this is how we assign data to this
guy and this is how we use functions and
will rather call functions we can also
use that so I'll just say anti or rather
but I already have my so our equals John
dot H I'm sorry
quite simple quite easy so I'm just
using an attribute inside John all right
that came out pretty bad and that I can
call a function or a rather member
function within the class of John within
the class of human but an instance of a
human plat of our human class John okay
so here's the thing that a lot of people
make mistakes when they're first
learning about classes so there are two
things there is a class and there's an
object all right this guy right here is
what we call a class it's like an empty
form that we need to fill in okay it's
like a scheme it's like a blueprint like
okay if you want to have a human you're
human needs to have these two right
that's basically it
now I want to create a human so I create
a human and the name of the human is
John now John is an object of the class
type human just like int is an object of
type int well not really but they are
kind of the same thing all right I'm
sorry if I say like this these are
teaching mistakes I have to make them
just so you can understand it easier but
I don't worry about it
they're all just teaching mistakes I am
intentionally sometimes making mistakes
I like in the case of pointers the key
thing is it's not really a bug in the
box but I explain it like that so you
understand it better any help so this is
a class this is an object all right a
class you can't really you can't really
do something like this just yeah
generally do dot dot walk it's just
gonna give an error you can't even do
something like that it's still gonna
give you an error there are reason to it
there's a reason to it and I will talk
to you about that a bit more is a gap we
will talk about that operator soon
enough not just yet so yeah this is our
object this is a class quite easy quite
simple and the way we initialize classes
is kind of different couldn't do
something like this and then we have
initialized our class
so if I just run it I'm not gonna posit
or anything you can just see that it
builds correctly and it goes through
with ease come on dude thank you my dude
oh there we go
so as you can see there is no error
there everything is good everything is
easy we just have created a value type
of John you're gonna use that pretty
goddamn clean all right and that's
basically it for this video I hope you
understood what classes are all about
pretty goddamn easy and simple they're
just a representation of something in
the real world or some conceptual thing
that we want to talk about or rather we
want to bring in our code like say if we
have an object class and that object
class has a math or I'm sorry in mass or
a density integer or a calculate density
function or whatever so possibilities
are endless if we can't imagine it you
can put it in a class simple as that and
you can just represent it like this
anyhow that's been it for this video
guys I hope you enjoyed if you have any
questions do ask them in the comments
below if you have if you enjoyed this
video do leave a like and maybe even
subscribe that would help a lot thank
you very much I will see you in the next
one
浏览更多相关视频
Classes and Objects in Python | OOP in Python | Python for Beginners #lec85
Java Tutorial: Access modifiers, getters & setters in Java
Object Oriented Programming Features Part 3 | C ++ Tutorial | Mr. Kishore
Python OOP Tutorial 1: Classes and Instances
Class Methods in Python | How to add Methods in Class | Python Tutorials for Beginners #lec87
Overloading dan Overriding Kotlin
5.0 / 5 (0 votes)