14 Replies - 7648 Views - Last Post: 08 February 2011 - 04:19 AM Rate Topic: -----

#1 collegejunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-February 11

creating a class called Date. Data members- month, day, and year (int)

Posted 05 February 2011 - 01:48 PM

I'm having trouble getting started with the constructor and parameters. I think that If I could get started then I could finsih it. This is what I have so far, can you point me in the right direction if I'm on the right track or not?

class Date
{ 
Public:
       Date( string )
       {
             setDate1( date );
             }
             
       void setMonth( string ); 
       string getMonth();
       
       void setDay( int );
      
       void setYear( int );
       
       void displayDate();    



Also this is the main that he provided.

int main(){
      //variables with assigned values, used for first instance of Date
      int test_month = 15, test_day = 31, test_year = 2011;
      //variables with values set from user input, used for second instance of Date
      int user_month, user_day, user_year;
      //setting up first instance of Date, date1:
      Date date1(test_month, test_day, test_year);
      cout<<"First date\n";
      date1.displayDate(); //prints out date in mm/dd/yyyy format
      cout<<"\nUser input for second date\n";
      cout<<"Enter the month: ";
      cin>>user_month; //take in user input for month
      cout<<"Enter the day: ";
      cin>>user_day; //take in user input for the day of month
      cout<<"Enter the year: ";
      cin>>user_year; //take in user input for year
      Date date2(user_month, user_day, user_year);
      cout<<"\nSecond date\n";
      date2.displayDate();
      //third instance of Date, using constants:
      Date date3(2, 29, 2008);
      cout<<"\nThird date, using constants\n";
      date3.displayDate();
	  date3.setMonth(3);
	  date3.setDay(19);
	  date3.setYear(1988);
	  cout<<"\nThird date, after changes\n";
	  cout<<date3.getMonth()<<"/"<<date3.getDay()<<"/"<<date3.getYear();
      system("PAUSE");
      return 0;
   
   }


This post has been edited by Martyr2: 05 February 2011 - 01:56 PM
Reason for edit:: Please use code tags in the future, thanks!


Is This A Good Question/Topic? 0
  • +

Replies To: creating a class called Date. Data members- month, day, and year (int)

#2 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: creating a class called Date. Data members- month, day, and year (int)

Posted 05 February 2011 - 01:55 PM

use [code] tag.
Was This Post Helpful? 1
  • +
  • -

#3 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4187
  • View blog
  • Posts: 11,847
  • Joined: 18-April 07

Re: creating a class called Date. Data members- month, day, and year (int)

Posted 05 February 2011 - 01:55 PM

Based on the main provided, your instructor is expecting you to build a constructor which takes three arguments... a month, a day and a year. So you shouldn't be defining one that simply takes "String"

public:

Date(int month, int day, int year) {
     prvmonth = month;
     prvday = day;
     prvyear = year;
}



This is the basic idea behind the constructor. We take in three parameters for each piece of information. The variables prvmonth, prvday, prvyear are your private class variables which will store the incoming data values.

Now the one thing I didn't include here is any sort of validation. You will want to get started working on validating the incoming data. Does it make sense for day to be 54 or month to be 17? You can validate this information by checking if the incoming data fits in specific ranges. Normally you would set properties like setDay(int day) and in there check the range. You can then call setDay from your constructor.

But I will let you figure that part out. :)
Was This Post Helpful? 1
  • +
  • -

#4 collegejunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-February 11

Re: creating a class called Date. Data members- month, day, and year (int)

Posted 05 February 2011 - 02:58 PM

But how do I set up all 3 dates? This is the 2nd program and I'm a beginner! I keep getting complier errors like invalid function declaration, and No members named.. I don't know how to start it off!
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4187
  • View blog
  • Posts: 11,847
  • Joined: 18-April 07

Re: creating a class called Date. Data members- month, day, and year (int)

Posted 05 February 2011 - 03:07 PM

Well I just told you how to start it off... in your class declaration you will have...

Date(int month, int day, int year);



That is what it will be in your class' .h header file. Then for the constructor definition in the .cpp file it will look like...

Date::Date(int month, int day, int year) {
     prvmonth = month;
     prvday = day;
     prvyear = year;
}



