在 C++ 中访问抽象类成员中的类
Accessing a Class in an Abstract Class' member in C++
>我有一个类点,如下所示:
class Point {
private:
int x,y;
public:
Point(int x, int y){
setX(x);
setY(y);
}
Point(Point ©){
x = copy.getX();
y = copy.getY();
}
int getX(){
return x;
}
int getY(){
return y;
}
void setX(int setx){
x = setx;
}
void setY(int sety){
y = sety;
}
void print(){
cout << "(" << getX() << "," << getY() << ")" << endl;
}
};
我正在尝试创建一个名为GeometricShape的抽象类,该类能够利用其构造函数中的一个点,然后在其成员中调用该点。
我尝试声明一个点,然后利用复制构造函数来构造几何形状。但是,如果没有"调用'Point::P oint(('几何形状(点坐标(的匹配函数",我似乎无法让它工作。
我还尝试在构造函数中声明该点:
Point ShapePoint = coord;
但是我无法访问打印成员中的形状点。
这是我目前所处的位置:
class GeometricShape{
Point ShapePoint;
public:
GeometricShape(Point coord){
ShapePoint = Point(coord);
}
virtual float getArea(){
return 0;
}
virtual float getPerimeter(){
return 0;
}
virtual void print(){
ShapePoint.print();
}
};
使用成员初始值设定项
GeometricShape(Point coord)
: ShapePoint(coord)
{
}
我还建议将这一点作为常量参考const Point& coord
以避免内存中不必要的副本。
相关文章:
- 这是关于成员访问规则的正确摘要吗
- 为什么我在空指针错误(链表)中获取成员访问权限
- 成员访问是否在空指针上定义C++?
- C++ IDE 不会推断/自动完成对模板类中的 std::array 下标表达式的成员访问
- 为什么类成员数据必须是静态的才能被模板化类的模板化结构成员访问
- 为什么c++允许成员函数定义中实例的私有成员访问
- C/C++ 包含点的宏参数(成员访问运算符)
- 访问说明符(私有/公共/受保护)如何在内部工作(限制成员访问)?
- 如何将超类的受保护成员访问到其派生类. 如果已在派生类中声明了具有相同名称的函数?
- 内部类私有成员访问和封闭的友好性
- 通过 C++ 中的另一个结构成员访问结构
- 具体化 PRVALUES 成员访问的 decltype 行为不正确
- 常量表达式中的静态成员访问
- XVALUE来自类成员访问表达式
- 未经授权的私有类成员访问会产生编译时错误而不是运行时错误?
- 在 c++ 中,为什么 -> 被称为二进制中缀指针成员访问运算符?
- 如何访问模板参数的成员?“成员访问不完整的类型”
- 不明确的可变参数类成员访问
- C 受保护的成员访问
- 将typeID转换为静态成员访问(C )的命名空间