#include<cmath>
const double PI=3.1416;
using namespace std;
class shape
{
public:
shape(double w=0) {
r=w;
}
virtual double Area()=0;
protected:
double r;
};
class circle:public shape
{
circle(double g=0):shape(g)
{
}
double Area()
{
double s=PI*r*r;
return s;
}
};
class square:public shape
{
square(double a=0,double b=0):shape(a),l(b) {}
double Area()
{
double s=l*r;
}
protected:
double l;
};
class triangle:public shape
{
triangle(double c=0,double e=0,double d=0):shape(c)
{
r1=e;
angle=PI*d/180.0;
}
double Area()
{
double s=0.5*r*r1*sin(angle);
return s;
}
protected:
double r1;
double angle;
};
double fun(shape *p)
{
return p->Area();
}
int main()
{
square sq(1,1);
triangle tr(1,1,45);
circle ci(1);
shape *p;
double s=0;
p=&sq;
cout<<"正方形面积是:"<<fun(p)<<endl;
s+=fun(p);
p=&tr;
cout<<"三角形面积是:"<<fun(p)<<endl;
s+=fun(p);
p=&ci;
cout<<"圆形的面积是:"<<fun(p)<<endl;
s+=fun(p);
cout<<"三图形总面积是:"<<s<<endl;
return 0;
}
形状.cpp
C:\Documents and Settings\Administrator\桌面\形状.cpp(61) : error C2248: 'square::square' : cannot access private member declared in class 'square'
C:\Documents and Settings\Administrator\桌面\形状.cpp(29) : see declaration of 'square::square'
C:\Documents and Settings\Administrator\桌面\形状.cpp(62) : error C2248: 'triangle::triangle' : cannot access private member declared in class 'triangle'
C:\Documents and Settings\Administrator\桌面\形状.cpp(40) : see declaration of 'triangle::triangle'
C:\Documents and Settings\Administrator\桌面\形状.cpp(63) : error C2248: 'circle::circle' : cannot access private member declared in class 'circle'
C:\Documents and Settings\Administrator\桌面\形状.cpp(17) : see declaration of 'circle::circle'
执行 cl.exe 时出错.
各位高手帮帮忙
8 个解决方案
#1
你的square类,triangle类和circle类中的构造函数默认都是private访问权限,修改为public即可,我想你是忘记加public标记了,不写的话,默认是private访问的,这个和struct结构体不同,struct中如果不写,默认的是public访问的。
还有你的square类的double Area()函数需要返回一个double类型的值。
代码帮你修改完成:
还有你的square类的double Area()函数需要返回一个double类型的值。
代码帮你修改完成:
#include<iostream>
#include<cmath>
const double PI=3.1416;
using namespace std;
class shape
{
public:
shape(double w=0) {
r=w;
}
virtual double Area()=0;
protected:
double r;
};
class circle:public shape
{
public:
circle(double g=0):shape(g)
{
}
double Area()
{
double s=PI*r*r;
return s;
}
};
class square:public shape
{
public:
square(double a=0,double b=0):shape(a),l(b) {}
double Area()
{
double s=l*r;
return s;
}
protected:
double l;
};
class triangle:public shape
{
public:
triangle(double c=0,double e=0,double d=0):shape(c)
{
r1=e;
angle=PI*d/180.0;
}
double Area()
{
double s=0.5*r*r1*sin(angle);
return s;
}
protected:
double r1;
double angle;
};
double fun(shape *p)
{
return p->Area();
}
int main()
{
square sq(1,1);
triangle tr(1,1,45);
circle ci(1);
shape *p;
double s=0;
p=&sq;
cout<<"Õý·½ÐÎÃæ»ýÊÇ£º"<<fun(p)<<endl;
s+=fun(p);
p=&tr;
cout<<"Èý½ÇÐÎÃæ»ýÊÇ£º"<<fun(p)<<endl;
s+=fun(p);
p=&ci;
cout<<"Ô²ÐεÄÃæ»ýÊÇ£º"<<fun(p)<<endl;
s+=fun(p);
cout<<"ÈýͼÐÎ×ÜÃæ»ýÊÇ£º"<<s<<endl;
return 0;
}
#2
cout<<"Õý·½ÐÎÃæ»ýÊÇ£º"<<fun(p)<<endl;
s+=fun(p);
后面 cout<<"Èý½ÇÐÎÃæ»ýÊÇ£º"<<fun(p)<<endl;
s+=fun(p);
的s再相加就成了正方形和三角形面积的和,应该在s+=之前加上s=0;
后面的同样????
s+=fun(p);
后面 cout<<"Èý½ÇÐÎÃæ»ýÊÇ£º"<<fun(p)<<endl;
s+=fun(p);
的s再相加就成了正方形和三角形面积的和,应该在s+=之前加上s=0;
后面的同样????
#3
你可以双击报错的语句 VS就会找到那句话 你就可以看看到底是什么样的问题 要学会分析和解决问题
#4
1楼正解
由错误提示:
error C2248: 'square::square' : cannot access private member declared in class 'square'
就可以看出你定义成private成员了,而private成员是无法在类外面使用的
由错误提示:
error C2248: 'square::square' : cannot access private member declared in class 'square'
就可以看出你定义成private成员了,而private成员是无法在类外面使用的
#5
vc类中默认为私有...
#6
private成员是无法在类外面使用的,即使类的对象也不可以
#7
#8
帮顶一个
#1
你的square类,triangle类和circle类中的构造函数默认都是private访问权限,修改为public即可,我想你是忘记加public标记了,不写的话,默认是private访问的,这个和struct结构体不同,struct中如果不写,默认的是public访问的。
还有你的square类的double Area()函数需要返回一个double类型的值。
代码帮你修改完成:
还有你的square类的double Area()函数需要返回一个double类型的值。
代码帮你修改完成:
#include<iostream>
#include<cmath>
const double PI=3.1416;
using namespace std;
class shape
{
public:
shape(double w=0) {
r=w;
}
virtual double Area()=0;
protected:
double r;
};
class circle:public shape
{
public:
circle(double g=0):shape(g)
{
}
double Area()
{
double s=PI*r*r;
return s;
}
};
class square:public shape
{
public:
square(double a=0,double b=0):shape(a),l(b) {}
double Area()
{
double s=l*r;
return s;
}
protected:
double l;
};
class triangle:public shape
{
public:
triangle(double c=0,double e=0,double d=0):shape(c)
{
r1=e;
angle=PI*d/180.0;
}
double Area()
{
double s=0.5*r*r1*sin(angle);
return s;
}
protected:
double r1;
double angle;
};
double fun(shape *p)
{
return p->Area();
}
int main()
{
square sq(1,1);
triangle tr(1,1,45);
circle ci(1);
shape *p;
double s=0;
p=&sq;
cout<<"Õý·½ÐÎÃæ»ýÊÇ£º"<<fun(p)<<endl;
s+=fun(p);
p=&tr;
cout<<"Èý½ÇÐÎÃæ»ýÊÇ£º"<<fun(p)<<endl;
s+=fun(p);
p=&ci;
cout<<"Ô²ÐεÄÃæ»ýÊÇ£º"<<fun(p)<<endl;
s+=fun(p);
cout<<"ÈýͼÐÎ×ÜÃæ»ýÊÇ£º"<<s<<endl;
return 0;
}
#2
cout<<"Õý·½ÐÎÃæ»ýÊÇ£º"<<fun(p)<<endl;
s+=fun(p);
后面 cout<<"Èý½ÇÐÎÃæ»ýÊÇ£º"<<fun(p)<<endl;
s+=fun(p);
的s再相加就成了正方形和三角形面积的和,应该在s+=之前加上s=0;
后面的同样????
s+=fun(p);
后面 cout<<"Èý½ÇÐÎÃæ»ýÊÇ£º"<<fun(p)<<endl;
s+=fun(p);
的s再相加就成了正方形和三角形面积的和,应该在s+=之前加上s=0;
后面的同样????
#3
你可以双击报错的语句 VS就会找到那句话 你就可以看看到底是什么样的问题 要学会分析和解决问题
#4
1楼正解
由错误提示:
error C2248: 'square::square' : cannot access private member declared in class 'square'
就可以看出你定义成private成员了,而private成员是无法在类外面使用的
由错误提示:
error C2248: 'square::square' : cannot access private member declared in class 'square'
就可以看出你定义成private成员了,而private成员是无法在类外面使用的
#5
vc类中默认为私有...
#6
private成员是无法在类外面使用的,即使类的对象也不可以
#7
#8
帮顶一个