如何从一个类中获取变量并将其放入另一个类,然后将其放入构造函数

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

本文关键字:构造函数 然后 另一个 变量 获取 一个      更新时间:2023-10-16

我这里有点问题。我在课堂上有一个作业,我们制作了3个头文件(.h文件)和3个.cpp文件。任务是人日期和时间。

我会把它们展示给你看。

人.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

个人.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

日期.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

时间.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;
}
}

好吧一个人要得到一个人的名字和姓氏约会是为了得到一个约会。。。年月日时间是向用户询问时间,并输出军用时间和标准时间。

现在我们的老师要我们上第四节课。。。命名为DentalAppointment。

任务是这样说的

"创建一个名为DentalAppointment的类。包括患者数据(使用现有Person类)、日期(使用现有类)、时间(使用现有time类)的字段,以及以分钟为单位的约会持续时间。此外,还包括一个包含约会结束时间的字段-该字段将使用向时间对象添加分钟的time类函数根据开始时间和持续时间进行计算。DentalAppointment构造函数需要名字和姓氏、月份、日期和年份以及约会的小时和分钟。允许在构造DentalApppointment时使用或不使用持续时间的附加参数,并在未提供参数时强制将持续时间设置为30分钟。构造函数不应允许任何超过240分钟的预约。构造函数将根据开始时间和持续时间计算约会结束时间。为DentalAppointment类创建一个显示函数。编写一个main()函数,该函数至少循环三次,提示用户输入DentalAppointment数据并显示信息。使用构造函数的初始化列表。"

好吧,这就是我困惑的地方。

所以基本上我必须将值从Person、Date、Time传递到DentalAppointment。

所以我先上了牙医预约课。

牙科预约.h

#include <iostream>
#include "person.h"
#include "Date.h"
#include "Time.h"
using  namespace std;
class DentalAppointment
{
private:
public:
};

我不知道该把什么放在私人和公共部分。我知道吗?

#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();
};

如果是,那么我该为我的cpp文件放入什么?应该是这样吗???

#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;
}

请告诉我我是离得很远,还是离得很近。因为从不同的类中调用私有变量和公共变量并将它们放入不同的类会让我感到困惑。将它们放入构造函数会让我丧命……请帮帮我!!!非常感谢。

我做了你所做的,但现在它的"C:\Users\Ibrahim Munaser\Documents\C++\DentalAppointment\DentalAppointment.cpp|9|错误:没有用于调用"Time::Time()"|的匹配函数。。。。我不知道这意味着什么。。。我会把我的h和cpp文件发给你,看看你是否能找到

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();
};

日期.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;
}

您想直接从PersonDateTime构建DentalAppointment。所以更像:

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
}

编辑:为了简化构造函数,您可以从开始

DentalAppointment(Person p, Date d , Time t) {
     person_ = p;
     date_ = d;
     time_ = t;
}

这只需获取PersonDateTime的对象,并将它们分配给数据成员。然后,当你学习更多的C++时,你最终会得到上面给定的构造函数。但这是一个很好的起点。

相关文章: