关于抽象类的问题 [英] question about abstract class

查看:60
本文介绍了关于抽象类的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)什么是抽象类?
2)当我们将抽象类与示例一起使用时?

1)what is abstract class?
2)when we use abstract class with example?

推荐答案

http://msdn.microsoft.com/en-us/library/c8whxhf1.aspx[^]

Google for more.


抽象类很抽象.

[
An abstract class is, well, abstract.

This[^] is what I did when I was learning.

Why use it ? Because it acts like an interface. It defines what a child class needs to do, but perhaps has no value on it''s own. For example, a fruit class might be useless, because you always want a TYPE of fruit, but the class defines what a fruit needs to do ( for example, attend the mardi gras )


以上所有解决方案都是正确的,我想举一个真实的例子.考虑一个绘图程序,该程序可让您使用各种形状绘制线条图形,例如线条,正方形或圆形.您可能已经知道,每当窗口大小改变时,或者当窗口的隐藏部分暴露时,您都必须重新绘制包含这些形状的图像.因此,您开发了一个名为场景"的类来管理绘图和重绘:
While all the above solutions are correct, I''d like to add a real world example. Consider a drawing program that lets you draw line graphics using various shapes, such as lines, squares, or circles. As you might be aware of, everytime a window size changes, or when hidden parts of the window get exposed, you have to redraw your image containing these shapes. So you develop a class called ''Scene'' that manages the drawing and redrawing:
class Scene {
private:
   std::list<class Shape*> shapes_;
public:
   void redraw();
};


此类包含必须重绘的形状列表.但是,您只是在开始程序,还不知道要支持哪种形状,已经想到的几种形状是以不同的方式存储和绘制的.因此,您将"Shape"作为基类,并从该基类派生"Line","Square"和"Circle".绘制这些形状的代码也应分别在每个形状中实现,因此您向Shape添加了一个虚拟函数"draw",该函数被具体的派生形状类所覆盖:


This class contains a list of shapes that it has to redraw. However, you are just starting your program, and you don''t know yet what kind of shapes you''d want to support, and the few you already thought of are stored and drawn in different ways. So you make ''Shape'' a base class, and derive ''Line'', ''Square'' and ''Circle'' from that base. The code to draw these shapes should also be implemented in each shape seperately, so you add a virtual function ''draw'' to Shape, that gets overridden by the concrete, derived shape classes:

class Shape {
public:
   virtual ~Shape(); // don''t forget virtual destructor on base classes
   virtual void draw() = 0;
};

class Line : public Shape {
private:
   Point p1_;
   Point p2_;
public:
   virtual ~Line();
   void draw();
};

class Square : public Shape {
private:
   Point position_;
   double width_;
public:
   virtual ~Square();
   void draw();
};

class Circle : public Shape {
private:
   Point position_;
   double radius_;
public:
   virtual ~Circle();
   void draw();
};


在Shape Shape中实现draw()几乎没有意义,因为Shape本身没有定义任何可以绘制的元素.因此,我声明了draw()一个虚函数.根据定义,这会将Shape转换为抽象类.结果,您将无法创建"Shape"类型的实例.

Shape类型的对象根本不存在,它们仅以派生类LineCircleSquare的形式存在.这些类被视为 concrete ,因为实际上存在这些类型的实例.形状OTOH被认为是抽象,因为不存在这种特定类型的对象,并且该类仅用于描述特定范围的对象类型的概念或共有属性.


It makes little sense to implement draw() in the class Shape, since Shape itself does not define any elements that can be drawn. Therefore I declared draw() a pure virtual function. This, by definition, turns Shape into an abstract class. As a result, you will not be able to create an instance of type ''Shape''.

Objects of type Shape simply don''t exist, they exist only in the form of derived classes, Line, Circle, or Square. These classes are considered concrete, since instances of these types actually exist. Shape OTOH is considered abstract, because no objects of this specific type exists, and the class is only used to describe a concept or common property of a certain range of object types.


这篇关于关于抽象类的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