C++ c++;构造后初始条件不同的同一类对象
我创建了一个名为well的类,它有一个打印其状态的函数;在我将它们构造为“well w1,w2,w3….,w10”后,其中8或9个打印相同的状态,但其中1或2个打印另一个状态(我定义了总共4个状态,最初它有一个默认状态,其中8-9个对象正确打印) 我使用代码块并通过“#include“well.h”添加well类。 简言之;我定义了一个类,并通过编写对象的名称来创建对象,其中一些对象的行为与其他对象不同C++ c++;构造后初始条件不同的同一类对象,c++,class,constructor,codeblocks,C++,Class,Constructor,Codeblocks,我创建了一个名为well的类,它有一个打印其状态的函数;在我将它们构造为“well w1,w2,w3….,w10”后,其中8或9个打印相同的状态,但其中1或2个打印另一个状态(我定义了总共4个状态,最初它有一个默认状态,其中8-9个对象正确打印) 我使用代码块并通过“#include“well.h”添加well类。 简言之;我定义了一个类,并通过编写对象的名称来创建对象,其中一些对象的行为与其他对象不同 enter code here #include <iostream>
enter code here
#include <iostream>
#include "myQueue.h"
#include "well.cpp"
using namespace std;
int main(){
Well w1,w2,w3,w4,w5,w6,w7,w8,w9,w10;
w1.checkState();
w1.printState();
w2.checkState();
w2.printState();
w3.checkState();
w3.printState();
w4.checkState();
w4.printState();
w5.checkState();
w5.printState();
w6.checkState();
w6.printState();
w7.checkState();
w7.printState();
w8.checkState();
w8.printState();
w9.checkState();
w9.printState();
w10.checkState();
w10.printState();
return 0;
}
class implementation
include "well.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
Well::Well()
{
int state = 4;
int day = 1;
float storage = 2.0;
int offday = 0;
int useCount = 0;
}
int Well::checkState()
{
if (storage==0)
{if (useCount==2) state = 3; // dry and broken
else state = 1;} //dry
else if (useCount==2) state = 2; //broken
else state = 4; // available
}
void Well::printState()
{
if (state==1) cout << "d" << endl;
else if (state==2) cout << "b" << endl;
else if (state==3) cout << "db" << endl;
else if(state==4) cout << "avai"<< endl;
}
void Well::GFG()
{
checkState();
if (state !=2 ) {storage= min(5.0,storage + 0.5 );
checkState(); };
}
void Well::SP()
{
if (state == 4 )
{
storage = max(0.0, storage-1.0);
useCount++;
checkState();
}
}
float Well::getStorage() const
{
return storage;
}
void Well::newDay()
{
++day;
GFG();
--offday;
useCount=0;
checkState();
}
on screen
avai
avai
d
avai
avai
avai
avai
d
avai
avai
在此处输入代码
#包括
#包括“myQueue.h”
#包括“well.cpp”
使用名称空间std;
int main(){
w1、w2、w3、w4、w5、w6、w7、w8、w9、w10井;
w1.检查状态();
w1.printState();
w2.checkState();
w2.printState();
w3.checkState();
w3.printState();
w4.checkState();
w4.printState();
w5.checkState();
w5.printState();
w6.checkState();
w6.printState();
w7.checkState();
w7.printState();
w8.checkState();
w8.printState();
w9.checkState();
w9.printState();
w10.checkState();
w10.printState();
返回0;
}
类实现
包括“well.h”
#包括
#包括
使用名称空间std;
Well::Well()
{
int state=4;
整日=1;
浮动存储=2.0;
int offday=0;
int usecont=0;
}
int Well::checkState()
{
如果(存储==0)
{if(useCount==2)state=3;//干燥和破损
else state=1;}//dry
如果(useCount==2)状态=2;//则为else
else state=4;//可用
}
void Well::printState()
{
如果(状态==1)不能此代码:
Well::Well()
{
int state = 4;
int day = 1;
float storage = 2.0;
int offday = 0;
int useCount = 0;
}
不初始化类的成员。它初始化构造函数中的局部变量。提高编译器的警告级别以获取有关此类未使用变量的警告
以下是初始化成员的变量:
Well::Well()
: state( 4 )
, day( 1 )
, storage( 2.0 )
, offday( 0 )
, useCount( 0 )
{}
检查你喜欢的C++教材的初始化语法。< /P>而不是让我们猜测,你可以张贴一个吗?嗯,你可能有一些未初始化状态,但它真的很难猜出你做什么错误更多的信息。没有必要投票关闭时,因为在这里,错误是显而易见的。谢谢帮助,这工作良好。