Of course replacing prvmonth, prvday, and prvyear with your member variables names. Which you will have to define. Check out the following page to see how constructors are put together...

C++: Constructors

There are two pieces here and the error is probably telling you that you have declared the function, but you have not defined it. The class code you are putting together is letting the compiler know what a "Date" is. The other part is telling the compiler what it can DO with a Date. Thus the code you put in the class declaration must match the signature of the definition (aka implementation).

:)
Was This Post Helpful? 0
  • +
  • -

#6 collegejunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-February 11

Re: creating a class called Date. Data members- month, day, and year (int)

Posted 05 February 2011 - 03:25 PM

This is what I have so far, but something still isn't right? Do I need to declare something to Date1, Date2, and Date3?

#include<iostream>
using namespace std;

class Date
{
Public:
       Date( int mm, int dd, int yyyy )
       {
             setMonth(mm);
             setDay(dd);
             setYear(yyyy);
             }
       void setMonth(int mm)
       {
            if( mm < 1 )
            month = 1;
            if( mm > 1 )
            {
                if( mm > 12 )
                month = 1;
                
                if( mm < 12 )
                month = mm;
                }
       }//end of function setMonth
       
//function that sets the day
void setDay(int dd)
{
     day = dd;
     }
//function that sets the year
void setYear(int yyyy)
{
     year = yyyy;
     }
            
int main(){
      //variables with assigned values, used for first instance of Date
      int test_month = 15, test_day = 31, test_year = 2011;
      //variables with values set from user input, used for second instance of Date
      int user_month, user_day, user_year;
      //setting up first instance of Date, date1:
      Date date1(test_month, test_day, test_year);
      cout<<"First date\n";
      date1.displayDate(); //prints out date in mm/dd/yyyy format
      cout<<"\nUser input for second date\n";
      cout<<"Enter the month: ";
      cin>>user_month; //take in user input for month
      cout<<"Enter the day: ";
      cin>>user_day; //take in user input for the day of month
      cout<<"Enter the year: ";
      cin>>user_year; //take in user input for year
      Date date2(user_month, user_day, user_year);
      cout<<"\nSecond date\n";
      date2.displayDate();
      //third instance of Date, using constants:
      Date date3(2, 29, 2008);
      cout<<"\nThird date, using constants\n";
      date3.displayDate();
	  date3.setMonth(3);
	  date3.setDay(19);
	  date3.setYear(1988);
	  cout<<"\nThird date, after changes\n";
	  cout<<date3.getMonth()<<"/"<<date3.getDay()<<"/"<<date3.getYear();
      system("PAUSE");
      return 0;
   
   }


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 05 February 2011 - 03:37 PM

Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: creating a class called Date. Data members- month, day, and year (int)

Posted 05 February 2011 - 03:38 PM

When you post code, you must obey the rules and USE CODE TAGS! Like this:

:code:

If that's too complicated, read the first link in my signature.\
Was This Post Helpful? 0
  • +
  • -

#8 collegejunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-February 11

Re: creating a class called Date. Data members- month, day, and year (int)

Posted 05 February 2011 - 03:54 PM

I'm sorry! I forgot, but I got it now!
So this is what I have so far. I don't know if I'm supposed to declare something to Date1, Date2, or Date3??

#include<iostream>
using namespace std;

class Date
{
Public:
       Date::Date( int month, int day, int year )
       {
             mm = month;
             dd = day;
             yyyy = year;
             }
       void setMonth(int mm)
       {
            if( mm < 1 )
            month = 1;
            if( mm > 1 )
            {
                if( mm > 12 )
                month = 1;
                
                if( mm < 12 )
                month = mm;
                }
       }//end of function setMonth
       
//function that sets the day
void setDay(int dd)
{
     day = dd;
     }
//function that sets the year
void setYear(int yyyy)
{
     year = yyyy;
     }
     
Private:
        int month;    //Month for the Date class
        int day;      //Day for the Date class
        int year;     //Year for the Date class
        };            // end of class definition
            
int main(){
      //variables with assigned values, used for first instance of Date
      int test_month = 15, test_day = 31, test_year = 2011;
      //variables with values set from user input, used for second instance of Date
      int user_month, user_day, user_year;
      //setting up first instance of Date, date1:
      Date date1(test_month, test_day, test_year);
      cout<<"First date\n";
      date1.displayDate(); //prints out date in mm/dd/yyyy format
      cout<<"\nUser input for second date\n";
      cout<<"Enter the month: ";
      cin>>user_month; //take in user input for month
      cout<<"Enter the day: ";
      cin>>user_day; //take in user input for the day of month
      cout<<"Enter the year: ";
      cin>>user_year; //take in user input for year
      Date date2(user_month, user_day, user_year);
      cout<<"\nSecond date\n";
      date2.displayDate();
      //third instance of Date, using constants:
      Date date3(2, 29, 2008);
      cout<<"\nThird date, using constants\n";
      date3.displayDate();
	  date3.setMonth(3);
	  date3.setDay(19);
	  date3.setYear(1988);
	  cout<<"\nThird date, after changes\n";
	  cout<<date3.getMonth()<<"/"<<date3.getDay()<<"/"<<date3.getYear();
      system("PAUSE");
      return 0;
   
   }


Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: creating a class called Date. Data members- month, day, and year (int)

Posted 05 February 2011 - 04:23 PM

