c++ - 初始化构造函数 C++

标签 c++ constructor

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

上面是我的类(class), 我需要在 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 个类。因此,您需要创建一个名为 DrivingLicence.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
}

这就是你要问的吗???

关于c++ - 初始化构造函数 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26256511/

相关文章:

c++ - 多个不同的构造函数作为函数参数

c++ - 为什么显示的颜色不是 VTK 中预期的颜色?

c++ - 静态变量应该在哪里声明

c++ - 无法在 cbegin 中定义 initializer_list

java - 在测试用例中创建构造函数,使其仅被调用一次

java - 如何处理2个几乎相同的构造函数

c++ - 返回 *this

python - Poco,子通信挂起(python解释器是子)

c++ - Qt C++编译完成,程序不显示

c++ - 覆盖基类调用的函数?