Rodhos Soft

備忘録を兼ねた技術的なメモです。Rofhos SoftではiOSアプリ開発を中心としてAndroid, Webサービス等の開発を承っております。まずはご相談下さい。

基礎

c++の基礎的なことをさらった。

//
//  main.cpp
//  cppABC
//
//  Created by KatagiriSo on 2017/06/28.
//  Copyright © 2017年 rodhos. All rights reserved.
//

#include <iostream>
#include <sstream>
#include <iomanip>
#include <fstream>



// ref:http://ppp-lab.sakura.ne.jp/ProgrammingPlacePlus/cpp/language/004.html


struct Point {
    int x;
    int y;
    int d2();
};

enum MyType : int {
    GREEN,RED,YELLOW
};

enum struct MyType2 : int {
    GREEN,RED,YELLOW
};

enum class MyType3 : int {
    GREEN,RED,YELLOW
};

double getValue();

namespace spaceA {
    double getValue();
    
    namespace specialB {
        double getValue();
    }
    
    inline namespace Util {
        double getUtilValue();
    }
}

// file only
namespace {
    const int MAX = 1000;
}

int globalX = 2000;


namespace SpaceC {
    void helloSpaceC();
}

int twice(int x);
int twice(float x);
//float twice(int x); NG

// default
int fc(int x = 1000);

// template
template <typename T>
void echo(T x);

template <typename R, typename T1, typename T2>
R echo(T1 x, T2 y);

// new function def
auto nfunc(int x) -> int;
auto nfunc2(int x) -> decltype(x);


// inline
inline int infunc(int x);


// class
class Man {
    
public:
    void hello();
    
    Man(); // default constructer
    Man(std::string name);
    
    ~Man();
    
//    static int commonNumber;
    bool operator==(const Man& rhs) const;
    
    inline bool operator!=(const Man& rhs) const {
        return !(*this == rhs);
    }
    

private:
    std::string name;
    int age;
    void util();
    int n = 10;
};

void hello(Man *m);


// constexpr


template <typename T>
class Poi {
public:
    void echo(T x);
};


int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    
    // struct
    Point p;
    p.x = 10;
    
    std::cout << p.x << std::endl;
    

    // enum
    MyType type = MyType::RED;
    MyType2 type2 = MyType2::RED;
    MyType3 type3 = MyType3::RED;
    std::cout << type << std::endl;
    std::cout << static_cast<int>(type2) << std::endl;
    std::cout << static_cast<int>(type3) << std::endl;

    
    // auto
    auto v = getValue();
    std::cout << v << std::endl;
    
    // namespace
    double v2 = spaceA::getValue();
    double v3 = spaceA::specialB::getValue();
    double v4 = spaceA::getUtilValue();
    std::cout << v2 << std::endl;
    std::cout << v3 << std::endl;
    std::cout << v4 << std::endl;
    std::cout << MAX << std::endl;
    std::cout << globalX << std::endl;
    

    // using
    using namespace SpaceC;
    helloSpaceC();

    // alias
    namespace B = spaceA::specialB;
    double b = B::getValue();
    std::cout << b << std::endl;
    
    // string
    std::string hoge("hoge");
    std::string empty;
    std::string str = hoge;
    std::string hoge2("hoge");
    std::cout << hoge << std::endl;
    std::cout << "empty=" << empty << std::endl;
    std::cout << str << empty << std::endl;
    if (hoge == hoge2) {
        std::cout << "hoge == hoge2" << std::endl;
    }
    
    std::cout << hoge.size() << std::endl;
    std::cout << hoge.length() << std::endl;
    std::cout << empty.empty() << std::endl;

    // ostringstream
    std::ostringstream oss;
    oss << "hello" << std::endl;
    
    std::cout << oss.str() << std::endl;

    // istringstream
    std::istringstream inputstream("hoge 200 300.3");
    std::string s;
    int num;
    double d;
    inputstream >> s >> num >> d;
    std::cout << "s=" << s << " num=" << num << " d=" << d << std::endl;

    // escape
    std::string stresc = "a\nb\nc";
    std::string stresc2 = R"(a
b
c)";
    std::cout << stresc << std::endl;
    std::cout << stresc2 << std::endl;

#if 0
    // cin
    std::cout << "input number" << std::endl;
    int x;
    std::cin >> x;
    std::cout << "x=" << x << std::endl;
#endif

    // error
    std::cerr << "cerr!" << std::endl;
    std::clog << "clog!" << std::endl; // buffering

#if 0
    // manipulator
    std::cout << "input string (size 3)" << std::endl;
    char ch[3];
    std::cin >> std::setw(sizeof(ch)) >> ch;
    std::cout << "ch=" << ch << std::endl; // endl is also manipulator
