Class constructors, header files and default parameters took me far to long to get the hang of.
Header files, as you already know, are referenced using the “#include” at the beginning of programs. Header files end with a “.h”. Distinct from header files, source files for C++ end with “.cpp”. Sources files are compiled and not “included”.
My first program tackling header files came out of Starting Out with C++: Early Objects 8th Edition . Chapter 7, Introduction to Classes and Objects, ends with programming challenges.
Programming challenge 1. Date, asks you to design a class called Date that has integer members for the month, day and year. The default constructor should have parameters integers for the month, day and year. The parameters should have default values, so the an object could be created from the class constructor without passing any arguments. If arguments are passed, the program should check the validity of integers. Month should be 1 – 12 and day should be no larger then the number of days in that month. The class should have methods to output the date in the following three formats:
11/8/2017
November 11, 2017
17 November 2017
To accomplish this, you need to create a project. I use Code::Blocks. It is a bit tricky setting up a project and a class if it is your first time. The following video walks you through the steps.
You need to have a header file, source file and main file. The header file is where we declare the Date class and the member variables and functions. The .cpp source file is where we define those variables and functions. Then, the main file is where we have the program that makes use of our class constructor.
The most difficult for me was getting the default parameters for the class constructor to work correctly. You only define the default parameters in the header file. Then, when defining the default constructor in the source file, Dates.cpp, you follow the parameter list with the following, “:month(m), day(d), year(y)”. Then, if you want to create a default object in the main program you would write, “Dates example”, notice no empty parentheses.
First, create the header file.
[code language=”cpp”] // Dates.h#ifndef Dates_H
#define Dates_H
#include <string>
class Dates
{
// Private variables, only accessible to other class members
private:
int month;
int day;
int year;
public:
// Class constructor with default values
Dates(int m=1, int d=1, int y=2001);
static const std::string monthNames[12];
//Methods to get class variables
int getMonth();
int getDay();
int getYear();
// Methods to set value of private variables
void setMonth(int);
void setDay(int);
// Methods to check for accurate inputted numbers
bool checkMonth(int);
bool checkDay(int, int);
// Methods to output date in different formats
void printSlashStyle();
void printUSStyle();
void printEuropeStyle();
};
#endif
[/code]
Next make a source file, be sure to “#include” your header file.
[code language=”cpp”] // Dates.cpp, define methods from our header file//Make sure to include the header Dates.h file
#include "Dates.h"
#include <iostream>
using namespace std;
// Array used to convert numeric months
const string Dates::monthNames[] = {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
// Default class constructor definition
Dates::Dates(int m, int d, int y):month(m), day(d), year(y){}
//Methods to get class variables
int Dates::getMonth(){
return month;
}
int Dates::getDay(){
return day;
}
int Dates::getYear(){
return year;
}
// Methods to set value of private variables
void Dates::setMonth(int m)
{
month = m;
}
void Dates::setDay(int d)
{
day = d;
}
// Methods to check for accurate inputted numbers
bool Dates::checkMonth(int m){
if (m > 0 && m <= 12)
return true;
month = 1;
cout << "Invalid month entered.\nDefault of 1 used instead.\n\n";
return false;
}
bool Dates::checkDay(int m, int d){
if (d<1){
day = 1;
return false;
}
else if(d <= 29)
return true;
else if( m==4 || m==6 || m==9 || m==11){
if (d <= 30){
return true;
}
} else if (d <= 31)
return true;
day = 1;
cout << "Invalid day entered.\nDefault of 1 used instead.\n\n";
return false;
}
// Methods to output Dates in different formats
void Dates::printSlashStyle(){
cout << month << "/" << day << "/"<<year<< endl;
}
void Dates::printUSStyle(){
cout << monthNames[month-1] << " " << day << ", " << year << endl;
}
void Dates::printEuropeStyle(){
cout << day<< " " << monthNames[month-1] << " "<<year<< endl;
}
Finally, design the main program that creates our Dates object.
[code language=”cpp”] // main.cpp//Include the created header file
#include "Dates.h"
#include <iostream>
using namespace std;
int main(){
/* Create a default object of the Dates class
Dates example; Would create default object
Default: month: 1, day: 1, year: 2001*/
Dates example(3, 65, 2017);
// Output program details
cout << "Dates Program\n\nThe user creates Dates object.\n"
"Date is outputted in 3 formats.\n\n";
//Check user object, set new values if input inaccurate
if (!example.checkMonth(example.getMonth()));
if (!example.checkDay(example.getMonth(), example.getDay()));
//Output date in several formats
cout << "***Dates Display***\n";
example.printSlashStyle();
example.printUSStyle();
example.printEuropeStyle();
return 0;
}
[/code]
The output of my code:
No Comments Yet