The Use of Classes

So, I have a lot of the basics down in c++, but the only issue I am having is wrap my head around the idea of a class. I try reading the tutorials on this website, and watch a few youtube videos, but whenever my teacher makes a class, he uses booleans, and it just really throws me off. I was hoping could give me a better idea of classes and their uses. If someone could just comment and give me some tips, I would greatly appreciate it. Thanks much!

Cheers,
Ryan

P.s This is just some homework that I am working on, and struggling with. Lol.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  //  Time.cpp

#include "Time.h"

namespace time_namespace
{
    Time::Time(void)
    {
        hours   = 0;
        minutes = 0;
    }
    
    Time::Time( int i_hours )
    {
        hours = i_hours;
    }
    
    Time::Time( int i_minutes, int i_hours )
    {
        minutes = i_minutes;
        hours = i_hours;
    }
    
    int Time::getCurrentHour(void)
    {
        if (hours != 0) return hours;
        else            return 0;
    }
    
    bool Time::getCurrentMinute(void)
    {
        if (minutes != 0) return minutes;
        else              return 0;
    }
    
    bool Time::setCurrentHour(void)
    {
        return true;
    }
    
    bool Time::setCurrentMinute(void)
    {
        return true;
    }
    
    bool Time::setHourAndMinute(void)
    {
        return true;
    }
    
    bool Time::addHours(void)
    {
        return true;
    }
    
    bool Time::addMinutes(void)
    {
        return true;
    }

}
I just threw the booleans in there to stub out the functions to make to make the program run.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Time::Time( int i_hours )
    {
        hours = i_hours;
    }
    
    Time::Time( int i_minutes, int i_hours )
    {
        minutes = i_minutes;
        hours = i_hours;
    }
    
    int Time::getCurrentHour(void)
    {
        if (hours != 0) return hours;
        else            return 0;
    }
    
    bool Time::getCurrentMinute(void)
    {
        if (minutes != 0) return minutes;
        else              return 0;
    }


Firstly, I would test the inputs, in those definitions, you could make hour = 123456, is this a clock or a timer?

Why do booleans throw you off? It is a data type like any other, simpler in fact, as it can only hold two values, 0 or 1.

1
2
3
4
5
bool LessThan(int num, int test)
{
        if(num < test) return 1;
        else return 0;
}
This is actually a 24 hour clock, but without using <time.h>. it's more like just adding and subtracting user inputted hours. I struggle with classes like for this project. I just cannot seem to wrap my head around the concept. Like how would I make a class that adds the user inputted number of hours and minutes?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <thread>  // this_thread
#include <chrono> // seconds

class Clock
{
	unsigned int hrs, mins, secs;
public:
	Clock(unsigned int hours, unsigned int minutes, unsigned int seconds): hrs(hours), mins(minutes), secs(seconds)
	{
		if(hrs > 23) throw "Invalid hours";
		if(mins > 59) throw "Invalid minutes";
		if(secs > 59) throw "Invalid seconds";
	}
	~Clock() {}

	void Tick()
	{
		std::this_thread::sleep_for(std::chrono::seconds(1));
		secs++;
		if(secs > 59)
		{
			mins++; secs = 0;
		}
		if(mins > 59)
		{
			hrs++; mins = 0;
		}
		if(hrs > 23)
		{
			hrs = 0;
		}
	}

	unsigned int& GetHours() { return hrs; }
	unsigned int& GetMinutes() { return mins; }
	unsigned int& GetSeconds() { return secs; }
};

int main()
{
	try
	{
		Clock MyClock(0,0,0);
		while(1)
		{
			MyClock.Tick();
			std::cout << "\r" << MyClock.GetHours() << ":" << MyClock.GetMinutes() << ":" << MyClock.GetSeconds();
			std::cout.flush();
		}
	}
	catch(char* what) { std::cout << what; }

	return 0;
}
"How would I make a class that does..."

First, this has little to do with classes.

Classes are like blueprints. They can hold data and functions. The only real special tricks are that any data held by the class is considered in scope for any functions in the same class, and that functions can be made to do things to that data when two instances of the class are used to do math or comparisons. (Aka, a function can determine what happens when you do "obj1 + obj2")

Other than that, a class isn't anything special. Adding user input has nothing to do with classes, but you can put the code that does that stuff into a class if you want.
Topic archived. No new replies allowed.