无法在受保护的类中显示变量
can't display variable inside protected class
尝试将其打印出来时无法访问变量" shipcost
"。我要么获得0.00,要么在十六进制中获得地址。不确定为什么会发生这种情况,我将其声明在主类Package
中的保护中,然后在私有Video Games
子级中专门为4.99
。在其他子类中,它们与众不同...我应该注意,这是它受到保护的全部原因。因此,4.99不是一个常数。
,但请注意,在计算中使用成本和总成本时,它会返回正确的美元金额。因此,它被正确使用,但我尝试使用" cout<&lt shipcost< endl;"在类软件包内部,它无法输出0.00以外的任何内容。那是我的问题。
class Package
{
protected:
string name_and_address = "?";
double cost = 0.0;
double discount = 0.0;
double discount_rate = 0.0;
bool overnight_delivery = false;
bool insured = false;
string package_contents = "?";
double shipcost = 0.0;
};
class Video_Games :public Package
{
private:
int num_games = 0;
double shipcost = 4.99;
public:
Video_Games(string location, int number_of_games, bool express, bool insurance)
{
num_games = number_of_games;
name_and_address = location;
overnight_delivery = express;
insured = insurance;
package_contents = to_string(num_games) + " Video Game(s)";
cost = calculate_cost();
discount = calculate_discount();
}
~Video_Games() {};
protected:
double calculate_cost()
{
cost = num_games * 19.99;
if (overnight_delivery) { cost += shipcost; }
if (insured) { cost *= 1.06; }
return cost;
}
};
我不确定您如何尝试访问该变量,但是简单的公共功能将打印正确的值:
class Video_Games :public Package
{
private:
int num_games = 0;
double shipcost = 4.99;
public:
Video_Games( string location , int number_of_games , bool express , bool insurance )
{
num_games = number_of_games;
name_and_address = location;
overnight_delivery = express;
insured = insurance;
package_contents = to_string( num_games ) + " Video Game(s)";
cost = calculate_cost();
//discount = calculate_discount();
}
~Video_Games() {};
void PrintVars( ostream& out )
{
out << num_games << 't' << shipcost << 'n';
}
protected:
double calculate_cost()
{
cost = num_games * 19.99;
if ( overnight_delivery ) {
cost += shipcost;
}
if ( insured ) {
cost *= 1.06;
}
return cost;
}
};
Video_Games vg("abcde",10,true,true);
vg.PrintVars( cout );
我仍然不确定是否得到。在我看来,设置船舶的值而不是声明新的值,将允许每个派生的类访问自己的价值:
class Package
{
protected:
string name_and_address = "?";
double cost = 0.0;
double discount = 0.0;
double discount_rate = 0.0;
bool overnight_delivery = false;
bool insured = false;
string package_contents = "?";
double shipcost = 0.0;
void PrintVars( ostream& out )
{
cout << "Cost = " << cost << 'n'
<< "Discount = " << discount << 'n'
<< "Discount Rate = " << discount_rate << 'n'
<< "Ship Cost = " << shipcost << 'n';
}
};
class Video_Games :public Package
{
private:
int num_games = 0;
public:
Video_Games( string location , int number_of_games , bool express , bool insurance )
{
shipcost = 4.99;
num_games = number_of_games;
name_and_address = location;
overnight_delivery = express;
insured = insurance;
package_contents = to_string( num_games ) + " Video Game(s)";
cost = calculate_cost();
//discount = calculate_discount();
}
~Video_Games() {};
void Print( ostream& out )
{
PrintVars( out );
}
protected:
double calculate_cost()
{
cost = num_games * 19.99;
if ( overnight_delivery ) {
cost += shipcost;
}
if ( insured ) {
cost *= 1.06;
}
return cost;
}
};
class Board_Games :public Package
{
private:
int num_games = 0;
public:
Board_Games( string location , int number_of_games , bool express , bool insurance )
{
shipcost = 3.99;
num_games = number_of_games;
name_and_address = location;
overnight_delivery = express;
insured = insurance;
package_contents = to_string( num_games ) + " Video Game(s)";
cost = calculate_cost();
//discount = calculate_discount();
}
~Board_Games() {};
void Print( ostream& out )
{
PrintVars( out );
}
protected:
double calculate_cost()
{
cost = num_games * 19.99;
if ( overnight_delivery ) {
cost += shipcost;
}
if ( insured ) {
cost *= 1.06;
}
return cost;
}
};
Video_Games vg("abcde",10,true,true);
vg.Print( cout );
cout << 'n';
Board_Games bg( "cdefg" , 20 , false , false );
bg.Print( cout );
这产生了此返回:
Cost = 217.183
Discount = 0
Discount Rate = 0
Ship Cost = 4.99
Cost = 399.8
Discount = 0
Discount Rate = 0
Ship Cost = 3.99
您没有问一个问题。但是,因为您说:
"不确定为什么会发生这种情况"
以及,
"我尝试使用
cout << shipcost << endl;
"在class Package
内,除了0.00
以外,它都无法输出任何内容。那是我的问题"
我必须将您的预期问题解释为:
当我在class Package
中添加"cout << shipcost << endl;"
时,以下代码为什么不输出值4.99
?
我至少看到一种可能性:4.99
的值在class Video_Games
中。尝试在class Video_Games
中添加"cout << shipcost << endl;"
,而不是将此语句放入class Package
的内部。另外,您将两次使用"double"
单词宣布两次造船厂。尝试在class Video_Games
(删除" double")中使用Setter函数
- 为什么静态数组成员变量在调用对象的实例后不显示任何内容?
- 为什么我的双变量通过添加 c++ 显示 nan?
- 类中的一个变量显示,但另一个不显示
- 变量在循环中显示,但不在循环中显示
- 显示 C++ 中"anonymous"变量创建的警告
- gdb映射显示不正确的成员变量
- 使用 while 循环显示顺序变量的值
- Intellisense未显示变量
- 在每次迭代中显示变量
- 无法在受保护的类中显示变量
- C++:显示变量
- 如何在C++中显示变量
- 强制编译器显示变量的类型
- C++生成器 XE 2 -- 如何在 TEdit 控件中显示变量数据
- 如何使用DrawText()来显示变量
- 使控制台标题栏显示变量的值
- c++矢量错误,显示变量有歧义-我已经尝试了一切仍然没有希望
- 使用c++获取当前运行进程中的X显示变量
- 显示变量消息的自定义异常,不同编译器中的行为不同
- 在呈现文本C++时显示变量SDL_TTF