Easy Tutorial: Computer Programming for DUMMIES -- object oriented programming 2 (classes)


Image source

Hey guys! This tutorial is about classes. Classes are sort of a continuation of data structures. There is one very important difference between structures and classes. Classes can have their own functions, unlike structures. This is a continuation of my C++ tutorial series. If you want to check out the rest of them, here they are:

Part 1: Hello World!

Part 2: Variables

Part 3: Functions

Part 4: if(), else, else if()

Part 5: Loops

Part 6: Arrays

Part 7: Basic Input/Output

Part 9: Sorting Arrays

Part 10: Random Numbers

Part 11: Colored Text in your Terminal

Part 12: Recursion

Part 13: Binary Search

Part 14: 2D Arrays

Part 15: String Processing

Part 16: Binary, Bitwise Operators, and 2^n

Part 17: Pointers

Part 18: Pointer Arithmetic

Part 19: Object Oriented Programming 1 -- Data Structures <-- definitely check this tutorial out first before you continue on.

Declaring a Class

class name 
{
    private:
        /*    private members    */
    public:
        /*    public members    */
} /*    object names    */;
  • name --> the name of the class
  • private: --> this is where "private" members go -- these can only be accessed by members of the same class
  • public: --> this is where "public" members go -- these can be accessed from anywhere
  • Note: there is another access specifier (other than private and public) called protected
    • protected: --> where "protected" members go -- these can be accessed by members of the same class and by members of derived classes (but this tutorial won't touch on that; I will include that in another tutorial)

Here is a more specific example:

class Employees
{
    private:
        string name;
        int id;
    public:
        void change_name(string);
        void change_id(int);
} fred, sally, billy;
  • Employees --> a class that holds information about different employee objects
  • name --> a string to hold the name for each employee object
  • id --> an integer to hold the IDs of each employee object
  • change_name() --> a function that can change an employee object's name
  • change_id() --> a function that can change an employee object's ID number
  • fred --> an object of the Employees class called fred
  • sally --> an object of the Employees class called sally
  • billy --> an object of the Employees class called billy

Notes:

  • It is good practice to capitalize the first letter of a class name
  • The private members cannot be accessed from anywhere except by other class members (such as from main() ), so I declared public functions that can access and change the members

Initializing Class Functions

In the above (Employees) example, I declared functions, but did not initialize them. The syntax for declaring functions in a class is as follows:

return_type name(argument1_type, argument2_type, ...);
  • If it has no arguments, insert "void" as the argument type

Initializing a class function is similar to initializing a regular function. There is only one difference. Place the class name and two colons before the function name. Here is an example with, change_name(), the function I declared earlier:

void Employees::change_name(string x)
{
    name = x;
}

I can call it by saying:

fred.change_name("Fred");
  • this changes the member name of object fred to "Fred"

Here is a program I wrote. It is an expansion of the program in my data structure tutorial. It just uses a class instead of a data structure:

#include
#include
using namespace std;

class Animal
{
    private:
        string name;
        int num_legs;
    public:
        void change_name(string);
        void change_legs(int);
        void printA(void);
        string color;
};

void Animal::change_name(string new_name)
{
    name = new_name;
}

void Animal::change_legs(int new_num_legs)
{
    num_legs = new_num_legs;
}

void Animal::printA()
{
    cout << name
    << "s are "
    << color
    << ", and have "
    << num_legs
    << " legs.\n";
}

int main()
{
    Animal dog, bird, spider;
    
    dog.change_name("Dog");
    dog.color = "brown";
    dog.change_legs(4);

    bird.change_name("Bird");
    bird.color = "grey";
    bird.change_legs(2);

    spider.change_name("Spider");
    spider.color = "black";
    spider.change_legs(8);

    dog.printA();
    bird.printA();
    spider.printA();

    bird.change_name("Parrot");
    bird.color = "green";
    bird.printA();

    return 0;
}
  • Animal --> a class that describes different Animal objects
    • name --> a private string to hold the name of each Animal object
    • num_legs --> a private integer to hold the number of legs of each Animal object
    • change_name() --> a public function to access and change the name of each Animal object
    • change_legs() --> a public function to change the number of legs each Animal object has
    • printA() --> a function that prints the attributes of each Animal object
    • color --> a public string that holds the color of each Animal object
  • main()
    • creates 3 Animal objects (dog, bird, and spider)
    • sets the attributes of each object
      • if the attribute is a private member: call the public function that is allowed to change that attribute
      • if the attribute is a public member: simply change the attribute just like we did with data structures
    • call printA() for each object
    • change the name of object bird to "Parrot"
    • change the color of the object bird to "green"
    • call printA() for the object bird again

Here is the output:

[cmw4026@omega test]$ g++ class.cpp
[cmw4026@omega test]$ ./a.out
Dogs are brown, and have 4 legs.
Birds are grey, and have 2 legs.
Spiders are black, and have 8 legs.
Parrots are green, and have 2 legs.
[cmw4026@omega test]$

Note that to change the color, we could do that from main (it is a public member). For the other two attributes, we had to call a class function to change them (they are private members).

I hope this helped! Be looking for the next object oriented programming tutorial in the near future. Leave any suggestions in the comments!

H2
H3
H4
Upload from PC
Video gallery
3 columns
2 columns
1 column
5 Comments