Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
C言語でオブジェクト指向プログラミング / object-oriented-in-c
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
kazto
August 02, 2019
Programming
1k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
C言語でオブジェクト指向プログラミング / object-oriented-in-c
2019/8/2 吉祥寺.pm にて、C言語でオブジェクト指向プログラミングできるんですよ、というご紹介をしました。
kazto
August 02, 2019
Transcript
27 slides
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言語まったく使いません!!
More Decks by kazto
See All by kazto
TUIライブラリつくってみた / i-just-make-TUI-library
kazto
1
710
アイカツ!から学ぶビジネスパーソンとしての姿勢 / learn-the-attitude-of-businessperson-from-aikatsu
kazto
0
1.1k
20250228-create-npm-package-with-wasm
kazto
1
180
Golangでwget劣化クローンをつくる / wget clone with golang
kazto
1
560
登壇を応援する会忘年LT_今からやぞ_.pdf
kazto
2
4.8k
Other Decks in Programming
See All in Programming
手の動かし方がわからなかった私が個人開発者になるまで
usagi0920_pochi
0
340
AIエージェントの「テストは通るのに本番で壊れる」を減らす
shibuiwilliam
3
130
Engineering Biometric UX Across Android Versions | DroidKnights 2026
ryunen344
1
220
AIコーディングをセッションで終わらせない: PHPライブラリ開発におけるコンテキスト承継設計
ickx
0
110
Canvas 2D Context Next
petamoriken
0
500
Debian JP Projectのサーバー構成
uwabami
1
190
例外からRich Errorsまで Kotlinエラーハンドリングの進化を読み解く
shiita0903
0
250
Stage 3でも仕様は変わる。アプリケーション開発者のためのTC39プロセスの読み方
bicstone
1
670
テストだけで見つかる? Rubyでつくるメソッド発掘器
izawa
0
160
AI駆動開発4年目の現在地(『あなたはFDEになるべきか?』イベント資料)
kumaiu
0
710
読みやすい!と評判(?)の発表の準備にかける私のこだわり
shoheimitani
0
170
正解の無い問題を 「できるだけ上手く」解く ヒューリスティックで感性を磨こう。〜現実課題をプログラミングで解きあうコミュニティについて〜
allegrogiken
0
120
Featured
See All Featured
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.4k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.7k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
20k
The Invisible Side of Design
smashingmag
301
52k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
2
410
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
49k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
2k
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
620
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
33
5.9k
First, design no harm
axbom
PRO
2
1.4k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
620