Ive just started doing OO programming as opposed to function orientated and its caused a whole load of new problems for me.
I was given a piece of code and asked to add functionality for a user to add his/her details. I cannot get the the class to accept my information and use the getters that were specified for example the one for the date of birth.
Help appreciated
Code:
///////////////////////////////////////////////////////////////////////////////
// CP2026 - Workshop 1
// Filename: person.cpp
// Author: Ryan Pincher
#include <iostream.h>
#include <conio.h>
#include <cstring.h>
#include <date.h> // ensure that the 'include' path also points to classlib
#include <stdio.h>
///////////////////////////////////////////////////////////////////////////////
// Class Declarations
class Person
{
private:
// Attributes
string theName;
string theAddress;
TDate theDateOfBirth;
char theGender;
string theTelNo;
public:
// Methods
Person();
Person(string N, string A, TDate D, char G, string T);
void setAddress(string A);
void setTelNo(string T);
void Print(void);
TDate getDateOfBirth(void);
}; // end Person class declaration
///////////////////////////////////////////////////////////////////////////////
// Class Definitions
// Default Constructor
// As this will create an undefined 'person' we should make sure that
// all attributes are either cleared or set to invalid values
Person::Person()
{
theName = "";
theAddress = "";
theDateOfBirth = TDate(0,1,0); // ensures date is invalid
theGender = '?'; // ensures gender is invalid
theTelNo = "";
}; // end default constructor
// Paramaterised Constructor
Person::Person(string N, string A, TDate D, char G, string T)
{
theName = N;
theAddress = A;
theDateOfBirth = D;
theGender = G;
theTelNo = T;
theName.to_upper();
}; // end constructor
// Setter for address attribute
void Person::setAddress(string A)
{
theAddress = A;
}; // end setAddress()
// Setter for telephone number attribute
void Person::setTelNo(string T)
{
theTelNo = T;
}; // end setTelNo()
// Print method that produces formatted output of all attributes
void Person::Print(void)
{
TDate::SetPrintOption(TDate::European);
cout << "\nName : " << theName << endl;
cout << "Address : " << theAddress << endl;
cout << "Telephone: " << theTelNo << endl;
cout << "Date of Birth : " << theDateOfBirth;
cout << "\tGender: " << theGender << endl;
}; // end Print()
// Getter for date of birth
TDate Person::getDateOfBirth(void)
{
return theDateOfBirth;
}; // end getDateOfBirth()
///////////////////////////////////////////////////////////////////////////////
// Main Program
void main(void)
{
string sName;
// create a 'default' person object
Person P1;
Person P2;
//Get user info
cout<<"Please Enter first name: ";
cin>>sName;
endl;
cout<<"DOB: ";
TDate Person::getDateOfBirth(void);
// create a specific person object
//Person P2("Fred", "The Larches", TDate(12, 4, 1985), 'M', "01902 321000");
// Now print them both using the Print method
P1.Print();
P2.Print();
getch();
// To Do:
// add to the above program to allow the user to enter the details of
// a new person then create the person object and print it out.
// Add code to demonstrate how you would use the other methods available
// in the Person class.
} // end main
Code Tags added by Kermi3