A class called Date is demonstrated in Unit 2. Here is the class declaration: cl
ID: 3852558 • Letter: A
Question
A class called Date is demonstrated in Unit 2. Here is the class declaration:
class Date {
public:
Date();
~Date();
void setDate( int, int, int );
int getDay(); // method or function to retrieve a day value
int getMonth(); // method or function to retrieve a month value
int getYear(); // method or function to retrieve a year value
private:
int day; // data member of this class
int month; // data member of this class
int year; // data member of this class
};
Implementations of the methods from this class are found in Unit 2.
Create a header file: date.h and place the date declaration in it.
Create an implementation file: date.cpp and place the method implementations in it.
Create a header file: person.h and declare a class called Person with the following
data members and methods:
data member data type
lastName string of 20 characters
firstName string of 15 characters
middleInit char
streetAddress string of 25 characters
city string of 20 characters
state string of 2 characters
zipcode string of 10 characters
homePhone string of 12 characters
workPhone string of 12 characters
dateOfBirth Date
Rememberr, if a string is to contain 2 characters, it must be declared (for
example) char state[3]; to allow room for the null character that terminates the
string. All data members should be declared private except dateOfBirth, which
should be declared public. The actual data member of dateOfBirth will remain
private in the Date class.
The following tables lists the class methods:
method argument(s) return type
constructor none none
destructor none
setLastName pointer to string none (void)
setfirstName pointer to string none (void)
setMiddleInit pointer to string none (void)
setStreetAddress pointer to string none (void)
setCity pointer to string none (void)
setState pointer to string none (void)
setZipCode pointer to string none (void)
setHomePhone pointer to string none (void)
setWorkPhone pointer to string none (void)
getLastName none pointer to string
getfirstName none pointer to string
getMiddleInit none char
getStreetAddress none pointer to string
getCity none pointer to string
getState none pointer to string
getZipCode none pointer to string
getHomePhone none pointer to string
getWorkPhone none pointer to string
Create a method implementation file: person.cpp and define minimal
implementations of the methods of the class (no error checking).
write a small program (main()) that performs a simple implementation of the class.
Attach the five files to an email to me for grading.
We will continue to use these classes as foundations in later programming
assignments
Here is a short example of a project with a class called person with one data
member. Assuming you are using Dev c++, click on File, new, project, console
application, give it a name, and put the following files in the project:
//contents of person.h:
// prevent multiple inclusions of header file
#ifndef PERSON_H
#define PERSON_H
class Person {
public:
void setLastName(char *s);
char* getLastName();
private:
char lastName[21];
};
#endif
//contents of person.cpp:
#include <string.h>
#include "person.h"
void Person::setLastName(char *s)
{
strcpy (lastName, s);
}
char* Person::getLastName()
{
static char temp[21];
strcpy (temp, lastName); // last name is copied to temp which is returned
return temp;
}
//contents of main.cpp
#include <iostream>
using namespace std;
#include "person.h"
int main(int argc, char *argv[])
{
Person p1;
p1.setLastName("Smith");
cout << p1.getLastName() << endl;
system("PAUSE"); // only needed for devc++
return 0;
}
Explanation / Answer
//Date.h
class Date {
public:
Date();
~Date();
void setDate( int, int, int );
int getDay(); // method or function to retrieve a day value
int getMonth(); // method or function to retrieve a month value
int getYear(); // method or function to retrieve a year value
private:
int day; // data member of this class
int month; // data member of this class
int year; // data member of this class
};
//Date.cpp
#include<iostream>
#include<string>
#include"date.h"
using namespace std;
//Default Constructor
Date::Date(){}
//Method Definition
void Date::setDate(int m, int d,int y)
{
month = m;
day = d;
year = y;
}
//Accessors
int Date::getMonth()
{
return month;
}
int Date::getDay()
{
return day;
}
int Date::getYear()
{
return year;
}
Date::~Date(void)
{ }
//person.h
#ifndef PERSON_H
#define PERSON_H
#include "date.h"
class Person {
private:
char lastName[21];
char firstName[16];
char middleInit;
char streetAddress[26];
char city[21];
char state[3];
char zipcode[11];
char homePhone[13];
char workPhone[13];
public:
Date dateOfBirth;
Person();
~Person();
void setLastName(char*);
void setfirstName(char*);
void setMiddleInit(char*);
void setStreetAddress(char*);
void setCity(char*);
void setState(char*);
void setZipCode(char*);
void setHomePhone(char*);
void setWorkPhone(char*);
char* getLastName();
char* getfirstName();
char getMiddleInit();
char* getStreetAddress();
char* getCity();
char* getState();
char* getZipCode();
char* getHomePhone();
char* getWorkPhone();
};
#endif
//Person.cpp
#include <string.h>
#include "person.h"
Person::Person(){}
Person::~Person(){}
void Person::setLastName(char *s)
{
strcpy (lastName, s);
}
char* Person::getLastName()
{
static char temp[21];
strcpy (temp, lastName); // last name is copied to temp which is returned
return temp;
}
void Person::setfirstName(char *s)
{
strcpy (firstName, s);
}
char* Person::getfirstName()
{
static char temp[16];
strcpy (temp, firstName); // first name is copied to temp which is returned
return temp;
}
void Person::setMiddleInit(char s)
{
middleInit = s;
}
char Person::getMiddleInit()
{
return middleInit;
}
void Person::setStreetAddress(char *s)
{
strcpy(streetAddress,s);
}
char* Person::getStreetAddress()
{
static char temp[26];
strcpy (temp, streetAddress); // street is copied to temp which is returned
return temp;
}
void Person::setCity(char *s)
{
strcpy(city,s);
}
char* Person::getCity()
{
static char temp[21];
strcpy (temp, city); // city is copied to temp which is returned
return temp;
}
void Person::setState(char *s)
{
strcpy(state,s);
}
char* Person::getState()
{
static char temp[3];
strcpy (temp, state); // state is copied to temp which is returned
return temp;
}
void Person::setZipCode(char *s)
{
strcpy(zipcode,s);
}
char* Person::getZipCode()
{
static char temp[11];
strcpy (temp, zipcode); // zipcode is copied to temp which is returned
return temp;
}
void Person::setHomePhone(char *s)
{
strcpy(homePhone,s);
}
char* Person::getHomePhone()
{
static char temp[13];
strcpy (temp, homePhone); // homephone is copied to temp which is returned
return temp;
}
void Person::setWorkPhone(char *s)
{
strcpy(workPhone,s);
}
char* Person::getWorkPhone()
{
static char temp[13];
strcpy (temp, workPhone); // work phone is copied to temp which is returned
return temp;
}
//Main.cpp
//contents of main.cpp
#include <iostream>
using namespace std;
#include "person.cpp"
int main(int argc, char *argv[])
{
Person p1;
p1.setfirstName("John");
p1.setLastName("Doe");
p1.setMiddleInit('P');
p1.setStreetAddress("xxx");
p1.setCity("YYY");
p1.setState("ZZZ");
p1.setZipCode("568467");
p1.setHomePhone("829385925");
p1.setWorkPhone("990128399");
cout << p1.getfirstName() << endl;
cout << p1.getLastName() << endl;
cout << p1.getMiddleInit() << endl;
cout << p1.getStreetAddress() << endl;
cout << p1.getCity() << endl;
cout << p1.getState() << endl;
cout << p1.getZipCode()<< endl;
cout << p1.getHomePhone() << endl;
cout << p1.getWorkPhone() << endl;
system("PAUSE"); // only needed for devc++
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.