注册 登录
编程论坛 C++教室

[求助]类继承问题,请大家看下,错哪了?找不出来啊。

a8451727 发布于 2007-06-24 14:48, 1045 次点击

#include"License.h"
#include<string>
class HuntingLicense:public License
{
public:
HuntingLicense(){};
HuntingLicense(string hunting,string fName,string mName,string lName,int theAge,int theId);
string getTheName()const;
void read(istream&in);
void print(ostream&out)const;
private:
string TheHunting;
};
inline HuntingLicense::HuntingLicense(string hunting,string fName,string mName,string lName,int theAge,int theId)
:License()
{
TheHunting=" ";
}

inline HuntingLicense::HuntingLicense(string hunting,string fName,string mName,string lName,int theAge,int theId)
:License(fName,mName,lName,theAge,theId)
{
TheHunting=hunting;
}

inline string HuntingLicense::getTheName()const
{
return TheHunting;
}
inline void HuntingLicense::read(istream&in)
{
cin>>TheHunting;
License::read(in);
}
inline void HuntingLicense::print(ostream&out)const
{
cout<<TheHunting;
License::print(out);
}

错误信息:
huntinglicense.h(15) : error C2084: function '__thiscall HuntingLicense::HuntingLicense(void)' already has a body

我不知道哪错了,知道的指点一下

[此贴子已经被作者于2007-6-24 14:55:06编辑过]

16 回复
#2
aipb20072007-06-24 15:25
以下是引用a8451727在2007-6-24 14:48:55的发言:

#include"License.h"
#include<string>
class HuntingLicense:public License
{
public:
HuntingLicense()/*{}*/; //这里不要加括号,括号表示定义
HuntingLicense(string hunting,string fName,string mName,string lName,int theAge,int theId);
string getTheName()const;
void read(istream&in);
void print(ostream&out)const;
private:
string TheHunting;
};
inline HuntingLicense::HuntingLicense(string hunting,string fName,string mName,string lName,int theAge,int theId)
:License()
{
TheHunting=" ";
}

inline HuntingLicense::HuntingLicense(string hunting,string fName,string mName,string lName,int theAge,int theId)
:License(fName,mName,lName,theAge,theId)
{
TheHunting=hunting;
}

inline string HuntingLicense::getTheName()const
{
return TheHunting;
}
inline void HuntingLicense::read(istream&in)
{
cin>>TheHunting;
License::read(in);
}
inline void HuntingLicense::print(ostream&out)const
{
cout<<TheHunting;
License::print(out);
}

错误信息:
huntinglicense.h(15) : error C2084: function '__thiscall HuntingLicense::HuntingLicense(void)' already has a body

我不知道哪错了,知道的指点一下

#3
HJin2007-06-24 15:31
[CODE]:License()[/CODE]

What is your purpose here? Are you initializing some object in the initializer list?



#4
aipb20072007-06-24 15:36
以下是引用HJin在2007-6-24 15:31:50的发言:
[CODE]:License()[/CODE]

What is your purpose here? Are you initializing some object in the initializer list?



It means the current class's constructor construct a base class object.
Because of the inheritance.

#5
a84517272007-06-24 15:56
呵呵```谢谢了
#6
HJin2007-06-24 16:00
thanks aipb2007, got your idea.
#7
a84517272007-06-24 16:17
那个括号在什么情况下用或不用的?
#8
neverDie2007-06-24 16:27
以下是引用a8451727在2007-6-24 16:17:48的发言:
那个括号在什么情况下用或不用的?

return_type fun_name(parameter list);
这叫声明,只有函数原形。
return_type fun_name(parameter list)
{
//函数体
}
这叫定义,花括号中是函数体,函数体为空时,这个函数仍然被定义,只是没有功能。

[此贴子已经被作者于2007-6-24 16:28:21编辑过]

#9
a84517272007-06-24 20:21
谢谢大家。
#10
a84517272007-06-25 21:33

又请问一下大家,我该怎样才能修改下面红色字的那个值?(改成一个新的值)。因为他是继承"BankAccount.h"类的,
所以不懂如何修改,该如何写一个修改器?请大家帮下忙。

#include"BankAccount.h"
class CheckingAccount:public BankAccount
{
public:
CheckingAccount();
CheckingAccount(string theName,string theNumber,double theBalance);
/*例如 theBalance是余额,我想要一个取款或存款的修改器*/
void read(istream&in);
void print(ostream&out)const;
private:
double theBalanceNumber;
};
inline CheckingAccount::CheckingAccount()
:BankAccount()
{
}
inline CheckingAccount::CheckingAccount(string theName,string theNumber,double theBalance)
:BankAccount(theName,theNumber,theBalance)
{
}

[此贴子已经被作者于2007-6-25 21:37:49编辑过]

#11
a84517272007-06-25 23:11
懂的帮下
#12
aipb20072007-06-25 23:18

意思不很明确!

#13
a84517272007-06-25 23:35
以下是引用aipb2007在2007-6-25 23:18:25的发言:

意思不很明确!


比如驱动程序:
CheckingAccount aChecking("Name","Number",100);
aChecking.print(cout);

//运行后是 Name Number 100

我想在类里面构造一个函数,让100那可以增加或减少(存款、取款)的意思。

#14
a84517272007-06-26 17:24
大家帮下忙啊不懂这个问题,下面的也学不懂啊
#15
aipb20072007-06-26 20:03
你可以在你的基类里增加一个函数,对那个double theBalance进行处理,然后在当前类里调用这个函数。

从而间接修改基类的私有成员。


是这个意思吗?

[此贴子已经被作者于2007-6-26 20:06:01编辑过]

#16
野比2007-06-26 20:38
派生类方法修改基类成员...
#17
a84517272007-06-27 00:59
以下是引用aipb2007在2007-6-26 20:03:36的发言:
你可以在你的基类里增加一个函数,对那个double theBalance进行处理,然后在当前类里调用这个函数。

从而间接修改基类的私有成员。


是这个意思吗?


嗯 应该是这意思吧。 因为这个类是继承前一个类,所以不懂处理这个double theBalance

1