Make your own free website on Tripod.com

An Introduction to OOP

 

This is my first actual tutorial on OOP, I will provide further details as time permits and with your suggestions. There is no link from my main page to here at present.

With a sample program using classes and the various properties with regards to Object Oriented programming I hope it will be easier to learn this strange method of programming. Dont forget, use a decent C++ book as reference.

 Lets start,

U will need a C++ compiler of some sort, if you are using Borland C++ 5.0 as I am then you will have no problem with the project here. Otherwise, please consult your manuals. Open the BC++ and select FILE - NEW - PROJECT from the tool bar at the top of the screen.

Select the options as shown below to allow the project to be compiled in a DOS standard. In this way, you do not need to worry about windows programming. Please right click and select view on any of the graphics enclosed if you need to see it clearer.

 

 

Click on the Advance button and make sure to uncheck the .rc and .def boxes. Click Ok for both the boxes and start writing the source code. Double click on the maindate1.cpp to pop-up a box for the source code to be entered. Create 3 files;

and enter the following source codes. Finally you should have the files as shown in the project screen. Click on the maindate1.exe then click on the button with the yellow lightning bolt. If all goes well, you would have compiled the calendar program correctly. A sample of a successful project is shown below . Any errors, will have to be debug by you. Check for spelling mistakes, commas, semi-colons and etc. With the error messages. Make use of the help facilities found in the compiler. Assuming all went well, a DOS screen will be up (Sample output). Enter the year for the calendar between 1900 and 2100. Why that range? The 1st Jan 1900 is a Monday, thats how I used to find out what day of the year is for the calendar. Please read the encyclopedia or some similar reference on your own for further information. Next, press enter. Watch how the destructor works, see how the dates are displayed. Compared the source code and trace each displayed character on the screen. Press a key followed by enter to see the rest of the months. You are welcome to modify and test the program to understand better the various methods used in OOP. Enjoy your OOP. If there is any suggestions, again please leave it in an easy to understand language for me. And just in case you really cant get your program to work, (1) I have enclosed a compiled maindate1.exe for you to see how it works, download use and RUN it, (2) the source code in zip format.

 

// filename: date1.h

// definition of class date

#include<iostream.h>

 

class Date

{

friend void dispMonth (int );

public:

// Date(){} // constructor with initialization of the three fields in a date

Date(int d =1, int m =1, int y =1900);

// constructor with 3 args

void getDate(int, int, int); // member function to get the valid date

void display() ;

int caldays();

~Date();

private:

int _day;

int _mth;

int _year;

static int days[]; // array of days per month

};

// filename: date1.cpp

// member function definitions for date1 class

// last date : 24 Aug 98

#include<iostream.h>

#include"date1.h"

 

//initialize static members

//since arrays start at zero, the first number is 0 which

//is not used.

int Date::days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};

 

//Date constructor accepting 3 arguments

Date::Date(int d, int m, int y)

{getDate(d, m, y);}

 

//Date destructor

Date::~Date()

{cout << "This is where the class Date destructor occurs\n"; }

 

int Date::caldays()

{

int firstdays=0;

for(int countyear=1900;countyear<_year;countyear++)

{if(countyear%4 == 0 && (countyear % 400 == 0 || countyear % 100 != 0))

firstdays += 366;

else

firstdays += 365;

}

cout << "total days " << firstdays;

return firstdays ;

}

 

//member function definition to display the date

void Date::getDate(int d1, int m1, int y1 )

{

// ensure value of month is between 1 and 12

_mth = (m1 >= 1 && m1 <= 12) ? m1 : 1;

// ensure value of year is between 1900 and 2100

_year =(y1 >= 1900 && y1 <= 2100) ? y1 : 1900;

// test for leap year

if(_mth == 2 && _year % 4 == 0 && (_year % 400 == 0 || _year % 100 != 0))

_day=(d1 >= 1 && d1 <= 29) ? d1 : 1;

else

_day=(d1 >= 1 && d1 <= days[_mth]) ? d1 : 1;

}

 

 

//member function of Date to display the dates

void Date::display()

{char dummy;

char validday;

int totaldays;

totaldays=0;

totaldays=caldays(); // zero is Monday, one is Tues and etc

for(_mth = 1; _mth <= 12; _mth++)

{

validday = 'y';

dispMonth(_mth);

_day = 1;

cout << ", " << _year << "\n";

cout << "Sun\tMon\tTue\tWed\tThu\tFri\tSat\n";

for(int i = 0; i <= ((totaldays % 7 ) == 6 ? -1 : totaldays%7 ); i++)

cout << "\t";

while(validday != 'n')

{

cout << _day ;

totaldays++;

//increment the days by one

_day++ ;

if((totaldays % 7)==6)

cout << "\n";

else

cout << "\t";

 

// test for leap year

if(_mth == 2 && _year % 4 == 0 && (_year % 400 == 0 || _year % 100 != 0))

{if( _day > 29 )

validday = 'n'; //it is a leap year

}

else if( _day > days[_mth] )

validday = 'n'; //it is not aleap year

}

cout << "\nTotal days: " << totaldays <<

" Press [x] and enter key to continue :";

cin >> dummy;

}

}

 

//function to determine wordings of the month to be displayed

void dispMonth(int validmonth)

{

static char *monthName[13] = {"", "January",

"February", "March", "April", "May", "June", "July", "August",

"September", "October", "November", "December"};

cout << "\n" << monthName[validmonth] ;

// filename: maindate1.cpp

//Driver for class date

//Last modified Date : 24 August 1998

// significance : Lunar Eclipse day

#include<iostream.h>

#include "date1.cpp"

 

int main()

{

char dummy;

int myday = 1;

int mymonth = 1;

int myyear;

//instantiate d1 as an object of the class Date

Date d1;

//Intro to the programm

cout << "This program is by Nicholas\n";

cout << "to display the calender of any given year.\n";

cout << "Select a year between 1900 and 2100 to see the calendar.\n";

cout << "Enter the year : \n";

//accept the year

cin >> myyear;

//pass the year to object d1

d1.getDate(myday,mymonth,myyear);

//display the calendar

d1.display();

//inform user program is done

//run the exe to start again

cout << "\n" << "done";

cin >> dummy;

return 0;

}

 

The completed PROJECT in BC++ 5.0

Sample Output screen

 

[HOME] [Back to top]