C++类内的构造函数无法访问私有成员,怎么回事??
#include<iostream>#include<string>#include<cmath>usingnamespacestd;classShape{public:...
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Shape
{
public:virtual float area() const=0;
virtual void shapename() const=0;
};
class Circle:public Shape
{
public:
Circle()
{
radius=0;
}
Circle(float r):radius(r){};
virtual void shapename() const
{
cout<<"Circle"<<endl;
}
virtual float area() const;
private:
float radius;
};
float Circle::area() const
{
return 3.14159*radius*radius;
};
class Rectangle:public Shape
{
public:
Rectangle()
{
width=0;
longth=0;
}
Rectangle(float a,float b)
{
width=a;
longth=b;
}
virtual void shapename() const
{
cout<<"Rectangle"<<endl;
}
virtual float area() const;
private:
float width;
float longth;
};
float Rectangle::area() const
{
return width*longth;
}
class Triangle:public Shape
{
Triangle()
{
s1=0;
s2=0;
s3=0;
};
Triangle(float a,float b,float c)
{
s1=a;
s2=b;
s3=c;
};
virtual void shapename() const
{
cout<<"Triangle"<<endl;
}
virtual float area() const;
private:
float s1;
float s2;
float s3;
};
float Triangle::area() const
{
return sqrt(((s1+s2+s3)/2)*((s1+s2+s3)/2-s1)*((s1+s2+s3)/2-s2)*((s1+s2+s3)/2-s3));
}
int main()
{
Circle c1(3.6);
Rectangle r1(1.5,9.8);
Triangle t1(2.5,3.5,4.1);
Shape *p;
p=&c1;
p->shapename();
cout<<p->area()<<endl;
p=&r1;
p->shapename();
cout<<p->area()<<endl;
p=&t1;
p->shapename();
cout<<p->area()<<endl;
cin.get();
return 0;
};
错误:Triangle t1(2.5,3.5,4.1);
编译环境 vs2008
1>error C2248: “Triangle::Triangle”: 无法访问 private 成员(在“Triangle”类中声明) 展开
#include <string>
#include <cmath>
using namespace std;
class Shape
{
public:virtual float area() const=0;
virtual void shapename() const=0;
};
class Circle:public Shape
{
public:
Circle()
{
radius=0;
}
Circle(float r):radius(r){};
virtual void shapename() const
{
cout<<"Circle"<<endl;
}
virtual float area() const;
private:
float radius;
};
float Circle::area() const
{
return 3.14159*radius*radius;
};
class Rectangle:public Shape
{
public:
Rectangle()
{
width=0;
longth=0;
}
Rectangle(float a,float b)
{
width=a;
longth=b;
}
virtual void shapename() const
{
cout<<"Rectangle"<<endl;
}
virtual float area() const;
private:
float width;
float longth;
};
float Rectangle::area() const
{
return width*longth;
}
class Triangle:public Shape
{
Triangle()
{
s1=0;
s2=0;
s3=0;
};
Triangle(float a,float b,float c)
{
s1=a;
s2=b;
s3=c;
};
virtual void shapename() const
{
cout<<"Triangle"<<endl;
}
virtual float area() const;
private:
float s1;
float s2;
float s3;
};
float Triangle::area() const
{
return sqrt(((s1+s2+s3)/2)*((s1+s2+s3)/2-s1)*((s1+s2+s3)/2-s2)*((s1+s2+s3)/2-s3));
}
int main()
{
Circle c1(3.6);
Rectangle r1(1.5,9.8);
Triangle t1(2.5,3.5,4.1);
Shape *p;
p=&c1;
p->shapename();
cout<<p->area()<<endl;
p=&r1;
p->shapename();
cout<<p->area()<<endl;
p=&t1;
p->shapename();
cout<<p->area()<<endl;
cin.get();
return 0;
};
错误:Triangle t1(2.5,3.5,4.1);
编译环境 vs2008
1>error C2248: “Triangle::Triangle”: 无法访问 private 成员(在“Triangle”类中声明) 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询