初始化构造函数 C++

initializing a constructor c++

本文关键字:C++ 构造函数 初始化      更新时间:2023-10-16
#include <iostream>
using namespace std;
class DrivingLicence
{
protected:
    Person owner;
    char * type;
    Date validity;
    int id;
    static int cid;
public:
    DrivingLicence(Person &o,char* t,Date &d);
    DrivingLicence(Person &o,char* t);
    DrivingLicence(const DrivingLicence & other);
    Date getValidity() const;
    int checkValidity() const;
    void print() const;
    bool operator== (const DrivingLicence& o) const;
    void operator +(const int num);
    const DrivingLicence& operator= (const DrivingLicence& other);
    ~DrivingLicence();
};
class Person
{
private:
    int id;
    char* name;
    Date birthday;
public:
    Person(char* name,int id1,Date &d);
    Person(const Person &other);
    ~Person();
    Date getBirthday() const;
    const Person& operator= (const Person & other);
    void print() const;
};
class Date
{
    int day;
    int month;
    int year;
public:
    Date (int day,int month,int year);
    ~Date();
    const Date& operator=(const Date& other);
    bool operator==(const Date & other);
    void print() const;
    int getYear()const;
    int getMonth()const;
    int getDay()const;
};

上面是我的课程,我需要初始化 DrivingLicence 类中的两个构造函数(而不是复制缺点(,但我无法做到这一点。

有人可以帮我解决这个问题的语法吗?

我的意思是:

#include <NameOfHeaderFile>
DrivingLicense::DrivingLicense( Person &o,char* t,Date &d ) : //Code here for 1stconstructor
{
}
DrivingLicense::DrivingLicense( Person &o,char* t ) ://Code here for 2nd constructor
{
}

我不知道如何初始化值

我假设这是一个头文件,但通常每个 .h 文件只有 1 个类。因此,您需要创建一个名为DrivingLicense.cpp的文件。

在此文件中写入:

#include <NameOfHeaderFile>
DrivingLicense::DrivingLicense( Person &o,char* t,Date &d )
{
     //Code here for 1stconstructor
}
DrivingLicense::DrivingLicense( Person &o,char* t )
{
     //Code here for 2nd constructor
}

这就是你要问的吗???

如果我理解了你的问题,你需要一个DriverLicence构造函数还初始化 DriverLicense 实例中的人员。

如果是这样,您只需将此重载添加到您的类中:

DriverLicense::DriverLicense(
    // Data for Person
    char *person_name, int person_id, Date day_birth,
    // Data for Driver licence
    char *t, Date d)
{
    owner = Person(person_name, person_id, day_birth); 

    // More code here initializing DeriverLicence members.
}

当然,请记住将其添加到您的班级中:

class DriverLicence
{
public:
    // ...
    DriverLicense::DriverLicense(char *, int, Date, char *t, Date d);
   // ...
}

几点建议:

  1. 您正在使用C++,请改用std:string char*
  2. 不要滥用传递参数作为参考。有时这不是最好的做法。

如果您尝试使用传递给构造函数的参数初始化 DrivingLicense 类的成员,则可以在初始值设定项列表中执行此操作。

将这些

定义放在 cpp 中是明智的。还要注意类型和恒常性或我提供的参数的更改。

DrivingLicense::DrivingLicense(const Person &o, const std::string& t, const Date &d )
  : owner(o)
  , type(t)
  , validity(d)
{ }
DrivingLicense::DrivingLicense(const Person &o, const std::string& t)
  : owner(o)
  , type(t)
{ } // Note that 'validity' here is default constructed.

include "header.h">

国际驾驶执照::cid =1000;

驾驶执照::D rivingLicense(Person &o, char* t, Date & d (: 所有者(o(, 类型(t(, 有效性 (d({

}

驾驶执照::D rivingLicense(Person &o,char* t( :owner(o(, type(t(,validity(8,10,2024({}

这就是我的意思。感谢您的所有答案。