【发布时间】:2018-05-17 07:38:33
【问题描述】:
在我们的一项 C++ 编程任务(继承)中,我必须设计一个抽象类 Shape,它具有一些属性,如颜色、旋转度等,它们是形状之间常见。但是,在实现像 Circle、Rectangle 和......这样的 Base 类期间,我必须添加一些属性,如圆的中心(它自己需要自己的 setter 和 getter,因为它是私有的!)或矩形的 4 个角(它的 setter 和 getter )在 out Base 类中没有作为函数(无论是否虚拟)被提及。
最初我想使用基类的指针访问派生类的每个方法。在我的 main 中,我使用了 基类的指针 em>,Shape * 具有 Dynamic 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;
};
分别是我的 Circle 和 Rectangle 类
圈头:
#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
圈出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 ;
}
最初我想使用基类的指针访问派生类的每个方法。
【问题讨论】:
-
如果您有
Shape*并想将其用作Circle*,请使用dynamic_cast<> -
您的实现似乎有缺陷。为什么
Shape函数是纯虚函数,然后在所有子类中重新实现它们?这是很多代码重复。 -
至于你的问题,你应该研究一下downcasting。
-
在这个作业中,我们应该在 Shape 中有纯虚函数,这就是为什么我必须在每个子类中分别实现它们
-
你能说明一下我的主要(通过动态演员纠正)应该是怎样的吗?
标签: c++ class inheritance derived-class dynamic-cast