Intro To C++/Objects

Since C++ is an object oriented programming language, you're going to be using classes, objects, and methods on a regular basis. The idea behind all of this is creating a "template" for you or other programmers to use without having to continuously define new variables and functions with the same practicality. That would be the reason objects are called objects, they represent an instance of a class. A good example is to use a real-world reference, albeit the fact that it will have no literal meaning to any physical objects. Now my approach to teaching classes and objects is quite different than that of my teachers. They would first teach you about how to use objects, and then teach you how to create a class. But I will do that the other way around, as it would make more sense to have a good foundation on classes, simply because objects depend on classes. Now here is the code to a class that I made:

For the first line, class Book, you are creating a new class called Book. Then inside this code block, there are two key elements, public and private. In total, there are three encapsulation methods, public, private, and protected. Each one is used with a : after it. This is to organize which variables are where. Public means that you are able to edit the variable(s) outside of the class. Private means you are unable to edit the variable(s) outside of the class. Encapsulation will be gone over later. Inside each different encapsulation, there are an assorted amount of variables, with varying types. Notice each one's location, Copies is located under public, while Name, Author, and Pages are located under private. Since you can't edit private data outside the class, we need to create methods to change the values. The way that is done is by creating a new function inside the class. We will create three new methods to change the private data.

Now that we have a basic class set up, lets see how to use it.

Whenever we want to access something inside the class, we follow the variable name with a period, and then the method we want to call. And since the Copies variable inside the Book class is public, we don't have to go around the bush by creating a method to change it. I left it public because usually, people purchase the book, changing the value. Now that doesn't stop us from creating a method to change the value. We could create a method inside the class as following:

So instead of changing it every single time, we just call the method, and it automatically changes the value. To use it, we would just call it how we did the edit methods:

And it's done. Now of course, that doesn't stop the value from going negative. To prevent that, we would simply add this to the method:

Now the value will only change if the value of Copies is greater than 0.

There is one problem, we can change the values of the private variables, but we can't read it. To fix this, we could just do this:

Now you can probably get where the return statement comes in handy. Lets take a look at how its used:

Since we set the value of Pages, Pages now equals 848. So when we return Pages, it returns the value of Pages, which in turn, is used to set the value of PagesInMyBook. So by the end of the program, the value of PagesInMyBook is equal to 848.

This page will be worked on.