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
Cpp chap.10
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Iseya
April 08, 2018
Programming
17
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Cpp chap.10
Iseya
April 08, 2018
More Decks by Iseya
See All by Iseya
0章 開発環境の導入(C/C++入門)
hoge_piyo
0
14
Other Decks in Programming
See All in Programming
為什麼你並不需要ViewModel / No, you don't need a ViewModel
lovee
1
500
複数の Claude Code が"放置"されてしまう問題をCLI ダッシュボードを自作して解決した話
sumihiro3
1
700
Claude CodeとAgentCore Gatewayを繋ぐ際の認証認可 / Authentication and authorization when connecting Claude Code with AgentCore Gateway
har1101
2
310
komatsuna「分散システムにおけるバグ分析手法」
komatsunaqa
0
270
これって Effect でできたのでは? / TSKaigi Mashup Kansai #2
susisu
0
250
What's New in Android 2026
veronikapj
0
260
Cloudflare is Agents
chimame
0
170
Google Apps Script で Ruby を動かす
kawahara
0
270
改善しないと、タスクが回らない。 “てんこ盛りポジション” を引き継いだ情シスの、入社3ヶ月の業務改善録
krm963
0
270
170k Jobs a Day on GKE: Scaling Mercari's CI Platform - and What's Next for AI-Native Development
junyaokabe
0
110
in-process GraphQL のすすめ #ginzajs
izumin5210
4
1.4k
言葉の格闘技のススメ~紙とペンと言葉から始める、キャリアの描き方~
progresscicada
2
160
Featured
See All Featured
Chasing Engaging Ingredients in Design
codingconduct
0
270
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
2
3.8k
Ethics towards AI in product and experience design
skipperchong
2
350
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.3k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
800
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Optimising Largest Contentful Paint
csswizardry
37
3.9k
Fireside Chat
paigeccino
42
4k
Producing Creativity
orderedlist
PRO
348
40k
A Tale of Four Properties
chriscoyier
163
24k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
780
Transcript
10章 クラスの基本 1
クラスの概要 2 ・クラスとは いろいろな変数や関数をオブジェクトとしてまとめる事ができるもの。 C++のように、オブジェクトを用いるプログラム言語のことを、 オブジェクト指向の言語ということもある。 他には、Java や Objective-C などがある。
・クラスの使い方 クラスを作り、オブジェクトを作って、各メンバにアクセスする。 例えば、Statusクラスを作って、mob1,mob2 というオブジェクトを作り、 mob1やmob2のオブジェクトの中のstrongという変数だったり、 showStrong() という関数にアクセスする。 Q.構造体は? A.知らない子ですね ※クラス ≠ オブジェクト ※クラス ≒ 型
クラスの宣言 3 class クラス名{ アクセス指定子: 変数や関数の宣言 ︙ }; ・クラスの宣言 ・メンバ関数の定義(クラスの
外) 型名 クラス名::メンバ関数名(引数){ 「処理」 } ※アクセス指定子は後述 ・説明 クラス内で宣言や定義されたものを メンバという。 また、その中で定義や宣言された 変数…メンバ変数 関数…メンバ関数 という。 メンバ ・メンバ関数の定義(クラスの 中) 型名 メンバ関数名(引数){ 「処理」 } ※クラスの中で定義する場合、普通の関数と同じ ※クラスの中で定義した関数は、強制的にインライン関数になる。 ← ; を忘れない ↓通常は、こっちを使う
メンバへのアクセス 4 ・構文 オブジェクト名 .メンバ名 #include<iostream> using namespace std; class
Status { public: int strong; void showStrong(); }; void Status::showStrong() { cout << "私の戦闘力は" << strong << endl; } int main() { Status freeza; freeza.strong = 530000; freeza.showStrong(); return 0; } 私の戦闘力は530000↓ ・実行結果 ←オブジェクトの作成 ←メンバ変数へのアクセス ←メンバ関数へのアクセス ←アクセス指定子はpublicにしておく ←メンバ変数の定義 ←メンバ関数の宣言 メンバ関数の定義 ※この.を ドット演算子という
アクセス指定子 5 ・アクセス指定子 これをつけることによって、 クラスの中からしか、アクセスできないメンバと クラスの外からでも、アクセスできる メンバを作成できる。 ・アクセス指定子の種類 public …
クラスの外からでもメンバに「アクセス可能」 private … そのクラスの外からメンバへの「アクセス不可」 「アクセス可能」… オブジェクト名.メンバ というのが行える。 「アクセス不可」… オブジェクト名.メンバ というのが行えない。 ※public「公共の」 private「私有の」
アクセス指定子の利用① 6 #include<iostream> using namespace std; class square { private:
int tate; int yoko; public: int getArea() { return tate*yoko; }; void setTateYoko(int, int); }; void square::setTateYoko(int t, int y) { if (t > 0 && y > 0) { tate = t; yoko = y; } else cout << "error!!" << endl; } int main() { square box; box.setTateYoko(10, 5); cout << "面積:" << box.getArea() << endl; box.setTateYoko(-1, 3); cout << "面積:" << box.getArea() << endl; return 0; } 面積を計算し、 ↓それを返す ・説明 縦と横の長さは正の値しかとらないので、 負の値があるとき、"error!!"と表示。 ・実行結果 面積:50 error!! 面積:50 負の数があるので、 初期化を行わない Ⅰ Ⅱ
アクセス指定子の利用② 7 ・カプセル化 変数(データ)や関数(機能)などを、ひとまとめにして、 アクセス指定子を用いてメンバを保護すること。 ・アクセス指定子の設定 変数 … private 関数
… public に設定することが多い。 ・アクセス指定子の省略 すべて private のメンバになる。
コンストラクタの基本① 8 ・コンストラクタ オブジェクトが作成されると自動的に呼び出されるメンバ関数。 オブジェクトの作成と同時に呼び出されることから、 普通の関数と違い、戻り値を持たない(voidとすら書かない)。 メンバ変数の初期化を行うためによく用いられる。 ・構文(宣言と定義が別々) class クラス名{
public: ︙ クラス名(引数の型); ︙ }; クラス名::クラス名(引数){ 処理 } ←public でないと呼び出せない ↓この関数がコンストラクタ 戻り値の型なし ・構文(宣言と定義を同時) class クラス名{ public: ︙ クラス名(引数){ 処理 }; ︙ }; ※こっちの場合、 コンストラクタはインライン関数になる ※引数はなくても良い。