 Date::Date( int month, int day, int year )
       {
             mm = month;
             dd = day;
             yyyy = year;
             }



So, what are mm, dd, and yyyy? I don't see them defined anywhere in your class.
Was This Post Helpful? 0
  • +
  • -

#10 collegejunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-February 11

Re: creating a class called Date. Data members- month, day, and year (int)

Posted 07 February 2011 - 02:50 PM

#include<iostream>
using namespace std;


class Date
{
      int month;
      int day;
      int year;


public:
        
       void setDate( int mm, int dd, int yyyy )
       {
             month = mm;
             day = dd;
             year = yyyy;
       }
  
       
       void setMonth(int mm)
       {
            if( month < 1 )
            month = 1;
            if( month > 1 )
            {
                if( month > 12 )
                month = 1;
                
                if( month < 12 )
                month = mm;
                }
       }//end of function setMonth
      
      //function to get the month 
       string getMonth()
       {
              return month;
 
       
//function that sets the day
       void setDay(int dd)
           {
            day = dd;
           }
        string getDay( int dd )
           {
            return Day;
           }
};
//function that sets the year
        
        void setYear(int yyyy)
        {
             year = yyyy;
        }
        string getYear()
        {
                    return Year;
                    }
                    
void displayDate()
{
     cout << getMonth << getDay << getYear()
     << "!" << endl;
     }
     
}; // end function displayDate
     
private:
        
  Date;
  
};         
            
int main(){
      //variables with assigned values, used for first instance of Date
      int test_month = 15, test_day = 31, test_year = 2011;
      //variables with values set from user input, used for second instance of Date
      int user_month, user_day, user_year;
      //setting up first instance of Date, date1:
      Date date1(test_month, test_day, test_year);
      cout<<"First date\n";
      date1.displayDate(); //prints out date in mm/dd/yyyy format
      cout<<"\nUser input for second date\n";
      cout<<"Enter the month: ";
      cin>>user_month; //take in user input for month
      cout<<"Enter the day: ";
      cin>>user_day; //take in user input for the day of month
      cout<<"Enter the year: ";
      cin>>user_year; //take in user input for year
      Date date2(user_month, user_day, user_year);
      cout<<"\nSecond date\n";
      date2.displayDate();
      //third instance of Date, using constants:
      Date date3(2, 29, 2008);
      cout<<"\nThird date, using constants\n";
      date3.displayDate();
	  date3.setMonth(3);
	  date3.setDay(19);
	  date3.setYear(1988);
	  cout<<"\nThird date, after changes\n";
	  cout<<date3.getMonth()<<"/"<<date3.getDay()<<"/"<<date3.getYear();
      system("PAUSE");
      return 0;
   
   }



I keep getting errors on my get functions ( getMonth and so on). I don't know why I'm getting these errors???
Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: creating a class called Date. Data members- month, day, and year (int)

Posted 07 February 2011 - 03:00 PM

C and C++ are case-sensitive.

Posted Image

See also this blog entry.
Was This Post Helpful? 0
  • +
  • -

