Thread: I need some help with writing Classes/Structures! :P

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    5

    Lightbulb I need some help with writing Classes/Structures! :P

    Hi I am currently writing a code that finds the birthday of someone!

    -The class should have a three parameter default constructor that allows the date to be set when a new Date object is created.
    -If the user creates a Date object without any arguments or if the arguments have invalid values:
    • month should be between 1 and 12
    • date should be between 1 and the number of days in the selected month
    then the default values should be 1,1,2001.
    The class should have member functions that allow to print the date in any of the three following formats:
    • 3/15/13
    • March 15, 2013
    • 15 March 2013
    I need to test this class in a program that creates a date object with default parameters and another with program defined ones (no need to ask the user) and uses the class functions to print those dates using the three different printing functions.

    This is my code so far:
    Code:
    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 62 63 64 65 66 67 68 69 70 71 #include <iostream> using namespace std; class Date { private: int month; int day; int year; public: void getInfo( int month, int day, int year); void printOne(void); void printTwo(void); void printThree(void); Date(); }; void Date::getInfo(int m, int d, int y) { month=m; day=d; year=y; } void Date::printOne() { switch(month) { case 1: cout<<"January"<<day<<","<<year; break; case 2: cout<<<"February"<<day<<","<<year; break; case 3: cout<<"March"<<day<<","<<year; break; case 4: cout<<"April"<<day<<","<<year; break; case 5: cout<<"May"<<day<<","<<year; break; case 6: cout<<"June"<<day<<","<<year; break; case 7: cout<<"July"<<day<<","<<year; break; case 8: cout<<"August"<<day<<","<<year; break; case 9: cout<<"September"<<day<<","<<year; break; case 10: cout<<"October"<<day<<","<<year; break; case 11: cout<<"November"<<day<<","<<year; break; case 12: cout<<"December"<<day<<","<<year; break; } } Date::Date() {month=1;day=1;year=2001;} void Date::printTwo() { cout<<month<<"/"<<day<<"/"<<year<<endl; }
    I really need to know if I am on the right track with using classes. For the printout function I feel like i need to use a switch in order to convert the numbers into strings i.e. 3=march or 12=december. ANY advice is APRECIATED!!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are on the right path, although GetInfo should be a constructor according to the instructions. You can also merge this and the default constructor using optional parameters, though I don't know if it will violate your requirements since the user could enter only, say, year, and leave out the rest.
    You can also use a lookup-table for the months:

    Code:
    const std::array<std::string, 12> Months = { "January", "February", ..., "December" }; // If your compiler supports C++11
    const std::string Months[] = { "January", "February", ..., "December" }; // If your compiler does not support C++11
    std::cout << year << "," << Months.at(month - 1) << "," << day << "\n";
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    5
    Thank you so much Elysia!! You are very helpful!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with classes & structures
    By PenguinTux in forum C++ Programming
    Replies: 21
    Last Post: 04-11-2011, 02:52 PM
  2. Help with structures and classes
    By jdcollins in forum C++ Programming
    Replies: 1
    Last Post: 11-14-2009, 05:07 PM
  3. Classes and structures
    By mcorn in forum C++ Programming
    Replies: 19
    Last Post: 12-12-2002, 11:50 AM
  4. classes and structures
    By mcorn in forum C++ Programming
    Replies: 1
    Last Post: 12-07-2002, 05:08 PM
  5. structures and classes
    By abrege in forum C++ Programming
    Replies: 4
    Last Post: 11-17-2002, 03:26 AM

Tags for this Thread