RSS Forums RSS

Problem with constructor

Please support our C++ advertiser: Programming Forums
Reply
Posts: 16
Reputation: AcidG3rm5 has a little shameless behaviour in the past 
Solved Threads: 0
AcidG3rm5 AcidG3rm5 is offline Offline
Newbie Poster

Problem with constructor

  #1  
Nov 9th, 2008
#include <iostream>
#include <string.h>
using namespace std;

class Date
{
 public : Date();
          void setDate(int,string,int);
          void printDate();
          
 private : int day;
           string mth;
           int year;

};//end class


//date constructor
Date::Date()
{
 day = 01;
 mth = "Jan";
 year = 1990;
} 

//setDate
void Date::setDate(int inDay,string inMth, int inYear)
{
     day = inDay;
     mth = inMth;
     year = inYear;
     cout<<"Date set to "<<day<<"-"<<mth<<"-"<<year<<endl;     
}//end setDate

//printDate
void Date::printDate()
{
cout<<day<<"-"<<mth<<"-"<<year<<endl;
}//end print date

//functions declaration
void menu();
void setDateMenu(Date myDate);

int main()
{
    char choice;
    //show menu
    menu();
    //new a date object
    Date myDate;
    
    //read in the choice 
    cin>>choice;
    //change it to a lower case.
    choice = tolower(choice);
    
    while (1)
    {
          switch (choice)
          {
           case 'a' : setDateMenu(myDate);
                      //myDate.setDate(10,"Jan",2007);
                      system("pause");
                      system("CLS");
                      break;
           
           case 'b' : myDate.printDate();
                      system("pause");
                      system("CLS");
                      break;            
                     
           case 'q' : cout<<"Exiting system. Thank you for using calendar system."<<endl;
                      system("pause");
                      exit(0);
                      break;
                     
           default : cout<<"Please enter A to H only. Q to quit."<<endl;
                     system("pause");
                     system("CLS");
                     break;
          }//end switch
      cout << endl;                 
      menu(); 
      cin >> choice;
      choice = tolower(choice);
    }//end while
    
    
    system("pause");
}//end main

void menu()
{
     cout<<"A)\tSet Date"<<endl;
     cout<<"B)\tReturn Date"<<endl;
     cout<<"C)\tReturn number of days in month"<<endl;
     cout<<"D)\tReturn number of days passed in a year"<<endl;
     cout<<"E)\tCheck if leap year"<<endl;
     cout<<"F)\tReturn which day of week"<<endl;
     cout<<"G)\tAdd days to date"<<endl;
     cout<<"H)\tPrint month calendar"<<endl;
     cout<<"Q)\tQuit"<<endl;
     cout<<endl;
     cout<<"Please enter choice: "<<endl;
}//end menu

//setDate
void setDateMenu(Date myDate)
{
 //bool year = false;
 int inDay;
 string inMth;
 int inYear;
 
 cout<<"Enter Day: "<<endl;
 cin>>inDay;
 
 cout<<"Enter Month: "<<endl;
 cin>>inMth;
 
 cout<<"Enter Year: "<<endl;
 cin>>inYear;
 
 for(int i=0;i<inMth.length();i++)
 {//convert all the input for month to lower case
   tolower(inMth[i]);      
 }//end for loop

 myDate.setDate(inDay,inMth,inYear);
}//end setDateMenu

I have problem doing a set date for the above codes. every time i invoked the setDateMenu(Date myDate) to set a new date to my object myDate, it manages to take in the correct day,mth and year (I did a cout at void Date::setDate(int inDay,string inMth, int inYear) It shows the new day,mth and year set.

However, when i did a print date (option B), the new date values are still not saved into the dates and it still prints the default date value which is 01-Jan-1990.

Anyone got any idea where the codes went wrong and why the new values of the day,mth and year aren't passed into the object?
Last edited by AcidG3rm5 : Nov 9th, 2008 at 11:26 am.
AddThis Social Bookmark Button
Reply With Quote  
Posts: 13,883
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1231
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Problem with constructor

  #2  
Nov 9th, 2008
Your program does not include <string> for std::string class, so how in the world are you getting it to compile????? Don't attempt to execute a program that contains compiler errors or warnings. Yes, warnings in your code are usually errors too.
Reply With Quote  
Posts: 2,000
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 331
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Problem with constructor

  #3  
Nov 9th, 2008
  1. void setDateMenu(Date myDate);
You pass Date by value so set a new date to Date copy. The original object is intact.
Pass by reference:
  1. void setDateMenu(Date& myDate);
Common error...
I hope now you know that argument passed by value in C and C++ ...
Reply With Quote  
Posts: 16
Reputation: AcidG3rm5 has a little shameless behaviour in the past 
Solved Threads: 0
AcidG3rm5 AcidG3rm5 is offline Offline
Newbie Poster

Re: Problem with constructor

  #4  
Nov 9th, 2008
Thanks for the help. i made a reference variable to myDate and it worked.
Reply With Quote  
Posts: 2,000
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 331
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Problem with constructor

  #5  
Nov 9th, 2008
Yes, rather! It would be surprising if it didn't work
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 306 | Replies: 4 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:56 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC