C/C++ メモ
|がbitwise OR、&がbitwise AND、^がbitwise XOR、~がbitwise NOT。 #include <stdio.h> // 1バイトデータを2進数の数「字」として表示するための函数。 void printByteAsBin(unsigned char oneByte){ unsigned char binStr[8]; unsigned char mask = 128; // 0b 1000</stdio.h>…
#include <stdio.h> //#include <sstream> //#include <bitset> //#include <string> //#include <iostream> //using namespace std; /*// 1バイトデータを2進数の数「字」として表示するための函数 void printByteAsBin(uint8_t oneByte){ std::stringstream s; s << std::bitset<8>(oneByte); std::stri</iostream></string></bitset></sstream></stdio.h>…
別のクラスのインスタンスをメンバー変数として持つ。 #include "Counter.h" #include <iostream> using namespace std; class GenderCounter{ private: Counter male; // 別のクラスのインスタンスをメンバー変数として持つ。 Counter female; // 別のクラスのインス</iostream>…
CounterEX.h #ifndef CounterEX_h #define CounterEX_h class CounterEX: public Counter{ public: void downVal(); CounterEX(int initVal); CounterEX(); }; #endif CounterEX.cpp #include "Counter.h" #include "CounterEX.h" void CounterEX::downVal()…
Counter.h #ifndef Counter_h #define Counter_h class Counter{ protected: int val; public: void reset2zero(); void upVal(); int getVal(); Counter(int initVal); Counter(); }; #endif Counter.cpp #include "Counter.h" void Counter::reset2zero(){…
(参考: スラスラわかるC++ 第2版, pp.175-180) データに関する処理は必ず当のクラスが提供することが望ましい。要するに○インスタンス.函数()という形で処理することが望ましく、×インスタンス.public変数という形でメンバー変数の読み書きができる状態は望…
#include <iostream> #include <string> using namespace std; int main(){ string str = "abcDEF"; // string 型の変数を初期化する。 cout << str.length() << endl; // 文字列の文字数を取得する。 cout << str.find("c") << endl; // "c"の文字の[位置]を見つける。 cout <</string></iostream>…
構造体はサイズが大きいのでポインタで渡すのが一般的との由。 #include <iostream> #include <string> using namespace std; // 構造体を宣言する。 struct qb { string Name; int Att; int Comp; int Yds; int Td; int Int; double Rate; }; void sute(const qb *str){ // 構</string></iostream>…
#include <iostream> #include <string> using namespace std; // 構造体を宣言する。 struct qb { string Name; int Att; int Comp; int Yds; int Td; int Int; double Rate; }; int main(){ // 構造体をデータ型とした排列を作る。 qb QBs[] = {{"Rodgers, Aaron" , 30, 21, </string></iostream>…
排列とは違って、データ型の異なる値が複数まとめて扱える。 #include <iostream> #include <string> using namespace std; // 構造体は普通はヘッダーファイルで宣言するとの由。 struct qb { string Name; int Att; int Comp; int Yds; int Td; int Int; }; int main(){ qb S</string></iostream>…
排列は返せない。 多値を返したい場合と同じように、値を格納するための排列をあらかじめ用意しておいて、その排列(の先頭の要素のアドレス)を函数に渡して値を書き込ませる。 #include <iostream> using namespace std; void returnArr(int *arr){ arr[0] = 10; arr[1</iostream>…
#include <iostream> using namespace std; // 排列を引数に取るときは必ずポインタ渡しとなるので、勝手に書き換えられないようconst修飾するのがマナーとの由。 double sum(const double arr[], int num){ // こう書いたほうが直観的に思えるが、 //double sum(const</iostream>…
多値は返せない。参照渡しはポインタ渡しよりも少し単純で安全。 #include <iostream> using namespace std; void basicCalc(double a, double b, double &add, double &sub, double &mul, double &div){ add = a + b; sub = a - b; mul = a * b; div = a / b; } int m</iostream>…
多値は返せないので、値の格納先となるアドレスを函数に複数渡してそれぞれのアドレスに値を書き込んでもらうことにする。 #include <iostream> using namespace std; void basicCalc(double a, double b, double *add, double *sub, double *mul, double *div){ *add </iostream>…
#include <iostream> using namespace std; void sute(int *a){ // 假引数として普通の変数ではなくポインタ変数をとって、 *a = 10; // そのポインタで示された位置にあるメモリーに何かを格納する。 } int main(){ int A; // 何かを格納するための変数を用意しておい</iostream>…
ヘッダーファイルでプロトタイプ宣言をしておいて、そのヘッダーファイルをメインファイルにインクルードする。 コンパイルリンクするときにメインファイル.cppと一緒に外部ファイル.cppも指定する。 basicCalc.h double add(double a, double b); double su…
#include <iostream> using namespace std; int main(){ char vals[] = {'a','b','c','d'}; int num = sizeof(vals)/sizeof(char); // 普通のforループ for(char i=0; i</iostream>
函数ではなく演算子なのでカッコでくくる必要はないがカッコでくくるのが慣例との由。 #include <iostream> using namespace std; int main(){ int a[] = {0,1,2,3,4,5}; cout << "int型排列aの全体のバイト数: " << sizeof(a) << endl; cout << "int型のバイト数: " <</iostream>…
#include <iostream> #include <string> using namespace std; int main(){ string sute = "これはテストです。"; cout << sute << endl; cout << "これは試験です。" << endl; char hoge[] = "これも試験です。"; cout << hoge << endl; system("pause"); return 0; }</string></iostream>
#include <iostream> #include <iomanip> using namespace std; int main(){ double dbl; int n; cout << "Enter any real number: "; cin >> dbl; cout << "Specify the number of digits after the decimal: "; cin >> n; cout << fixed << setprecision(n) << dbl << endl; s</iomanip></iostream>…
#include <iostream> using namespace std; int main(){ int num; cout << "Enter any integer: "; cin >> num; cout << "The integer you have entered is: " << num << endl; return 0; }</iostream>
#include <iostream> using namespace std; int main(){ cout << "hello, world" << endl; system("pause"); return 0; } C++は、clするときに/EHscオプションをつける。</iostream>
参考: 日経ソフトウエア 2017年11月号 [雑誌], pp.077-085 下のソースコードをたとえば qb_rate.c というファイル名で c:\test_cpp に保存する。 「開発者コマンド プロンプト for VS 2017」を起動する。 コマンドプロンプトに「cd \test_cpp」と入力して En…
参考: 12歳からはじめる ゼロからのC言語 ゲームプログラミング教室 [新しいプロジェクト] ボタンをクリックする (あるいはメニューバーから [ファイル]、[新規作成]、[プロジェクト] の順に選択する)。 左のテンプレートにある [Visual C++] を選択し、真ん…