CPP01 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
C++ Module 01

Overview

メモリ割り当て、メンバへのポインタ、参照、switch文

Memory allocation, pointers to members, references and switch statements

Project Links

  • Intra - CPP01
  • GitHub - CPP01
  • My Documents : CPP01, CPP_Modules

    ‍Doxygenで作成されたソースコードドキュメントです。

    クラスの連携図やソースコードの説明に関する情報がまとめられています。

ex00 "BraiiiiiiinnnzzzZ"

Class : Zombie

Files :

Zombie.hpp , Zombie.cpp ,

newZombie.cpp, randomChump.cpp,

main.cpp , Makefile

内容:

キーワード:

メモリ管理(スタックとヒープ)
オブジェクトのライフサイクル(生存期間)
new, delete
例外処理 try{new Zombie("name")} catch (std::bad_alloc &e) {std::cerr << e.what()}
デフォルトコンストラクタ Zombi()
引数付きコンストラクタ Zombi(std::string name)
デストラクタの仕組み ~Zombi()
クラスから独立した関数 newZombie(), randomChump()

動作結果

./BraiiiiiiinnnzzzZ
Stacky created
Stacky: BraiiiiiiinnnzzzZ...
Heapy created
Heapy: BraiiiiiiinnnzzzZ...
Heapy died.
Chumpy created
Chumpy: BraiiiiiiinnnzzzZ...
Chumpy died.
Stacky died.

ex01 "Moar brainz!"

Class : Zombie

Files :

Zombie.hpp , Zombie.cpp ,

zombieHorde.cpp,

main.cpp , Makefile

内容:メモリ管理 複数のオブジェクトを割り当て

キーワード:

newの例: new horde[N];
placement new
 newでメモリ割り当てしたアドレス(オブジェクトの各要素)
 に対してコンストラクタを呼び出すことができる。
 例:new (&horde[j]) Zombie(name);
deleteの例: delete[] horde;

動作結果

./MoreBraiiiiiiinnnzzzZ
Zombie default created (default).
Zombie default created (default).
Zombie default created (default).
Zombie default created (default).
Zombie default created (default).
Zombie Braineater created.
Zombie Braineater created.
Zombie Braineater created.
Zombie Braineater created.
Zombie Braineater created.
Horde created!
Braineater: BraiiiiiiinnnzzzZ...
Braineater: BraiiiiiiinnnzzzZ...
Braineater: BraiiiiiiinnnzzzZ...
Braineater: BraiiiiiiinnnzzzZ...
Braineater: BraiiiiiiinnnzzzZ...
Deleting the horde...
Zombie Braineater destroyed.
Zombie Braineater destroyed.
Zombie Braineater destroyed.
Zombie Braineater destroyed.
Zombie Braineater destroyed.

ex02 "HI THIS IS BRAIN"

Class : std::string

Files :

main.cpp , Makefile

内容:参照とポインタ

キーワード:

std::string brain = "HI THIS IS BRAIN";
std::string *stringPTR = &brain;
std::string &stringREF = brain;

ex03 "Unnecessary violence"

Class : HumanA, HumanB, Weapon

Files :

HumanA.hpp , HumanA.cpp , HumanB.hpp , HumanB.cpp , Weapon.hpp , Weapon.cpp ,

main.cpp , Makefile

内容:

キーワード:


ex04 "Sed is for losers"

Class : FileReplacer, Test

Files :

FileReplacer.hpp , FileReplacer.cpp , Test.hpp , Test.cpp ,

main.cpp , Makefile , test.sh

内容:ファイル名と文字列s1,s2を受け取り、文字置換s1->s2した内容をファイル名.replaceに保存する。

キーワード:

sed like : sed s/$s1/$s2/g input.txt > input.txt.replace
クラス FileReplacer
コンストラクター:デフォルト、パラメータ化、コピーコンストラクター(オブジェクトを新しく作って中身をコピーする)
コピー演算代入子はオブジェクトの参照するコンストラクター オブジェクトの値をコピーする。
デストラクタ:今回は厳密にはいらないが(今回は動的メモリを使っていないため)、基本クラスになる可能性があるので入れている。
<fstream> ライブラリ
std::ifstream inputFile(filename.c_str()); 入力ファイルストリームオブジェクトを作成するコード
std::ifstream ファイルからデータを読み取ることができるクラス
filename.c_str() std::string filename をCスタイルの文字列(const char*)に変換する。
C++98のファイルストリームでは、多くの場合、引数としてCスタイルの文字列が必要になります。
std::ofstream outputFile(outputFilepath.c_str());
例外処理 if (!inputFile.is_open()) {throw std::runtime_error("Error: ..." + filename);}
std::runtime_error 標準例外クラス
std:string のメソッド 文字列操作系
while (std::getline(inputFile, line)) { ... }
inputFileから行ごとに読み取る
line.length() stringの長さを返す
findPos = line.find(s1, pos) posのインデックスを探して返す
if (foundPos == std::string::npos) { resultLine += line.substr(pos); break; }
std::string::npos std::string::find(s1, pos)でpositionが見つからなかった時に返す特別な値
resultLine += line.substr(pos) positionから末尾までの文字列を追加する

ex05 "Harl"

Class : Harl

Files :

Harl.hpp , Harl.cpp ,

main.cpp , Makefile ,

内容:

キーワード:


ex06 "Harl filter"

Class :

Files :

内容:

キーワード:

覚え書き
ex06をしなくてもCPP01はパスできます。

Other

  • Test GitHub Actions workflow on local PC
curl -o deploy_test_github.sh https://gist.githubusercontent.com/kamitsui/16f159bc43315ef3c30e29b6804969b5/raw/43ddf8e95b6f3fe5c93ff8b10bae2d0d8b8eb811/deploy_test_github_act.sh
chmod +x deploy_test_github.sh
./deploy_test_github.sh