2014-10-08 195 views
1
#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类(而不是复制利弊)来初始化构造函数,但我不能设法做到这一点。初始化构造函数C++

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

我的意思是:

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

} 

我不知道如何初始化值

+4

试试看吧?对我而言,你完全不清楚你有什么问题。 – nvoigt 2014-10-08 12:09:09

+0

初始化一个构造函数?你不初始化构造函数......构造函数初始化类的实例(或者至少它们应该这样做)。 – luk32 2014-10-08 12:11:59

+0

你为什么在C++中使用char *'?如果这是一个字符串,请使用'std :: string'。 – crashmstr 2014-10-08 12:16:06

回答

0

包括 “header.h”

INT DrivingLicence :: CID = 1000;

DrivingLicence :: DrivingLicence(人& O,字符* T,日期& d):所有者(○),式(t)的有效性(d) {

}

DrivingLicence :: DrivingLicence(Person & o,char * t):owner(o),type(t),validity(8,10,2024){}

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

0

我想这是一个头文件,但通常有每个.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 
} 

这是你在问什么???

0

如果我明白你的问题,你想有一个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. 请勿滥用传递参数作为参考。有些时候这不是 最好的。
+0

我感谢你的回答虐待给它尝试 – Daniel 2014-10-08 12:26:05

+0

我已经尝试过你的语法,但我仍然有一个汇编问题“智能感知:没有重载函数的实例”DrivingLicence :: DrivingLicence“匹配指定的类型。 char * intetionaly for practice。 – Daniel 2014-10-08 12:37:59

+0

你能告诉我们汇编问题吗? – 2014-10-08 12:39:10

0

如果您尝试使用传递给构造函数的参数初始化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. 
+0

非常感谢你的工作完美。我明白了这个问题 – Daniel 2014-10-08 13:13:33

+0

很高兴我能帮你理解。 – Aesthete 2014-10-09 09:45:50