How do I get variables from a class and put it into another class, and put it into a constructor?

时间:2015-10-31 00:12:07

标签: c++ class date constructor

I'm having a bit of a problem here. Well I have an assignment in my class where we made 3 header files(.h files) and 3 .cpp files. The assignments were Person Date and Time. I will display them for you to see. person.h #include <iostream> using namespace std; #ifndef PERSON #define PERSON class Person { private: string firstName; string lastName; public: Person(); Person(string,string); void displayPerson(); }; #endif person.cpp #include <iostream> #include "person.h" using namespace std; Person::Person() { firstName=" "; lastName=" "; } Person::Person(string fn, string ln) { firstName=fn; lastName=ln; } void Person::displayPerson() { cout<<firstName<<", "<<lastName; } Date.h #include <iostream> using namespace std; #ifndef DATE #define DATE class Date { private: string month; string day; string year; public: Date(); Date(string,string,string); void displayDate(); }; #endif // DATE Date.cpp #include <iostream> #include "Date.h" using namespace std; Date::Date() { month="01"; day="01"; year="2013"; } Date::Date(string m,string d, string y) { month=m; day=d; year=y; } void Date::displayDate() { cout<<month<<"/"<<day<<"/"<<year; } Time.h #include <iostream> using namespace std; #ifndef TIME #define TIME class Time { private: static const int MAXMIN=59; static const int MINMIN=0; static const int MAXHOUR=24; static const int MINHOUR=0; int militaryHours; int militaryMinutes; int standardHours; int standardMinutes; string AMPM; public: Time(int); Time(int,int); void displayTime(); void displayTime1(); }; #endif // TIME Time.cpp #include <iostream> #include "Time.h" using namespace std; Time::Time(int mh) { string zero="0"; militaryHours=mh; if (militaryHours>MAXHOUR || militaryHours<MINHOUR) { militaryHours=MAXHOUR; standardHours=12; } else if(militaryHours>12 && militaryHours<MAXHOUR) { standardHours=militaryHours-12; AMPM="P.M."; } else if(militaryHours<12 && militaryHours>=MINHOUR) { standardHours=militaryHours; AMPM="A.M."; } cout<<"MILITARY TIME: "<<militaryHours<<":"<<zero<<zero<<" "<<AMPM; cout<<"\nSTANDARD TIME: "<<standardHours<<":"<<zero<<zero<<" "<<AMPM; } Time::Time(int mh,int mm) { string zero="0"; militaryHours=mh; militaryMinutes=mm; if (militaryHours>MAXHOUR || militaryHours<MINHOUR) { militaryHours=MAXHOUR; standardHours=12; } else if(militaryHours>12 && militaryHours<=MAXHOUR) { standardHours=militaryHours-12; AMPM="P.M."; } else if(militaryHours<12 && militaryHours>=MINHOUR) { standardHours=militaryHours; AMPM="A.M."; } if(militaryMinutes>MAXMIN && militaryMinutes<MINMIN) { militaryMinutes=MAXMIN; } else if(militaryMinutes<MAXMIN && militaryMinutes>MINMIN) { standardMinutes=0; standardMinutes=militaryMinutes; } if(militaryMinutes<10 && militaryMinutes>=MINMIN) { standardMinutes=0; standardMinutes=militaryMinutes; cout<<"MILITARY TIME: "<< militaryHours<<":"<<zero<<standardMinutes<<" " <<AMPM; cout<<"\nSTANDARD TIME: "<<standardHours<<":"<<zero<<standardMinutes<<" "<<AMPM; } else { cout<<"MILITARY TIME: "<< militaryHours<<":"<<standardMinutes<<" "<<AMPM; cout<<"\nSTANDARD TIME: "<<standardHours<<":"<<standardMinutes<<" "<<AMPM; } } ok so person was to get the first name and last name of a person Date was to get a date...month day year Time was to ask a user for a time and to output military and standard time. Now our teacher wants us to make a fourth class... named DentalAppointment. This is what the assignment says "Create a class named DentalAppointment. Include fields for patient data(using the existing Person class), the date(using the existing class), the time(using the existing Time class), and the duration of the appointment in minutes. Also, include a field that contains the ending time for the appointment - this field will be calculated based on the start time and the duration using the Time class function that adds minutes to the time object. The DentalAppointment constructor requires a first and last name, a month,day, and year, and an hour and minute for the appointment. Allow a DentalApppointment to be constructed with or without and additional argument for duration and force the duration to 30 minutes when no argument is supplied. The constructor should not allow any appointment over 240 minutes. The constructor will calculate the appointment ending time base on the start time and duration. Create a display function for the DentalAppointment class. Write a main() function that loops at least three times prompting the user for DentalAppointment data and displaying the information. Use an initialization list for the constructor." Ok here is where I am confused. so basically I have to pass the values from Person,Date, Time to DentalAppointment. so first I made a DentalAppointment class. DentalAppointment.h #include <iostream> #include "person.h" #include "Date.h" #include "Time.h" using namespace std; class DentalAppointment { private: public: }; I dont know what to put in the private and public parts. Do I do? #include <iostream> #include "person.h" #include "Date.h" #include "Time.h" using namespace std; class DentalAppointment { private: Person pFirstName; Person pLastName; Date appointmentMonth; Date appointmentDay; Date appointmentYear; Time appointmentHour; Time appointmentMinutes; int duration; public: DentalAppointment(); DentalAppointment(Person,Person,Date,Date,Date,Time,Time); DentalAppointment(Person,Person,Date,Date,Date,Time,Time,int); void showDentalAppointment(); }; if so then what do I put in for my cpp file? should it be like this??? #include <iostream> #include "person.h" #include "Time.h" #include "Date.h" #include "DentalAppointment.h" using namespace std; DentalAppointment::DentalAppointment() { pFirstName=Person.firstName; pLastName=Person.lastName; appointmentMonth=Date.month; appointmentDay=Date.day; appointmentYear=Date.year; appointmentHour=Time.standardHours; appointmentMinutes=Time.standardMinutes; } DentalAppointment::DentalAppointment(Person fn,Person ln ,Date month,Date day,Date year,Time h,Time m) { pFirstName=fn; pLastName=ln; appointmentMonth=month; appointmentDay=day; appointmentYear=y; appointmentHour=h; appointmentMinutes=m; } DentalAppointment::DentalAppointment(Person fn,Person ln ,Date month,Date day,Date year,Time hours,Time minutes,int d) { pFirstName=fn; pLastName=ln; appointmentMonth=month; appointmentDay=day; appointmentYear=year; appointmentHour=hours; appointmentMinutes=minutes; duration=d; } please tell me if I am way off, or if im close. because calling private and public variables from different classes and putting them into different classes is confusing me. and putting them in constructors is killen me.... please help me!!! Thank You!!! I did what you did but now its "C:\Users\Ibrahim Munaser\Documents\c++\DentalAppointment\DentalAppointment.cpp|9|error: no matching function for call to 'Time::Time()'|".... I dont know what it means... im gonna send you my h and cpp file and see if you could find anything Date.h #include <iostream> #include "person.h" #include "Date.h" #include "Time.h" using namespace std; class DentalAppointment { private: Person pFirstName; Person pLastName; Date appointmentMonth; Date appointmentDay; Date appointmentYear; Time appointmentHour; Time appointmentMinutes; int duration; public: DentalAppointment(Person,Person,Date,Date,Date,Time,Time); DentalAppointment(Person,Person,Date,Date,Date,Time,Time,int); void showDentalAppointment(); }; Date.cpp #include <iostream> #include "person.h" #include "Time.h" #include "Date.h" #include "DentalAppointment.h" using namespace std; DentalAppointment::DentalAppointment(Person fn,Person ln ,Date month,Date day,Date year,Time h,Time m) { pFirstName=fn; pLastName=ln; appointmentMonth=month; appointmentDay=day; appointmentYear=y; appointmentHour=h; AppointmentMinutes=m; } DentalAppointment::DentalAppointment(Person fn,Person ln ,Date month,Date day,Date year,Time hours,Time minutes,int d) { pFirstName=fn; pLastName=ln; appointmentMonth=month; appointmentDay=day; appointmentYear=year; appointmentHour=hours; AppointmentMinutes=minutes; duration=d; }

1 个答案:

答案 0 :(得分:0)

You want to build DentalAppointment from Person, Date and Time directly. So more like: class DentalAppointment { Person person_; Date date_; Time time_; public: DentalAppointment(const Person& p, const Date& d , const Time& t) : person_(p), date_(d), time_(t) {} // other stuff } EDIT: To simplify the constructor, you start with: DentalAppointment(Person p, Date d , Time t) { person_ = p; date_ = d; time_ = t; } This simply takes objects of Person, Date and Time and assigns them to your data members. Then as you learn more C++ you end up at the given constructor above. But here's a good place for you to start.