using a header file with a class in it

I cant seem to get the my date header file do anything with the functions i have in my main,


here is my header file
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
  #ifndef DATE_H
#define DATE_H

#include<iostream>
#include<string>
#include<cstring>

using namespace std;

class Date
{
	private:
		static string date1;
		int month;
		int day;
		int year;
	public:
		Date()
		{ date1++;}

		void setMonth(int, string);
		void setDay(int, int);
		void setYear(int);
		int getMonth() const
		{ 
			return month;
		}
		int getDay() const
		{
			return day;
		}
		int getYear() const
		{
			return year;
		}
};
#endif


here are my functions and calls
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

Date::Date()
{
	month=1;
	day=1;
	year=2000;
}

void Date::setMonth(int m, string mName[])
{
	const int size=13;
	string mName[size]={"null", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
	if (m>=1 && m<=12)
	{
		month=mName[m];
	}
	else
	{
		cout<<"Invalid Month\n";
		exit (EXIT_FAILURE);
	}

}

void Date::setDay(int d, int m)
{
	
	int daysOfMonths[13] = {0,31,28,31,30,31,30,31,30,31,30,31,30};

	if (d<=daysOfMonths[m])
	{
		day=d;
	}
	else
	{
		cout<<"Invalid Day\n";
		exit(EXIT_FAILURE);
	}
}
void Date::setYear(int y) 
{
	year=y;
}
Have you included the header file and is it in the same folder as your cpp file?
 
#include "date.h" 

Also
 
{ date1++;}

Why are you trying to increment a string ?
Last edited on
yes, i just didnt include it in the lines i copied


when i try to compile it does find the file and i think attempt to use it, im just no getting results
Last edited on
What sort of error code are you getting.
It might be because you defined
1
2
3
4
5
6
Date::Date()
{
	month=1;
	day=1;
	year=2000;
}

1
2
Date()
{ date1++;}

twice and tried to increment a string which will cause an error.
I have to set my default date to1/ 1/ 2000
then increment it one day at a time
and have it display the date as January 1, 2000
Strings don't work like that.
You would probably be better having three ints, one for day,month and year.
so if i have three ints, where do i change my month to a string
Use an "if" statement like if month is 1 output January.
There is not really any need to use a string.
couldnt i make a tempvariable to use my month and select an element in my array, if that works, i just dont know where to put it, i have such a hard time understanding how to pass info to and from functions.


i have such a hard time understanding how to pass info to and from functions.


Read these pages for functions.
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/

Read these pages for classes:
http://www.cplusplus.com/doc/tutorial/classes/
http://www.cplusplus.com/doc/tutorial/classes2/

Here is also an example:
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
class Square {
public:
    int width, height;
    Square(void)
    {
        width = 5;
        height = 5;
    }

    Square(const unsigned int a, const unsigned int b)
    {
       width = a;
       height = b;
    }

    Square operator + (Square param)
    {
        Square temp;
        temp.height = height + param.height;
        temp.width = width + param.width;
        return(temp);
    }
    const unsigned int area(void)
    {
        return(width * height);
    }

    const unsigned int perimeter(void)
    {
        return((width + height) * 2);
    }

};
int main()
{
    Square sq1( 2 , 3 ), sq2( 4 ,5 ), sq3, *pSq1, *pSq2;
    sq3 = sq1 + sq2;
    pSq1 = new Square;
    pSq1 = &sq1;
    pSq2 = pSq1;

    std::cout << sq1.area() << std::endl;
    std::cout << sq2.perimeter() << std::endl;
    std::cout << sq3.area() << std::endl;
    std::cout << pSq1->perimeter() << std::endl;
    std::cout << pSq2->area() << std::endl;
}
Topic archived. No new replies allowed.