-5

I create the following class as an exmaple

class Date{

public:
   setDate( Date &obj );

private:
   int day, month, year;

};

Then in the main() I'd like to use the function setDate() but how do I fill the date & obj in this case?

void main(){

    Date * today;
    today->setDate( ??? );
}
  • 1
    Pass it another `Date` object. Also use `today->setDate()`, and the argument should probably be changed to `Date const &`. – cdhowie Jun 02 '17 at 19:42
  • There doesn't seem to be a way to set the date. The interface suggests you can only set the date to another date object, meaning there is no specify an arbitrary date. – François Andrieux Jun 02 '17 at 19:44
  • 3
    You've created a way to set a `Date` from another `Date` but that makes it impossible to set the first `Date`. You probably need a constructor or functions to set the `Date` from its components something like `Date(int day, int month, int year)`. – twain249 Jun 02 '17 at 19:44
  • @cdhowie `today.setDate()` is pretty much `today->setDate()` – Ahmad Khateeb Jun 02 '17 at 19:48
  • @twain249 so you are saying I can't use `Date` same way I use `int` or `char`? – Ahmad Khateeb Jun 02 '17 at 19:49
  • @AhmadKhateeb No. It's not "pretty much" something totally different. – cdhowie Jun 02 '17 at 19:50
  • @cdhowie when I press `.` my compiler automatically switches it to `->`, so I wouldn't know of it. anyway I will edit the post – Ahmad Khateeb Jun 02 '17 at 19:52
  • 3
    Why do you use `new` without `delete`? Why do you use `new` at all? – 463035818_is_not_an_ai Jun 02 '17 at 19:52
  • @tobi303 as far as I know I need to use `new` or at least so I was taught, and this is just an example, not the actual code – Ahmad Khateeb Jun 02 '17 at 19:53
  • 2
    You were taught poorly. C++ is not Java or C#. You don't need to use `new` to create an instance of a class. – Thomas Matthews Jun 02 '17 at 19:55
  • to say it clear: Thats just plain wrong. In c++ you create an object via `Date today;`. I suggest you to forget whatever you were taught and [read a book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – 463035818_is_not_an_ai Jun 02 '17 at 19:56

1 Answers1

0

1) First, don't use naked new, and don't use new al all if it is not necessary. Because it can lead to memory leak problems.

2) Don't use a setter just use a constructor.

3) Use const Date & in an overloaded Date constructor

4) Initialize Date with initilizer list. Use the overloaded constructor Date to build a Date using a const reference to other Date

5) In C++ return type of main must be int

class Date{

  public:
    Date( int dd, int mm, int yyyy);
    Date( const Date &obj );

  private:
    int day, month, year;

};

int main(){

    Date now = {02,06,2017};

    Date today(now);
    return 0;
}

6) Now if you really need a setter function you can do:

class Date{

public:
Date(int day, int month, int year); //A
Date( const Date &obj );  //B

void setDate( const Date &obj );

private:
   int day, month, year;

};

int main(){

    Date now = {02,06,2017};

    Date today(now);

    today.setDate({02,06,2017}); //Setter example using initilizer list + contructor A 
    today.setDate(now); //Setter example
    return 0;
}
Rama
  • 3,222
  • 2
  • 11
  • 26