0% found this document useful (0 votes)
54 views1 page

C++ Lab

This document demonstrates how to create a simple class in C++ called Date that contains member functions to set and print the values of day, month, and year. A Date object called today is created and the set() member function is used to initialize its values. The print() member function then outputs the date values separated by dashes.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views1 page

C++ Lab

This document demonstrates how to create a simple class in C++ called Date that contains member functions to set and print the values of day, month, and year. A Date object called today is created and the set() member function is used to initialize its values. The print() member function then outputs the date values separated by dashes.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 1

A simple example We consider a simple example which shows how to create a simple class and define the class's

member functions. // SimpleClass.cpp // A program to demonstrate creation and use of a simple class for dates #include <iostream> using namespace std; // Declaration of Date class class Date { public: void set(int, int, int); void print(); private: int year; int month; int day; }; int main() { // Create a Date object called today Date today; // Call Date member function set() today.set(1,9,1999); cout << "This program was written on "; today.print(); cout << endl; return 0; } // Date member function definitions void Date::set(int d, int m, int y) { if(d>0 && d<31) day = d; if(m>0 && m<13) month = m; if(y>0) year =y; } void Date::print() { cout << day << "-" << month << "-" << year << endl; }

You might also like