Class Example

#include <iostream.h>
/************************************************************************   
 File: class2.C
 Instantiate a date class, initilaize it the date object, and print it. 
 The date object is manipulated through pointers
************************************************************************/

class date {
     int day;
     int month;
     int year;
public:
     void init(int , int , int );
     void print();
};

void date::init(int d, int m, int y)
{
     day = d;
     month = m;
     year = y;
}

void date::print()
{
     cout << "The date stored in this object is:" << endl;
     cout << "Day: " << day << endl;
     cout << "Month: " << month << endl;
     cout << "Year: " << year << endl;
}

typedef date Date;        /* object and ...*/
typedef Date *Ptrdate;    /* ...pointer types */

int main()
{
     int day, month, year;
     Date today;
     Ptrdate ptrdate;
     ptrdate = &today;

     cout << "Enter today's date in DD MM YYYY format: ";
     cin >> day >> month >> year;

     ptrdate->init(day, month, year);
     ptrdate->print();
    
     exit(0);
}



Back to Previous Page

Document:
Local Date:
Last Modified On: