基于 c++ 中 strcmp 的用法错误分析
#include<iostream>原始代码(出错代码)为:
#include<string>
using namespace std;
class Point
{
protected:
int x,y;
public:
void draw(int a,int b)
{
x=a;
y=b;
}
void show()
{
cout<<"This point is the abscissa for "<<x<<", y coordinate for"<<y<<endl;
}
};
class colour_Point : public Point
{
private:
char colour;
public:
virtual void draw(int m,int n,char &q)
{
x=m;
y=n;
colour=q;
}
virtual void show()
{
cout<<"This point is the abscissa for "<<x<<", y coordinate for"<<y<<endl;
cout<<"And the Point's colour is "<<colour<<endl;
}
};
int main()
{
Point point;
point.draw(3,4);
point.show();
colour_Point colour_point;
colour_point.draw(4,5,"紫色");
colour_point.show();
system("pause");
return 0;
}
//以上代码编译出错,出错原因:字符串字长限制。
修改方案:将地址引用,换成指针传递变量。
修改成功的代码为:
#include<iostream>
#include<string>
using namespace std;
class Point
{
protected:
int x,y;
public:
void draw(int a,int b)
{
x=a;
y=b;
}
void show()
{
cout<<"This point is the abscissa for "<<x<<", y coordinate for"<<y<<endl;
}
};
class colour_Point : public Point
{
private:
char *colour;
public:
virtual void draw(int m,int n,char *q)
{
x=m;
y=n;
colour=q;
}
virtual void show()
{
cout<<"This point is the abscissa for "<<x<<", y coordinate for"<<y<<endl;
cout<<"And the Point's colour is "<<colour<<endl;
}
};
int main()
{
Point point;
point.draw(3,4);
point.show();
colour_Point colour_point;
colour_point.draw(4,5,"紫色");
colour_point.show();
system("pause");
return 0;
}
编译结果为: This point is the abscissa for 3 , y coordinate for 4
This point is the abscissa for 4 ,y coordinate for 5,and the Point's colour is 紫色