DILI #7: Classes - something Marxists does not understand!
Tags: coding, cpp, oop, productivity
Published: 2023-07-21 14:40:41

Moving on from the usual programming which is called Procedural Progarmming, where the code runs step by step from top down through fucntions, I have _mistakenly_ made my first step into OOP (Object-Oriented Programming). You see what I did there? Mistakenly and OOP! Anyways, OOP is usually said to be based on the "real world" with **objects** (hence the name) that contains datas and attributes and can interact with the real world. There are many new concepts that are introduced, but the most popular ones seemed to fly right over the Marxists' heads, classes.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ljmqwmlkgd8enhqze5v.png)

If the Marxists have read a bit about struct, they might have an easier time understanding classes. For them, surely they read OOP backwards. ...Ahem...they are similar as class can be used to replace struct in all cases:

```c++
struct DateStruct
{
    int year {};
    int month {};
    int day {};
};

class DateClass
{
public:
    int year {};
    int month {};
    int day {};
};
```

But the other way around can be met with some issues as structure is unable to have functions in them but class can.

```c++
class DateClass
{
public:
    int year {};
    int month {};
    int day {};

    void print() // defines a member function named print()
    {
        std::cout << year << '/' << month << '/' << day;
    }
};
```
The function of course can be accesed through the "." operators like in struct.  

```c++
int main()
{
    DateClass today { 2023, 7, 14 };

    today.m_day = 21; // use member selection operator to select a member variable of the class
    today.print(); // use member selection operator to call a member function of the class

    return 0;
}
```

This prints: 

```
2023/7/21
```

I'm sure you should have noticed the "public:" keyword there right below class. It's an access specifier that say the members of those in the class is public, meaning it can be accesed by functions outside of the class itself. By default, struct is public and class is private. Meaning if your code exclude that "public" keyword in class, only the functions or whatever inside that class can accessed it. Hence, why it presents.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u21yoyi0w3pmrwjean6v.jpg)

Typically, class members/objects are private so that no direct change can be made to them and actions/functions are made public. Consider the following program:

```c++
#include <iostream>

class DateClass // members are private by default
{
	int m_month {}; // private by default, can only be accessed by other members
	int m_day {}; // private by default, can only be accessed by other members
	int m_year {}; // private by default, can only be accessed by other members

public:
	void setDate(int month, int day, int year)
	{
		m_month = month;
		m_day = day;
		m_year = year;
	}

	void print()
	{
		std::cout << m_month << '/' << m_day << '/' << m_year;
	}

	// Note the addition of this function
	void copyFrom(const DateClass& d)
	{
		// Note that we can access the private members of d directly
		m_month = d.m_month;
		m_day = d.m_day;
		m_year = d.m_year;
	}
};

int main()
{
	DateClass date;
	date.setDate(10, 14, 2020); // okay, because setDate() is public

	DateClass copy {};
	copy.copyFrom(date); // okay, because copyFrom() is public
	copy.print();
	std::cout << '\n';

	return 0;
} 
```
This also shows that any function that is within the class can access both privately and publicly. 

In conclusion, classes are like struct but are more classy and can do way more things. It's also quite fun to mess about with, unlike in real life. 

P.S: Y'all **SHALL** give me some advices on blog-writing and programming. (I got tired of politely asking)

**REFERENCE: **
[Chapter 13.1 - 13.4](https://www.learncpp.com/)
[GeeksforGeeks](https://www.geeksforgeeks.org/structure-vs-class-in-cpp/)