[Solved] How do I call a method function from a class with more than one object/field


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;
}

solved How do I call a method function from a class with more than one object/field