DEV Community

Cover image for How To Get Started With Classes And Objects in C++
Natalia
Natalia

Posted on

How To Get Started With Classes And Objects in C++

For a few days I've been studying classes in C++, here's what I've learned!

Classes are user-defined types. It means that the user can create a new type for their desired things. An Object is a variable, that can store those desired things.

For example, if we want to create a variable that stores numbers, we would create type int (because it stores numbers) and then variable called numbers, so our code would look like this:

Alt Text

Now when we have those basics covered let's start with learning about classes.

How To Create A Class?

Now, to create a class, we can use two keywords: class or struct. We're going to use the keyword class. To create a class, first, we have to think about what do we want to store there. For example, let's create a class named Home.

Alt Text

We now officially created a class! Of course, it does nothing for now, but it is a class.

What's a Class Member?

You can think of it like a real class member. In every class (in school) there are at least a few class members. So, let's go ahead and create them! We're gonna add class member named household. You can add something else if you want i.e. car or even a pet!

Alt Text
int household; This part of the code is also called an Attribute.

Now our class Home has its first class member/attribute household! Let's keep going.

Access specifiers

Of course classes in our projects won't have only one class member. When we want to create a lot of attributes but we don't want the user to see all of it (i.e. at the ATM we don't want to know our password before manually typing it in), we then use control operators or access specifiers, call it as you like.

There are three types of access specifiers:

  • public Everyone has access

  • private Only the class itself and friends have access

  • protected Only the class itself, derived classes and friends have access

In this article, we won't touch the protected specifier but know that it does exist.

Okay, now we know that we have access specifiers, but how do we use them in our code?

Alt Text

Now, all the functions we added after saying public: are public, meaning, anyone can access that information. Okay, now you should know that the code we add after access specifier is called method. So in our method, we created two variables, num (is type int so it stores numbers) and text (which stores sequents of characters).

Now let's rewind for a second and visually pictured what we've learned so far:

Alt Text

What about Objects?

Well, the title is about class and objects so let's get into it.

Objects are, like the name says, objects, like house, car, bike etc. We can add many objects to our class.They go after the class curly bracket ends.

To create an object we first, have to say to which class this object belongs to followed by the name of our object:

Alt Text

Now when we want to access the attributes we created before we can do this:

Alt Text

And if we want to print out our object with its attributes, you can simply use cout for that.

Alt Text

This is it! Of course, there are more details to it but for starting out its essential to have the basics covered. This is my first article! So tips, advises etc. are appreciated. Thank you for reading and I hope you found it helpful!

Top comments (1)

Collapse
 
dimas_1307 profile image
Dima Arhipov

I think that coffee is very popular among people. And a lot of people drink it in the morning. So you need to make it quick. So go and check top 10 instant coffee