Thread: i do i get started on cpp file

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    41

    Exclamation i do i get started on cpp file

    I need a quick explanation of how to write the cpp file for my program. I have the class written below. I don't know how to use
    the code tags that you guys like. Sorry. Can someone help me get started on the cpp file, please?. Give me an example in you can please.

    I have to write a class called date. I also have to write a cpp file for the class that represents a calendar date in terms of month,
    day and year, as on a 12 month calander. The valid months are Jan. through December, a valid day must correspond to a valid
    day for the given month and the year must be positive number. The object should store a format setting to be used
    for display of dates to the screen. There is more than one possible format. The class features should work exactly as specified,regardless of what program might be using Date objects. Dates should be specified with integers. Month values with be 1 for Jan,
    2 for Feb and so on. Valid day will be an integer between 1 and the number of days in the month. Valid year values are positive
    numbers.


    #ifndef date_H
    #define date_H

    class date
    {

    public:

    date(); //default constructor
    date(int m , int d, int y);
    void input();// promts user to enter date
    void show();//outputs date
    bool Set (int m, int d, int y); //sets date to specified values
    bool SetFormat (char f);//allows user to change format setting
    void Increment();//move date foward one calendar year

    private:
    int GetMonth();//returns month
    int GetDay();// returns day
    int GetYear(); //returns year

    };//ends class date

    #endif

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    For one thing your class has no member variables. I'd think for what you're doing you'd want to include 3 ints:

    int Month;
    int Day;
    int Year;

    After you've added that you would start by creating a new *.cpp file and including your date.h file. To start off with in your default constructor you might want to initialize your 3 int var's with a default date.

    You can initialize them in the class definition or assign them in the implementation. Here's an example of both (I prefer initialization when possible):

    Code:
    //each example sets the default date to Jan. 1, 1990.
    
    //initialization in the definition (in your date.h file)
    class date
    {
      public:
        date() : Month(1) , Day(1) , Year(1900) {}; 
        //the rest of your stuff goes here.....
    };
    
    //OR
    //assignment in your *.cpp file
    #include "date.h"
    
    date::date()
    {
       Month=1;
       Day=1;
       Year=1990;
    }

    ***EDIT*** to use code tags type [ code ] at the start and [ /code ] at the end (just leave out the spaces between the brackets and the words code and /code.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    41

    Exclamation i'm still lost

    I still don't understand how to start the cpp file. Can I hve an example?

  4. #4
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    >>I still don't understand how to start the cpp file. Can I hve an
    >>example?

    I just showed you how to implement your default constructor, are you asking for me to code out each of your member functions? That's not going to happen.

    Simply create a new *.cpp file (date.cpp?) and include your header file. Then start to define your member functions:

    Code:
    #include "date.h"
    
    date::date()
    {
       Month=1;
       Day=1;
       Year=1990;
    }
    
    void date::Increment()
    {
       Year++;
    }
    Now break out your compiler and start making some attempts at defining your member functions as to the specifications your teacher gave you. When you get stumped or get an error post the code you have so far and the error messages you get. Then we can move forward...

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    41

    Exclamation here's what i have thus far

    I got two errors that are as a result of the bool statements. Howdo I get them to go away? How would I set up the SetFormat function? Would I leave the Show function blank?


    Code:
    #include <iostream>
    #include "date.h"
    using std::cout;
    
    int Month;
    int Day;
    int Year;
    
    date::date()
    {
       Month=1;
       Day=1;
       Year=1990;
    }
    
    void date::Increment()
    {
       Year++;
    }
    
    void date::input()
    {
    	cout<< "Enter the date:";
    }
    
    void date::show()
    {
    
    
    }
    
    date::bool Set(int m, int d, int y)
    {
      return true;
    }
    
    date::bool SetFormat(char f)
    {
        SetFormat ('L')
        
    	
    	return true;
    }

  6. #6
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    These 2 functions are declared wrong for one thing and for
    another they are implemented wrong:

    date::bool Set(int m, int d, int y)
    {
    return true;
    }

    date::bool SetFormat(char f)
    {
    SetFormat ('L')


    return true;
    }
    First things first, do you understand why Set is declared as a
    bool? My guess would be because you have to validate the
    parameters passed. If the params passed are invalid then you
    would NOT update the 3 int values and return false. If the params
    are valid then you WOULD update the 3 int values and return
    true.

    For instance:
    Code:
    //first off you declare it with the type first like this:
    bool date::Set(int m, int d, int y)
    {
       //first make sure we have a valid year
       if(y>=1) //disallowing the year 0000
       {
          //make sure that the month param represents a valid month
          if((m>=1)&&(m<=12))
          {
             switch(m)
             {
                case 1: //jan - all until break have 31 days
                case 3: //mar
                case 5: //may
                case 7: //jul
                case 8: //aug
                case 10: //oct
                case 12: //dec
                {
                    //validate the day
                    if((d>=1)&&(d<=31)
                    {
                           //everything checks out so update the date and...
                           Month=m;
                           Day=d;
                           Year=y;
                           //return true
                           return(true);
                    }
                    else //invalid day value entered
                    {
                       return(false);
                    }
                } break;
                case 4: //apr - all until break have 30 days
                case 6: //jun
                case 9: //sep
                case 11: //nov
                {
                   //validate the day
                    if((d>=1)&&(d<=30)
                    {
                           //everything checks out so update the date and...
                           Month=m;
                           Day=d;
                           Year=y;
                           //return true
                           return(true);
                    }
                    else //invalid day value entered
                    {
                       return(false);
                    }
                }break;
                case 2: //feb - you might later put in checks for leap year
                {
                  //validate the day
                    if((d>=1)&&(d<=28)
                    {
                           //everything checks out so update the date and...
                           Month=m;
                           Day=d;
                           Year=y;
                           //return true
                           return(true);
                    }
                    else //invalid day value entered
                    {
                       return(false);
                    }
                }break;
             }
          }
          else //month is less than 1 or greater than 12.
          {
             return(false);
          }
        }
        else //year is a negative number or 0
        {
          return(false);
        }
    }
    See you verify that all the params entered meet the criteria for a
    valid date and if they do then you update your date and return
    true (success), if not then don't update and return false (failure).

    As for the SetFormat function. It too was declared wrong. Look
    at your other functions if you still don't see what I mean by
    declaring the function type first:

    Code:
    //this is what you have
    ///first off your declaration should be: bool date::SetFormat(char f)
    date::bool SetFormat(char f)
    {
       ///secondly, why are you using recursion here?
        SetFormat ('L')
        
    	///thirdly, you've done no validation checks yet you
                   ///returned true (why?)
    	return true;
    }

    You're going to want to add another member variable, say maybe
    char Format, which you'd default to 'm' in your constructor. 'm'
    would represent a date format of month/day/year.

    You have to decide on how many formats you want to allow. Once you have that, decide on the values that represent the
    different formats.

    When a user issues a call to SetFormat validate that the param
    passed is a valid one. If so, reset the Format member variable
    and return true. If not, do NOT reset it and return false.

    This would come in to play when you call show(). Where show
    would cout the date to the screen in the format who's value
    matches Format.

    You also need to think about the design of input(). As of now it
    simply tells the user to enter a date but never gets a value back.
    Also, you will want to determine how you want to have it entered.
    Either as a whole string which you'll parse or to ask for the month,
    then ask for the day, then finally ask for the month. Also, consider
    that you'll want to validate your calls to cin() to make sure they
    enter numbers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  4. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM