Class objects can be assigned to each other using default memberwise assignment : class definition « Class « C++ Tutorial






#include <iostream>
using std::cout;
using std::endl;

class Date 
{
public:
   Date( int = 1, int = 1, int = 2007 );
   void print();
private:
   int month;
   int day;
   int year;
};

Date::Date( int m, int d, int y )
{
   month = m;
   day = d;
   year = y;
}
void Date::print() 
{ 
   cout << month << '/' << day << '/' << year; 
}

int main()
{
   Date date1( 8, 8, 2008 );
   Date date2;

   cout << "date1 = ";
   date1.print();
   cout << "\ndate2 = ";
   date2.print();

   date2 = date1;

   cout << "\n\nAfter default memberwise assignment, date2 = ";
   date2.print();
   cout << endl;
   return 0;
}
date1 = 8/8/2008
date2 = 1/1/2007

After default memberwise assignment, date2 = 8/8/2008








9.1.class definition
9.1.1.Define your first class
9.1.2.classes example
9.1.3.Create an object from a Class and call its function
9.1.4.Define a class with a member function that takes a parameter
9.1.5.Define class with a data member and member functions to set and get its value
9.1.6.Instantiating multiple objects of the class using its constructor
9.1.7.Define class to record time
9.1.8.Class objects can be assigned to each other using default memberwise assignment
9.1.9.Local Classes: A class may be defined within a function
9.1.10.containership with employees and degrees
9.1.11.Inner class
9.1.12.Friend Classes