#7 - ricky reed is best gem
How's it goin bros, Virgil Rhino here. Today, we're gonna discuss classes. Classes are similar to int main because they both use curly braces. The difference is that classes don't require a set of parameters, and int main doesn't require access specifiers (public:, protected:, and private:). Keep in mind, however, that classes are not functions, but they can contain functions. Say for instance, you create a class. For this example, we're going to call it 'kek'. Our class is going to have the public access specifier:class kek
{
public:
}
And it's here that we're going to create our function. We're going to create a void function, because unlike an int function, void returns nothing, so we don't have to type 'return 0;' at the end.
class kek
{
public:
void dAnK()
{
cout << "kek" << endl;
}
}
Functions inside of a class are called class member functions.
Now, we're going to use this class member function by creating an object. An object contains all the functions and variables contained on our class, which is how we can use that class member function. To make an object, go to int main() and type 'kek kekObj;', where 'kek' is our class name, and 'kekObj' is our object name. Below that, type 'kekObj.dAnK();'. This tells our object to use the class member function, titled dAnK(), which outputs 'kek'. When you're done, your program should look like this:
class kek
{
public:
void dAnK()
{
cout << "kek" << endl;
}
}
int main()
{
kek kekObj;
kekObj.dAnK();
return 0;
}
And your result should look like this:
This is how not to create a class. You don't make a class file without including private:, and you definitely don't use it to output text. That's a waste of a class file. I left this in here to help you avoid this.
To properly set up a class file, we first need to erase everything in our class and int main. It should look like this:
class kek
{
}
int main()
{
return 0;
}
Now, we add our public and private access specifiers:
class kek
{
public:
private:
}
And inside the private: access specifier, we add a variable and set it equal to any value. For this example, I'm going to set it to 40:
class kek
{
public:
private:
int dank = 40;
}
int main()
{
return 0;
}
Now in our public: access specifier, we add what are called Accessors and Mutators. These are self-explanatory. Accessors and Mutators are both class member functions that deal with the integer (also called 'data members') in the private: access specifier.
Mutators set the private: data member to something, and Accessors read from the private: data member.
Mutators are typically void functions because they're supposed to set the private: data member to something and return nothing. Accessors are usually int functions because they're only supposed to return the private: data member.
Something like this:
class kek
{
public:
void setDank(int x)
{
dank = x;
}
int getDank()
{
return dank;
}
private:
int dank = 40;
}
int main()
{
return 0;
}
And now we add our objects:
class kek
{
public:
void setDank(int x)
{
dank = x;
}
int getDank()
{
return dank;
}
private:
int dank = 40;
}
int main()
{
kek kekObj;
cout << kekObj.getDank();
return 0;
}
Note that you don't need to add cout <<, I'm just adding it as proof that our public: class member functions (Our 'getters and setters') did what they were supposed to.
Your result should look like this:
That's all for this post. In the next tutorial, we'll be going over objects and classes. Until then....