Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
C言語でオブジェクト指向プログラミング / object-oriented-in-c
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
kazto
August 02, 2019
Programming
1
930
C言語でオブジェクト指向プログラミング / object-oriented-in-c
2019/8/2 吉祥寺.pm にて、C言語でオブジェクト指向プログラミングできるんですよ、というご紹介をしました。
kazto
August 02, 2019
Tweet
Share
More Decks by kazto
See All by kazto
TUIライブラリつくってみた / i-just-make-TUI-library
kazto
1
450
アイカツ!から学ぶビジネスパーソンとしての姿勢 / learn-the-attitude-of-businessperson-from-aikatsu
kazto
0
640
20250228-create-npm-package-with-wasm
kazto
1
120
Golangでwget劣化クローンをつくる / wget clone with golang
kazto
1
520
登壇を応援する会忘年LT_今からやぞ_.pdf
kazto
2
4.6k
Other Decks in Programming
See All in Programming
AI Agent の開発と運用を支える Durable Execution #AgentsInProd
izumin5210
7
2.3k
開発者から情シスまで - 多様なユーザー層に届けるAPI提供戦略 / Postman API Night Okinawa 2026 Winter
tasshi
0
200
Rust 製のコードエディタ “Zed” を使ってみた
nearme_tech
PRO
0
150
SourceGeneratorのススメ
htkym
0
190
余白を設計しフロントエンド開発を 加速させる
tsukuha
7
2.1k
CSC307 Lecture 02
javiergs
PRO
1
770
React 19でつくる「気持ちいいUI」- 楽観的UIのすすめ
himorishige
11
6k
dchart: charts from deck markup
ajstarks
3
990
AI時代のキャリアプラン「技術の引力」からの脱出と「問い」へのいざない / tech-gravity
minodriven
20
6.9k
Spinner 軸ズレ現象を調べたらレンダリング深淵に飲まれた #レバテックMeetup
bengo4com
1
230
AI巻き込み型コードレビューのススメ
nealle
0
130
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
510
Featured
See All Featured
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.3k
The Invisible Side of Design
smashingmag
302
51k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
200
How Software Deployment tools have changed in the past 20 years
geshan
0
32k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
120
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
90
WCS-LA-2024
lcolladotor
0
450
The SEO Collaboration Effect
kristinabergwall1
0
350
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.4k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
350
Prompt Engineering for Job Search
mfonobong
0
160
Transcript
C言語で オブジェクト指向 プログラミング 2019/8/2 吉祥寺.pm kazto
一句
PCの 電源切れて 冷や汗なう
技書博 お疲れ様 でした!
自己紹介 • なまえ ◦ kazto(かずと) • しょくれき ◦ 組み込み系SIerで16年半 ◦
社員数5人のWebベンチャーに半年 ◦ 2019年7月から、クーコム株式会社にJoin! ◦ 採用もやっていき! • しゅみ ◦ HR/HM鑑賞
We Are Hiring!
C言語で オブジェクト指向 プログラミング
Perlとオブジェクト指向 あとづけ→(昔は)無かった C言語→もともと無かった
PerlとC Perlは(ほぼ) C言語で書かれている
おしながき • オブジェクト指向ってなんだったっけ? • え?C言語? • クラス ◦ コンストラクタ・デストラクタ •
メソッド • 継承とカプセル化
オブジェクト指向ってなんだったっけ? • よく言われるのは ◦ 継承、多態性、カプセル化
オブジェクト指向ってなんだったっけ? • Wikipediaによると(アラン・ケイ) ◦ メッセージング ◦ ローカルな記憶保持と保護と実行状態 の隠蔽 ◦ 徹底的な遅延バインディング
オブジェクト指向ってなんだったっけ?
オブジェクト指向ってなんだったっけ? 今回は継承、多態性、カプセル 化、で進めさせて下さい。 m(_ _)m
え?C言語!? • C言語は、手続き型プログラミング言語 ◦ クラスという文法は存在しません ◦ private、protectedというキーワードはありません ◦ コンストラクタ、デストラクタという機構もありません
では、どうするか? • 「運用でカバー」します(いやホントに) • クラス≒構造体 • 命名規則で、private、protectedを分類する ◦ それらの関数は、「呼ばない」 •
コンストラクタ・デストラクタは普通の関数 ◦ 自分で呼ぶ
クラス=struct • 当然、C言語にはクラスという文法は無い (二回目) • 構造体を用いて、クラスを表現する • 当然、privateやprotectedなメソッドは作成でき ない。すべてpublicとなる
よくある動物クラスの例 typedef struct classAnimal { enumAnimalType type; funcSay say; }
classAnimal; typedef struct classDog { classAnimal base; funcRun run; } classDog; typedef struct classBird { classAnimal base; funcFly fly; } classBird;
コンストラクタ・デストラクタ • コンストラクタ・デストラクタは、普通の関数として 宣言する • 手動でコンストラクタ・デストラクタ関数を呼んで、 メモリの確保・解放を行う ◦ コンストラクタ内で、オブジェクトにメソッドをセットする ◦
メンバ変数に別のオブジェクトを持つなら、さらにそのコンストラクタ役関数を呼 び、メモリを確保、初期化する
コンストラクタ・デストラクタ void ctorClassAnimal(classAnimal *this, enumAnimalType type, funcSay *say) { this->type
= type; this->funcSay = say; } void ctorClassDog(classDog *this) { ctorClassAnimal((classAnimal *)this, enumAnimalTypeDog, funcBark); this->funcRun = funcRun; } classDog * newClassDog() { classDog *ptr = (classDog *)malloc(sizeof(classDog)); if(ptr) { ctorClassDog(ptr); } return ptr; }
コンストラクタ・デストラクタ #include ”animal.h” #include ”dog.h” int main(void) { classAnimal *dog
= NULL; dog = (classAnimal *)newClassDog(); dog->funcSay(dog); return 0; } void funcBark(classAnimal *this) { printf(”%d: Bow Wow!!”, this->type); }
メソッド • メンバ関数は、予め関数ポインタ型をtypedefして おき、コンストラクタ内でセットする • メソッドは、関数ポインタをtypedefすることで表 現する
• 当然、クラスの継承を行う文法も、無い • 親クラス構造体を、子クラスのメンバの一番目に宣言する ◦ 一番目であることが重要 ◦ キャストしたときに、親オブジェクトと子オブジェクトが同じアドレスを指す • 親クラス構造体にキャストすることで、子クラス構造体のメンバは隠ぺいされる
• 子クラス構造体メンバにアクセスしたい時は、子クラス構造体にキャストする 継承とカプセル化
Child 継承とカプセル化 Parent Parent キャストして隠蔽 キャストして 子要素にアクセス
いかがでしたでしょうか? • C言語であってもオブジェクト指向に寄せた設計を 行うことで、より体系だった開発を行うことができ ます
技術書典7、待ってろよ!!(震え声
We Are Hiring! 大事なことなので2回 弊社はC言語まったく使いません!!