类成员问题 菜鸟真心求教
#include<iostream>
using namespace std;
enum choice {drawrect = 1,getarea,getperim,changedimensions,quit};
class rectangle
{
public:
rectangle(int width,int height);
~rectangle();
int getheight() const {return itsheight;}
int getwidth() const {return itswidth;}
int getperim() const {return 2*itsheight+2*itswidth;}
int getarea() const {return itsheight*itswidth;}
private:
int itswidth;
int itsheight;
};
void rectangle::setsize(int newwidth,int newheight)\\为什么这样定义会报错呢
{
itswidth = newwidth;
itsheight = newheight;
}
rectangle::rectangle(int width,int height)
{
itswidth = width;
itsheight = height;
}
rectangle::~rectangle() {}
int domenu();
void dodrawrect(rectangle);
void dogetarea(rectangle);
void dogetperim(rectangle);
int main()
{
rectangle therect(30,5);
int choice = drawrect;
int fquit = false;
while (!fquit)
{
choice = domenu();
if(choice < drawrect || choice > quit)
{
cout << "\n我也不知道这句什么意思\n\n";
continue;
}
switch (choice)
{
case drawrect:
dodrawrect(therect);
break;
case getarea:
dogetarea(therect);
break;
case getperim:
getperim(therect);
break;
case changedimensions:
int newlength,newwidth;
cout << "\nnew width";
cin >> newwidth;
cout << "newheight";
cin >> newlength;
therect.setsize(newwidth,newlength);
dodrawrect(therect);
break;
default:
cout << "error int choice\n";
fquit = true;
break;
}
}
return 0;
}
int domenu()
{
int choice;
cout << "\n\n ***menu***\n";
cout << "(1) rectangle\n";
cout << "(2)area\n";
cout << "(3)perimeter\n";
cout << "(4)resize\n";
cout << "(5)quit\n";
cin >>choice;
return choice;
}
void dodrawrect(rectangle therect)
{
int height = therect.getheight();
int width = therect.getwidth();
for(int i = 0;i < height;i++)
{
for(int j = 0;j < width;j++)
cout << "*" << endl;
}
}
void dogetarea(rectangle therect)
{
cout << "area:" << therect.getarea() << endl;
}
void dogetperim(rectangle therect)
{
cout << "perimeter" << therect.getperim() << endl;
}
[解决办法]
setsize 在类中木有声明
[解决办法]
把这个搞懂 还算不上入门
还得继续学习的
半年估计可能才入门(前提是普通人)
至于类的成员问题 你要是觉得抽象 可以和现实世界联系起来
思考
[解决办法]
#include<iostream>using namespace std;enum choice {drawrect = 1,getarea,getperim,changedimensions,quit};class rectangle{public:rectangle(int width,int height);~rectangle();void setsize(int,int ); //缺少了方法声明 int getheight() const {return itsheight;}int getwidth() const {return itswidth;}int getperim() const {return 2*itsheight+2*itswidth;}int getarea() const {return itsheight*itswidth;}private:int itswidth;int itsheight;};void rectangle::setsize(int newwidth,int newheight) //注释方向错了 \\为什么这样定义会报错呢{itswidth = newwidth;itsheight = newheight;}rectangle::rectangle(int width,int height){itswidth = width;itsheight = height;}rectangle::~rectangle() {}int domenu();void dodrawrect(rectangle);void dogetarea(rectangle);void dogetperim(rectangle);int main(){rectangle therect(30,5);int choice = drawrect;int fquit = false;while (!fquit){choice = domenu();if(choice < drawrect || choice > quit){cout << "\n我也不知道这句什么意思\n\n";continue;}switch (choice){case drawrect:dodrawrect(therect);break;case getarea:dogetarea(therect);break;case getperim:dogetperim(therect); // 这里少打了一个dobreak;case changedimensions:int newlength,newwidth;cout << "\nnew width";cin >> newwidth;cout << "newheight";cin >> newlength;therect.setsize(newwidth,newlength);dodrawrect(therect);break;default:cout << "error int choice\n";fquit = true;break;}}return 0;}int domenu(){int choice;cout << "\n\n ***menu***\n";cout << "(1) rectangle\n";cout << "(2)area\n";cout << "(3)perimeter\n";cout << "(4)resize\n";cout << "(5)quit\n";cin >>choice;return choice;}void dodrawrect(rectangle therect){int height = therect.getheight();int width = therect.getwidth();for(int i = 0;i < height;i++){for(int j = 0;j < width;j++)cout << "*" << endl;}}void dogetarea(rectangle therect){cout << "area:" << therect.getarea() << endl;}void dogetperim(rectangle therect){cout << "perimeter" << therect.getperim() << endl;}