2006-11-24から1日間の記事一覧

演算子のオーバーロード

#include <stdio.h> #include <string.h> class CString { char* s; public: CString(); CString(const char*); CString(const CString&); ~CString(); void Print(); CString& operator=(const CString&); friend CString operator+(const CString&, const CString&); }; CStri</string.h></stdio.h>…

平均点を計算する関数を仮想関数にする

cstudent.h #include <stdio.h> #include "cstudent.h" class CSciStudent : public CStudent //理系生徒クラス { int phys; public: void SetPhys(int pt = 0){phys = pt;} int GetPhys(){return phys;} float CalcAverage(){return (GetEng() + GetMath() + phys) /</stdio.h>…

仮想関数

#include <stdio.h> | #include <stdio.h> | class Base | class Base { | { public: | public: void Who(){printf( | virtual void Who(){printf( "私はBaseです\n");} | "私はBaseです\n");} }; | }; | class Derived : public Base | class Derived : public Base { | { pub</stdio.h></stdio.h>…

派生方法と基底クラスのメンバーが派生クラスでどう振舞うかの関係。

基底クラスの 派生方法 メンバー 私的派生 保護派生 公開派生 private アクセス不可 アクセス不可 アクセス不可 protected private protected prodected public private prodected public

基底クラスから派生クラスを作成する

cstudent.h #ifndef CSTUDENT_H #define CSTUDENT_H class CStudent { int id; int eng; int math; public: void SetID(int i){id = i;} int GetID(){return id;} void SetEng(int pt = 0){eng = pt;} int GetEng(){return eng;} void SetMath(int pt = 0){m…

アクセス演算子 private:

#include <stdio.h> class COrder { public: int number; int cHamberger; int cPotato; int cDrink; private: int total; public: void CalcTotal(); void PrintTotal(){printf("注文番号%d:%d円です\n", number, total);} }; void COrder::CalcTotal() { total = (c</stdio.h>…

スコープ解決演算子

#include <stdio.h> class COrder { public: int number; int cHamberger; int cPotato; int cDrink; int total; void CalcTotal(); void PrintTotal(){printf("注文番号%d:%d円です\n", number, total);} }; void COrder::CalcTotal() { total = (cHamberger * 200 +</stdio.h>…

メンバー関数を追加する

#include <stdio.h> class COrder { public: int number; int cHamberger; int cPotato; int cDrink; int total; void CalcTotal(){total = (cHamberger * 200 +cPotato * 180 + cDrink * 150) * 1.05;} void PrintTotal(){printf("注文番号%d:%d円です\n", number, t</stdio.h>…

参照引数を持つ関数

void swap(int& a, int& b) { int temp; temp = a; a = b; b = temp; }

参照変数の使用例

#include <stdio.h> int main(void) { int a = 0; int& ref_a = a; ref_a = 3; printf("a = %d\n", a); printf("ref_a = %d\n", ref_a); return 0; }ポインタを使って記述↓ #include <stdio.h> int main(void) { int a = 0; int* p_a = &a; *p_a = 3; printf("a = %d\n", a); p</stdio.h></stdio.h>…

関数オーバーロード

#include <stdio.h> int max(int a, int b) { printf("2個のint値を比較します\n"); return (a > b ? a : b); } int max(int a, int b, int c) { printf("3個のint値を比較します\n"); int d = (a > b ? a : b); return (d > c ? d : c); } float max(float a, float </stdio.h>…

奥村晴彦C,C++

http://oku.edu.mie-u.ac.jp/~okumura/c/

デフォルト引数を持つ関数

#include <stdio.h> void func(int a, int b = 1, int c = 2) { printf("a = %d, b= %d, c = %d\n", a, b, c); } int main(void) { func(7, 8, 9); func(7, 8); func(7); return 0; }実行結果 a = 7, b= 8, c = 9 a = 7, b= 8, c = 2 a = 7, b= 1, c = 2 D:\cp></stdio.h>

コンストラクタ・デストラクタ

#include <stdio.h> class CTest { public: int number; CTest(); ~CTest(); void print(void){printf("number = %d\n", number);} }; CTest::CTest() { printf("コンストラクタが呼ばれました\n"); } CTest::~CTest() { printf("デストラクタが呼ばれました\n"); } v</stdio.h>…