#endif

    // file
    std::ofstream ofs("test.txt");
    if (!ofs) {
        std::cerr << "[error]open file" << std::endl;
        std::exit(1);
    }
    ofs << "Hello, World\n123" << std::endl; // auto close
    
    std::ifstream ifs("test.txt");
    if (!ifs) {
        std::cerr << "[error]open file for ifstream." << std::endl;
        std::exit(1);
    }
    
    std::string bufstr;
    ifs >> bufstr;
    
    std::cout << bufstr << std::endl; //[note] output is Hello,
    
    
    std::ifstream ifs2("test.txt");
    if (!ifs2) {
        std::cerr << "[error]open file for ifstream." << std::endl;
        std::exit(1);
    }
    std::string bufstr2;
    getline(ifs2, bufstr2);
    std::cout << bufstr2 << std::endl; //[note] output is Hello, World
    
    getline(ifs2, bufstr2);
    std::cout << bufstr2 << std::endl; //[note] output is 123
    
    if (ifs2.eof()) {
        std::cout << "eof" << std::endl; //[note] output is 123
    }
    
    // null pointer
    int* a = nullptr;
    if (a == nullptr) {
        std::cout << "nullptr" << std::endl;
    }
    
    // decltype
    int sample = 100;
    decltype(sample) sample2 = 200; // Type of sample2 is Int.
    decltype(getValue()) sample3 = 3.3;//  Type of sample3 is Int.
    std::cout << sample << sample2 << sample3 << std::endl;

    // overload
    std::cout << twice(3) << std::endl;
    std::cout << twice(3.5f) << std::endl;

    // default
    std::cout << fc() << std::endl;

    // template
    echo(3);
    echo("hello");
    echo<std::string>("hello");
    std::cout << echo<int>(3, 4) << std::endl;
    std::cout << echo<float>(3.1f, 4.2f) << std::endl;

    // new function def
    std::cout << nfunc(10) << std::endl;
    std::cout << nfunc2(10) << std::endl;

    // inline
    std::cout << infunc(100) << std::endl;
    
    Man man;
    Man man2("poi");
    
    man.hello();
    man2.hello();
    
    hello(&man);
    
    
    const int c = 3;
//    c = 4; error
    
    Man *man3 = new Man();
    man3->hello();
    
    delete man3;
    
//    std::cout << "man.commonNumber = " << Man::commonNumber << std::endl; // 3

    
    // ref
    int r = 0;
    int& r2 = r;
    r2 = 100;
    std::cout << r << std::endl; // 100

    // template class
    Poi<std::string> p1;
    Poi<int> p2;
    p1.echo("hello");
    p2.echo(3);
    
    using Name = std::string;
    Name name = "name";
    
    typedef Poi<std::string> PoiStr;
    PoiStr p3;
    p3.echo("hello2");


    return 0;
}

int Point::d2() {
    return this->x + this->y;
}

double getValue() {
    return 2.0;
}

namespace spaceA {
    double getValue() {
        return 20.0;
    }
    
    
    namespace specialB {
        double getValue() {
            return 30;
        }
    }
    
    
    inline namespace Util {
        double getUtilValue() {
            return 50;
        }
    }

}

namespace SpaceC {
    void helloSpaceC() {
        std::cout << "Hello, CpaceC." << std::endl;

    }
}

int twice(int x) {
    return x * 2;
}

int twice(float x) {
    return x * 2;
}

int fc(int x) {
    return x+1000;
}

template <typename T>
void echo(T x) {
    std::cout << x << std::endl;
}

template <typename R, typename T1, typename T2>
R echo(T1 x, T2 y) {
    return static_cast<R>(x+y);
}

auto nfunc(int x) -> int {
    return x*10;
}

auto nfunc2(int x) -> decltype(x) {
    return x * 100;
}

inline int infunc(int x){
    return x+1;
}

void Man::hello() {
    std::cout << "Hello, my name is " << this->name << std::endl;

}

void Man::util() {
    std::cout << "util" << std::endl;

}

Man::Man() : name("name"), age(0){
//    commonNumber++;
}

Man::Man(std::string name) {
    this->name = name;
    age = 0;
//    commonNumber++;
}

Man::~Man() {
    std::cout << "dec" << std::endl;
}

void hello(Man *m) {
    m->hello();
}

bool Man::operator==(const Man &rhs) const {
    return name == rhs.name;
}

template <typename T>
void Poi<T>::echo(T x) {
    std::cout << "echo" << x << std::endl;
}