#12 collegejunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-February 11

Re: creating a class called Date. Data members- month, day, and year (int)

Posted 07 February 2011 - 05:40 PM

Can anyone help me try to get this code compiled? I've starred at it so long, I can't even concentrate!!! int Main() is correct, I'm supposed to work from that to create a class Date with 3 data members (month, day, and year)
and a constructor with three parameters...
The value for the year and day are correct. month value is range 1-12, if not set to 1. & also provide a set and get function for each data member!
#include<iostream>
using namespace std;


class Date
{
      int month;
      int day;
      int year;


public:
        
       void setDate( int mm, int dd, int yyyy )
       {
             month = mm;
             day = dd;
             year = yyyy;
       }
  
       
       void setMonth(int mm)
       {
            if( month < 1 )
            month = 1;
            if( month > 1 )
            {
                if( month > 12 )
                month = 1;
                
                if( month < 12 )
                month = mm;
                }
       }//end of function setMonth
      
      //function to get the month 
       int Date::getMonth()
       {
              return month;
              }
 
       
//function that sets the day
       void setday(int dd)
           {
            day = dd;
           }
        int Date::getDay()
           {
            return day;
           }
};
//function that sets the year
        
        void setYear(int yyyy)
        {
             year = yyyy;
        }
        int Date::getYear();
            {
                    return year;
            }
                    
void displayDate()
{
     cout << getMonth/n, << getDay/n, << getYear()
     << "!" << endl;
     }
     
}; // end function displayDate
     
private:
        
  Date;
  
};         
            
int main(){
      //variables with assigned values, used for first instance of Date
      int test_month = 15, test_day = 31, test_year = 2011;
      //variables with values set from user input, used for second instance of Date
      int user_month, user_day, user_year;
      //setting up first instance of Date, date1:
      Date date1(test_month, test_day, test_year);
      cout<<"First date\n";
      date1.displayDate(); //prints out date in mm/dd/yyyy format
      cout<<"\nUser input for second date\n";
      cout<<"Enter the month: ";
      cin>>user_month; //take in user input for month
      cout<<"Enter the day: ";
      cin>>user_day; //take in user input for the day of month
      cout<<"Enter the year: ";
      cin>>user_year; //take in user input for year
      Date date2(user_month, user_day, user_year);
      cout<<"\nSecond date\n";
      date2.displayDate();
      //third instance of Date, using constants:
      Date date3(2, 29, 2008);
      cout<<"\nThird date, using constants\n";
      date3.displayDate();
	  date3.setMonth(3);
	  date3.setDay(19);
	  date3.setYear(1988);
	  cout<<"\nThird date, after changes\n";
	  cout<<date3.getMonth()<<"/"<<date3.getDay()<<"/"<<date3.getYear();
      system("PAUSE");
      return 0;
   
   }

Was This Post Helpful? 0
  • +
  • -

#13 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: creating a class called Date. Data members- month, day, and year (int)

Posted 07 February 2011 - 05:46 PM

Merged topics. DON'T open a new topic when you already have one on the same subject.

I'm also guessing you didn't bother to read the blog entry to which I linked, given your poor question. If you're getting errors...TELL US WHAT THEY ARE!!! We're helping you for free, don't make us work harder than we have to.
Was This Post Helpful? 0
  • +
  • -

#14 collegejunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-February 11

Re: creating a class called Date. Data members- month, day, and year (int)

Posted 07 February 2011 - 05:59 PM

Sorry I didn't see that blog! But there are so many I don't even know where to start! This is only the second program I've written. I'm very new to it!!!

In function `void setYear(int)':
`year' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
no `int Date::getYear()' member function declared in class `Date'
declaration of `int Date::getYear()' outside of class is not definition
expected unqualified-id before '{' token
expected `,' or `;' before '{' token
In function `void displayDate()':
Was This Post Helpful? 0
  • +
  • -

#15 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: creating a class called Date. Data members- month, day, and year (int)

Posted 08 February 2011 - 04:19 AM

Please take the time to actually learn the class syntax. Stop just throwing code onto the page and expecting it to just work.

class MyClass
{
public:
   // public member functions and variables go here

private:
   // private member functions and variables go here
};


Make your braces all match up properly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1