// note that the preprocessor statements are missing in Date.h class Date { public: Date() : month(1), day(1), year(2000) { } Date(int mm,int dd,int yy) : month(mm), day(dd), year(yy) { } Date(const Date& old) : month(old.month),day(old.day), year(old.year) { } void setDate(int,int,int); void showDate(); private: int month; int day; int year; };
#include "Date.h" #include <iostream.h> // setDate sets the data members of the invoking object to the // values passed in the parms void Date::setDate(int mo, int da, int yr) { month = mo; day = da; year = yr; } // print the invoking object in the format mm/dd/yyyy void Date::showDate() { cout << month << '/' << day << '/' << year; }
#ifndef PERSON_H #define PERSON_H #include "Date.h" #include <string> class Person { public: Person() { } Person(string nn,Date bd,int aa) : name(nn),birthday(bd) { age = aa; } Person(const Person& old) : name(old.name), birthday(old.birthday), age(old.age) { } void print(); private: string name; Date birthday; int age; }; #endif
#include "Person.h" #include "Date.h" // print the invoking object void Person::print() { cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Birthday: "; birthday.showDate(); cout << endl; }
#include <iostream.h> #include <string> #include "Date.h" #include "Person.h" int main() { string nn; int mm,dd,yy,age; cout << "Enter your name: "; getline(cin, nn); cout << "Enter your birthday (month day year): "; cin >> mm >> dd >> yy; Date birth(mm,dd,yy); cout << "Enter your age: "; cin >> age; cout << endl; Person you(nn,birth,age); you.print(); }
>g++ Date.C Person.C main.C In file included from Person.h:4, from Person.C:2: Date.h:5: redefinition of `class Date' Date.h:18: previous definition here In file included from Person.h:4, from main.C:4: Date.h:5: redefinition of `class Date' Date.h:18: previous definition here
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page
© Copyright Emmi Schatz 2002