#include < iostream > using namespace std; int main() { cout << 123.23 << " hello " << 100 << '\n'; cout << 10 << ' ' << -10 << '\n'; cout << 100.0 << "\n\n"; cout.unsetf(ios::dec); cout.setf(ios::hex | ios::scientific); cout << 123.23 << " hello " << 100 << '\n'; cout.setf(ios::dec); cout.setf(ios::hex | ios::scientific); cout << 123.23 << " hello " << 100 << '\n'; cout.setf(ios::showpos); cout << 10 << ' ' << -10 << '\n'; cout. setf(ios::showpoint | ios::fixed); cout << 100.0; return 0; }
Category: C++
仮想基本クラス
#include < iostream > using namespace std; class base{ public: int i; }; class derived1 : virtual public base { public: int j; }; class derived2 : virtual public base{ public: int k; }; class derived3 : public derived1, public derived2{ public: int product() { return i * j * k; } }; int main() { derived3 ob; ob.i = 10; ob.j = 3; ob.k = 5; cout << "multiply is " << ob.product() << '\n'; return 0; }
派生クラスが複数の基本クラスを直接継承
#include < iostream > using namespace std; class B1 { public: B1() { cout << "B1 constructor call \n"; } ~B1() { cout << "B1 destructor call \n"; } }; class B2 { int b; public: B2() { cout << "B2 contructor call \n"; } ~B2() { cout << "B2 destructor call \n"; } }; class D : public B1, public B2{ public: D() { cout << "call D constructor\n"; } ~D() { cout << "call D destructor\n"; } }; int main() { D ob; return 0; }
多重継承
#include < iostream > using namespace std; class B1 { int a; public: B1(int x){ a = x; } int geta() { return a; } }; class D1 : public B1 { int b; public: D1(int x, int y): B1(y) { b = x; } int getb(){ return b; } }; class D2 : public D1 { int c; public: D2(int x, int y, int z): D1(y, z) { c = x; } void show() { cout << geta() << ' ' << getb() << ' '; cout << c << '\n'; } }; int main() { D2 ob(1, 2, 3); ob.show(); cout << ob.geta() << ' ' << ob.getb() << '\n'; return 0; }
派生クラスと基本クラス
#include < iostream > using namespace std; class base { int i; public: base(int n){ cout << "call base class constructor\n"; i = n; } ~base() { cout << "call base class destructor\n"; } void showi() { cout << i << '\n'; } }; class derived : public base { int j; public: derived(int n, int m): base(m){ cout << "call derived class constructor \n"; j = n; } ~derived() { cout << "call derived class destructor\n"; } void showj() {cout << j << '\n'; } }; int main() { derived o(10, 20); o.showi(); o.showj(); return 0; }
コンストラクタ、デストラクタ、継承
#include < iostream > using namespace std; class base { public: base() { cout << "call base class constructor\n"; } ~base() { cout << "base class destructor\n"; } }; class derived : public base { public: derived() { cout << "call derived class constructor\n"; } ~derived() { cout << "call derived class destructor\n";} }; int main() { derived o; return 0; }
被保護メンバがpublicとして継承
#include < iostream > using namespace std; class base { protected: int a, b; public: void setab(int n, int m){ a = n; b = m; } }; class derived : public base { int c; public: void setc(int n) { c = n; } void showabc(){ cout << a << ' ' << b << ' ' << c << '\n'; } }; int main() { derived ob; ob.setab(1, 2); ob.setc(3); ob.showabc(); return 0; }
被保護メンバの使用
#include < iostream > using namespace std; class samp { int a; protected: int b; public: int c; samp(int n, int m){ a = n; b = m; } int geta() { return a; } int getb() { return b; } }; int main() { samp ob(10, 20); ob.c = 30; cout << ob.geta() << ' '; cout << ob.getb() << ' ' << ob.c << '\n'; return 0; }
基本クラスのアクセス制御
#include < iostream > using namespace std; class base { int x; public: void setx(int n){ x = n; } void showx() { cout << x << '\n'; } }; class derived : public base { int y; public: void sety(int n){ y = n; } void showy() { cout << y << '\n'; } }; int main() { derived ob; ob.setx(10); ob.sety(20); ob.showx(); ob.showy(); return 0; }
+演算子のオーバーロード
#include < iostream > using namespace std; class coord { int x, y; public: coord() { x=0; y=0; } coord(int i, int j){ x=i; y=j; } void get_xy(int &i, int &j){ i=x; j=y; } coord operator+(coord ob2); coord operator+(int i); }; coord coord::operator+(coord ob2) { coord temp; temp.x = x + ob2.x; temp.y = y + ob2.y; return temp; } coord coord::operator+(int i) { coord temp; temp.x = x + i; temp.y = y + i; return temp; } int main() { coord o1(10, 10), o2(5, 3), o3; int x, y; o3 = o1 + o2; o3.get_xy(x, y); cout << "(o1+o2) x: " << x << ", Y: " << y << "\n"; o3 = o1 + 100; o3.get_xy(x, y); cout << "(o1+100) x: " << x << ", Y: " << y << "\n"; return 0; }