Pass Objects to function and return object from functions. - C++ Class

C++ examples for Class:Member Function

Description

Pass Objects to function and return object from functions.

Demo Code

                                                                                                                                           
#include <iostream> 
using namespace std; 
                                                                                                                                           
class Date // ww  w  . j ava 2  s .c o m
{ 
public: 
   Date( int = 1, int = 1, int = 2000 ); // default constructor 
   void print(); 
private: 
   int month; 
   int day; 
   int year; 
}; // end class Date 
                                                                                                                                           
// Date constructor (should do range checking) 
Date::Date( int m, int d, int y ) 
{ 
   month = m; 
   day = d; 
   year = y; 
}
                                                                                                                                           
// print Date in the format mm/dd/yyyy 
void Date::print() 
{ 
   cout << month << '/' << day << '/' << year; 
}
                                                                                                                                           
int main() 
{ 
    Date date1( 7, 4, 2018 ); 
    Date date2; // date2 defaults to 1/1/2000 
                                                                                                                                           
    cout << "date1 = "; 
    date1.print(); 
    cout << "\ndate2 = "; 
    date2.print(); 
                                                                                                                                           
    date2 = date1; // default memberwise assignment 
                                                                                                                                           
    cout << "\n\nAfter default memberwise assignment, date2 = "; 
    date2.print(); 
    cout << endl; 
}

Result


Related Tutorials