Intro To C++/Objects

From Wikibooks, open books for an open world
Jump to: navigation, search

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:

class Book
{
    public:
        int Copies;
 
    private:
        string Name;
        string Author;
        int Pages;
}

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.

class Book
{
    public:
        int Copies;
 
        void editName(string inName) //this will edit the Name value while staying inside the class. Keep these kinds of functions inside public.
        {
            Name = inName;
        }
 
        void editAuthor(string inAuthor) //this will edit the Author value while staying inside the class.
        {
            Author = inAuthor;
        }
 
 
        void editPages(int inPages) //this will edit the Pages value while staying inside the class.
        {
            Pages = inPages;
        }
 
    private:
        string Name;
        string Author;
        int Pages;
}

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

int main()
{
    Book Pre_cal; //Creates a new instance of the class Book, with the variable named being called Pre_cal
    Pre_cal.Copies = 1000;//since Copies is public, we can just edit the value directly, without having to create/call a method to change it, although you may still do that.
    Pre_cal.editName("Precalculus with Trigonometry");//To use the method editName, we use this line, with the string argument as the name of the book
    Pre_cal.editAuthor("Paul A. Foerster");//same concept, uses the method editAuthor to change the value of Author
    Pre_Cal.editPages(848);//once again, does the same thing, but to Pages
 
    return 0;
}

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:

    public:
        void singlePurchase()
        {
            Copies--; // shorter way of saying Copies = Copies - 1
        }

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 the edit methods:

    Pre_cal.singlePurchase();

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:

    public:
        void singlePurchase()
        {
            if(Copies>0)
                Copies--; // shorter way of saying Copies = Copies - 1
        }

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:

    public:
        string Name()
        {
            return Name;
        }
 
        string Author()
        {
            return Author;
        }
 
        int Pages()
        {
            return Pages;
        }

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

int main()
{
    Book Pre_cal;
    Pre_cal.editPages(848);
    int PagesInMyBook = Pre_cal.Pages();
 
    return 0;
}

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.