初始化构造函数 C++
initializing a constructor c++
#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);
// ...
}
几点建议:
- 您正在使用C++,请改用
std:string
char*
- 不要滥用传递参数作为参考。有时这不是最好的做法。
如果您尝试使用传递给构造函数的参数初始化 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({}
这就是我的意思。感谢您的所有答案。
相关文章:
- "error: no matching function for call to"构造函数错误
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 选择要调用的构造函数
- 如何委托派生类使用其父构造函数?
- 构造函数正在调用一个使用当前类类型的函数
- 没有用于初始化C++中的变量模板的匹配构造函数
- 初始化具有非默认构造函数的std::数组项的更好方法
- 当从函数参数中的临时值调用复制构造函数时
- 在c++构造函数中使用随机字符串生成器
- 一对向量构造函数:初始值设定项列表与显式构造
- 从构造函数抛出异常时如何克服内存泄漏
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 继承:构造函数,初始化C++11中基类的类C数组成员
- 具有默认模板类型的默认构造函数的类型推导
- 使用dynamic_cast和构造函数时出错
- 在c++中使用向量时,如何调用构造函数和析构函数
- 奇怪的构造函数行为