通过"a pointer of the base class"访问未在基类中声明的子类的方法或属性(动态)
Access by "a pointer of the base class" to a method or attribute of the child class which was not declared in the base class(dynamically)
在我们在C 编程中的一项作业中( sashitance ),我必须设计一个带有某些属性的抽象类形状,例如颜色,旋转程度等,形状之间是常见的。但是,在实施基本类中,例如Circle,Rectangle和... ...我不得不添加一些属性,例如Circle的中心(它会自我要求其自己的设定器和Getter,因为它是私有的!)或矩形的4个角(与其固定器和getter)在外类中未提及的函数(无论是否虚拟)。
最初,我想使用基类的指针。在我的主机上,我使用了base类的指针,shape *具有 dynamic bind bind 与公共方法和属性,但是在设置单独的(派生和碱基之间的罕见)时属性,它无法通过基类的指针访问。我试图将它们声明为我的基类中的虚拟功能,但是,它行不通,也是不合逻辑的,因为用户可能具有多种形状的特征!有人知道如何解决这个问题吗?我如何使用Shape*访问仅在派生类中声明的那些仅声明的方法和属性?tnx
这是我的形状的基类。
class Shape
{
public:
virtual void set_color(int color)=0;
virtual void set_border_color(int border_color)=0;
virtual void set_degree(float border_width)=0;
virtual void set_border_width(double rotate_degree)=0;
virtual void set_opacity(double opacity)=0;
protected:
int color;
int border_color;
float border_width;
double rotate_degree;
double opacity;
};
分别是我的圆圈和矩形类
圆头标头:
#ifndef CIRCLE_H
#define CIRCLE_H
#include "shape.h"
class Circle :public Shape
{
public:
void set_color(int _color);
void set_border_color(int _border_color);
void set_degree(float rotate_degree);
void set_border_width(double border_width);
void set_opacity(double _opacity);
virtual void set_x_center(int _x_center);
virtual void set_y_center(int _y_center);
virtual void set_radius(int _radius);
virtual void set_name(std ::string _name);
int get_color();
int get_x_center();
int get_y_center();
std::string get_name();
private:
int x_center;
int y_center;
int radius;
std ::string name;
};
#endif // CIRCLE_H
Circle CPP:
#include <sstream>
#include <iostream>
#include <algorithm>
#include <string>
#include "circle.h"
#include "shape.h"
void Circle::set_color(int _color){ color=_color;}
void Circle::set_border_color(int _border_color){border_color=_border_color;}
void Circle::set_degree(float _rotate_degree){rotate_degree=_rotate_degree;}
void Circle::set_border_width(double _border_width){border_width=_border_width; }
void Circle::set_opacity(double _opacity){opacity=_opacity;}
int Circle::get_color(){return color;}
void Circle::set_x_center(int _x_center){ x_center=_x_center;}
void Circle::set_y_center(int _y_center){ y_center=_y_center;}
void Circle::set_radius(int _radius){ radius=_radius;}
void Circle::set_name(std ::string _name){ name=_name;}
int Circle::get_x_center(){return x_center;}
int Circle::get_y_center(){return y_center;}
std::string Circle::get_name(){return name;}
矩形标题:
#ifndef RECT_H
#define RECT_H
#include <sstream>
#include <iostream>
#include <algorithm>
#include <string>
#include "rect.h"
#include "shape.h"
class Rect : public Shape
{
public:
void set_color(int _color);
void set_border_color(int _border_color);
void set_degree(float _border_width);
void set_border_width(double _rotate_degree);
void set_opacity(double _opacity);
void set_first_point(int _first_x,int _first_y);
void set_second_point(int _second_x,int _second_y);
void set_name(std ::string _name);
private:
int first_point [2];
int second_point [2];
std ::string name;
};
#endif // RECT_H
矩形CPP:
#include "rect.h"
#include "shape.h"
void Rect::set_color(int _color){ color=_color;}
void Rect::set_border_color(int _border_color){border_color=_border_color;}
void Rect::set_degree(float _border_width){border_width=_border_width;}
void Rect::set_border_width(double _rotate_degree){rotate_degree=_rotate_degree;}
void Rect::set_opacity(double _opacity){opacity=_opacity;}
void Rect::set_first_point(int _first_x,int _first_y){first_point[0]=_first_x;first_point[1]=_first_y;}
void Rect::set_second_point(int _second_x,int _second_y){second_point[0]=_second_x;second_point[1]=_second_x;}
void Rect::set_name(std ::string _name){name=_name;}
这是我的主要
#include <cstdlib>
#include <vector>
#include <cmath>
#include <string>
#include <vector>
#include <cmath>
#include <sstream>
#include <iostream>
#include <algorithm>
#include "shape.h"
#include "circle.h"
using namespace std;
int main()
{
Circle a;
Shape* b;
b=&a;
b->set_color(12);
b->set_x_center(30);
cout<< b->get_x_center();
return 0 ;
}
最初,我想使用基类的指针访问每种派生类方法。
您可以通过动态铸造回Circle *
:
b->set_color(12);
dynamic_cast<Circle *>(b)->set_x_center(30);
std::cout << dynamic_cast<Circle *>(b)->get_x_center();
但是,当这样的动态铸造确保结果不是nullptr
b->set_color(12);
Circle *c = dynamic_cast<Circle *>(b);
if (c != nullptr)
{
c->set_x_center(30);
std::cout << c->get_x_center();
}
相关文章:
- 将子类方法声明为基类的友元
- 函数从唯一代码调用正确的子类方法
- 将"this"从父类方法转换为子类方法是一种好的做法吗?
- 从C++中的父类实例访问子类方法
- 基类中的虚拟方法和子类中的非 Virtula 方法.仍然如何调用子类方法,即使它是非虚拟的
- 按名称调用父级中的子类方法
- C++ - 如何使用基指针重写子类方法
- 如何在子类方法中更改参数
- 如何在尝试为某些非类型值实例化模板子类方法时产生编译器错误
- 访问指针数组中的子类方法
- 调用子类方法会导致分段错误
- 指向方法父参数c++的子类方法指针
- 函数调用父类方法而不是子类方法
- C++ 无法使用指向类的指针调用子类方法e
- 从SuperClass C调用子类方法
- 无法从 QML 访问C++ QObject 子类方法
- 使用继承接口的类的子类方法
- 如何迭代定义为超类的向量,但调用子类方法
- 在c++中从超类数组中访问子类方法
- 超类调用子类方法的STL迭代器