How To Get Started With Classes And Objects in C++
Tags: cpp, beginners
Published: 2020-08-31 17:35:23

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](https://dev-to-uploads.s3.amazonaws.com/i/7tt0jdsjyv3ci1py4ua8.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/in8hyqhag0p35eo5vibd.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/8tmcjeu4vtbce5nc38go.png)
`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](https://dev-to-uploads.s3.amazonaws.com/i/jdw5026do3itvowm29p5.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/efi2xyykt856i2bctr56.png)

# 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](https://dev-to-uploads.s3.amazonaws.com/i/fzvfvaz1jzyueb0ial2j.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vcch0641jthdhi7hjrbn.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ysuq1d3wzdu26sl4j4ib.png)



**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!