pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

具體類和抽象類有什么區別?

What is the difference between a concrete class and an abstract class?(具體類和抽象類有什么區別?)
本文介紹了具體類和抽象類有什么區別?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在學習 C++,但我對抽象類和具體類感到困惑.一些真實世界的例子將不勝感激.

I am learning C++, but I am confused about abstract class and concrete class. Some real world examples would be appreciated.

推薦答案

抽象類是聲明了一個或多個方法但未定義的類,這意味著編譯器知道這些方法是類的一部分,但不為該方法執行什么代碼.這些被稱為抽象方法.這是一個抽象類的例子.

An abstract class is a class for which one or more methods are declared but not defined, meaning that the compiler knows these methods are part of the class, but not what code to execute for that method. These are called abstract methods. Here is an example of an abstract class.

class shape {
public:
  virtual void draw() = 0;
};

這聲明了一個抽象類,該類指定該類的任何后代都應該實現 draw 方法,如果該類是具體的.你不能實例化這個類,因為它是抽象的,畢竟,如果你調用成員繪制,編譯器不知道要執行什么代碼.所以你不能做以下事情:

This declares an abstract class which specifies that any descendants of the class should implement the draw method if the class is to be concrete. You cannot instantiate this class because it is abstract, after all, the compiler wouldn't know what code to execute if you called member draw. So you can not do the following:

shape my_shape();
my_shape.draw();

為了能夠實際使用 draw 方法,您需要從這個抽象類派生類,這些類確實實現了 draw 方法,使類變得具體:

To be able to actually use the draw method you would need to derive classes from this abstract class, which do implement the draw method, making the classes concrete:

class circle : public shape {
public:
  circle(int x, int y, int radius) {
    /* set up the circle */
  }
  virtual draw() {
    /* do stuff to draw the circle */
  }
};

class rectangle : public shape {
public:
  rectangle(int min_x, int min_y, int max_x, int max_y) {
    /* set up rectangle */
  }
  virtual draw() {
    /* do stuff to draw the rectangle */
  }
};

現在您可以實例化具體對象圓和矩形并使用它們的繪制方法:

Now you can instantiate the concrete objects circle and rectangle and use their draw methods:

circle my_circle(40, 30, 10);
rectangle my_rectangle(20, 10, 50, 15);
my_circle.draw();
my_rectangle.draw();

當然,問題是,您為什么要這樣做?你不能同樣定義圓和矩形類并取消整個形狀類嗎?你可以,但那樣你就無法利用他們的遺產:

Now of course the question is, why would you want to do this? Couldn't you just as well have defined the circle and rectangle classes and have done away with the whole shape class? You could, but then you wouldn't be able to take advantage of their inheritance:

std::vector<shape*> my_scene;
my_scene.push_back(new circle(40, 30, 10));
my_scene.push_back(new rectangle(20, 10, 50, 15));
std::for_each(my_scene.begin(), my_scene.end(), std::mem_fun_ref(&shape::draw)

此代碼可讓您將所有形狀收集到一個容器中.如果場景中有很多形狀和許多不同的形狀,這會讓??事情變得容易得多.例如,我們現在可以一次性繪制所有形狀,而這樣做的代碼甚至不需要知道我們擁有的不同類型的形狀.

This code let's you collect all your shapes into one container. This makes it a lot easier if you have a lot of shapes and many different shapes in your scene. For example we can now draw all the shapes in one go, and the code that does so doesn't even need to know about the different types of shapes we have.

現在終于要知道為什么shape的draw函數是抽象的,而不僅僅是一個空函數,即為什么不直接定義:

Now finally we need to know why the draw function of shape is abstract, and not just an empty function, i.e. why didn't we just define:

class shape {
public:
  virtual void draw() {
    /* do nothing */
  }
};

這樣做的原因是我們并不真正想要形狀類型的對象,無論如何它們都不是真實的東西,它們是抽象的.所以為 draw 方法定義一個實現是沒有任何意義的,即使是一個空的.使形狀類抽象可以防止我們錯誤地實例化形狀類,或錯誤地調用基類的空繪制函數而不是派生類的繪制函數.實際上,我們為任何想表現得像形狀的類定義了一個接口,我們說任何這樣的類都應該有一個 draw 方法,看起來就像我們已經指定的那樣.

The reason for this is that we don't really want objects of type shape, they wouldn't be real things anyway, they would be abstract. So it doesn't make any sense to define an implementation for the draw method, even an empty one. Making the shape class abstract prevents us from mistakenly instantiating the shape class, or mistakenly calling the empty draw function of the base class instead of the draw function of the derived classes. In effect we define an interface for any class that would like to behave like a shape, we say that any such class should have a draw method that looks like we have specified it should.

回答你最后一個問題,沒有任何普通派生類"之類的東西,每個類要么是抽象的,要么是具體的.具有任何抽象方法的類是抽象的,任何不具有抽象方法的類都是具體的.這只是區分這兩種類的一種方式.基類可以是抽象的或具體的,派生類可以是抽象的或具體的:

To answer you last question, there isn't any such thing as a 'normal derived class' every class is either abstract or concrete. A class that has any abstract methods is abstract, any class that doesn't is concrete. It's just a way to differentiate the two types of classes. A base class can be either abstract or concrete and a derived class can be either abstract or concrete:

class abstract_base {
public:
  virtual void abstract_method1() = 0;
  virtual void abstract_method2() = 0;
};

class concrete_base {
public:
  void concrete_method1() {
    /* do something */
  }
};

class abstract_derived1 : public abstract_base {
public:
  virtual void abstract_method3() = 0;
};

class abstract_derived2 : public concrete_base {
public:
  virtual void abstract_method3() = 0;
};

class abstract_derived3 : public abstract_base {
public:
  virtual abstract_method1() {
    /* do something */
  }
  /* note that we do not provide an implementation for
     abstract_method2 so the class is still abstract */
};

class concrete_derived1 : public concrete_base {
public:
  void concrete_method2() {
    /* do something */
  }
};

class concrete_derived2 : public abstract_base {
public:
  virtual void abstract_method1() {
    /* do something */
  }
  virtual void abstract_method2() {
    /* do something */
  }
  /* This class is now concrete because no abstract methods remain */
};

這篇關于具體類和抽象類有什么區別?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 123悬赏网_发布悬赏任务_广告任务平台 | 光伏家 - 太阳能光伏发电_分布式光伏发电_太阳能光伏网 | pbt头梳丝_牙刷丝_尼龙毛刷丝_PP塑料纤维合成毛丝定制厂_广州明旺 | 插针变压器-家用电器变压器-工业空调变压器-CD型电抗器-余姚市中驰电器有限公司 | 吹田功率计-长创耐压测试仪-深圳市新朗普电子科技有限公司 | 层流手术室净化装修-检验科ICU改造施工-华锐净化工程-特殊科室建设厂家 | 中高频感应加热设备|高频淬火设备|超音频感应加热电源|不锈钢管光亮退火机|真空管烤消设备 - 郑州蓝硕工业炉设备有限公司 | 上海办公室装修,写字楼装修—启鸣装饰设计工程有限公司 | 西门子伺服控制器维修-伺服驱动放大器-828D数控机床维修-上海涌迪 | SF6环境监测系统-接地环流在线监测装置-瑟恩实业 | 电动不锈钢套筒阀-球面偏置气动钟阀-三通换向阀止回阀-永嘉鸿宇阀门有限公司 | 权威废金属|废塑料|废纸|废铜|废钢价格|再生资源回收行情报价中心-中废网 | 金联宇电缆|广东金联宇电缆厂家_广东金联宇电缆实业有限公司 | 高温高压釜(氢化反应釜)百科| 精密五金加工厂-CNC数控车床加工_冲压件|蜗杆|螺杆加工「新锦泰」 | 超声波流量计_流量标准装置生产厂家 _河南盛天精密测控 | 清水-铝合金-建筑模板厂家-木模板价格-铝模板生产「五棵松」品牌 | 扬子叉车厂家_升降平台_电动搬运车|堆高车-扬子仓储叉车官网 | 电动葫芦|防爆钢丝绳电动葫芦|手拉葫芦-保定大力起重葫芦有限公司 | 流水线电子称-钰恒-上下限报警电子秤-上海宿衡实业有限公司 | 酸度计_PH计_特斯拉计-西安云仪 纯水电导率测定仪-万用气体检测仪-低钠测定仪-米沃奇科技(北京)有限公司www.milwaukeeinst.cn | 齿辊分级破碎机,高低压压球机,立式双动力磨粉机-郑州长城冶金设备有限公司 | 篮球架_乒乓球台_足球门_校园_竞技体育器材_厂家_价格-沧州浩然体育器材有限公司 | 硫酸钡厂家_高光沉淀硫酸钡价格-河南钡丰化工有限公司 | 警方提醒:赣州约炮论坛真的安全吗?2025年新手必看的网络交友防坑指南 | 北京企业宣传片拍摄_公司宣传片制作-广告短视频制作_北京宣传片拍摄公司 | 丹佛斯压力传感器,WISE温度传感器,WISE压力开关,丹佛斯温度开关-上海力笙工业设备有限公司 | ge超声波测厚仪-电动涂膜机-电动划格仪-上海洪富| IIS7站长之家-站长工具-爱网站请使用IIS7站长综合查询工具,中国站长【WWW.IIS7.COM】 | 深圳希玛林顺潮眼科医院(官网)│深圳眼科医院│医保定点│香港希玛林顺潮眼科中心连锁品牌 | 小威小说网 - 新小威小说网 - 小威小说网小说搜索引擎 | 不锈钢管件(不锈钢弯头,不锈钢三通,不锈钢大小头),不锈钢法兰「厂家」-浙江志通管阀 | 印刷人才网 印刷、包装、造纸,中国80%的印刷企业人才招聘选印刷人才网! | 浇钢砖,流钢砖_厂家价低-淄博恒森耐火材料有限公司 | 昆明网络公司|云南网络公司|昆明网站建设公司|昆明网页设计|云南网站制作|新媒体运营公司|APP开发|小程序研发|尽在昆明奥远科技有限公司 | 广东西屋电气有限公司-广东西屋电气有限公司 | 干粉砂浆设备-干粉砂浆生产线-干混-石膏-保温砂浆设备生产线-腻子粉设备厂家-国恒机械 | 户外-组合-幼儿园-不锈钢-儿童-滑滑梯-床-玩具-淘气堡-厂家-价格 | 合肥通道闸-安徽车牌识别-人脸识别系统厂家-安徽熵控智能技术有限公司 | 杭州用友|用友软件|用友财务软件|用友ERP系统--杭州协友软件官网 | 登车桥动力单元-非标液压泵站-非标液压系统-深圳市三好科技有限公司 |