#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/