Skip to content

Instantly share code, notes, and snippets.

@K-atc
Created June 16, 2017 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save K-atc/ac57fcc07128528b70131d02414b6791 to your computer and use it in GitHub Desktop.
Save K-atc/ac57fcc07128528b70131d02414b6791 to your computer and use it in GitHub Desktop.
C++のclassのメソッド呼び出しのお勉強 https://wandbox.org/permlink/OBeuz8iRwQUwjqM7
Start
d = 114514
3
10
924
0
Finish
#include <iostream>
class Base {
public:
Base(int a, int b);
Base(std::string a, std::string b);
int plus(int a, int b);
int plus(std::string a, std::string b); // Polymorphism
int getC(void);
int getD(void);
private:
int c;
int d;
};
Base::Base(int a, int b):
// initializer lists (see http://en.cppreference.com/w/cpp/language/initializer_list)
c{plus(a, b)},
d{114514}
{
}
Base::Base(std::string a, std::string b):
c{plus(a, b)},
d{810}
{
}
int Base::plus(int a, int b){
return a + b;
}
int Base::plus(std::string a, std::string b){
return std::stoi(a) + std::stoi(b);
}
int Base::getC(void){
return c;
}
int Base::getD(void){
return d;
}
int main(){
Base m(1,2);
// std::cout << "d = " << m.d << std::endl; // compile error, "error: 'int Base::d' is private within this context"
std::cout << "d = " << m.getD() << std::endl;
std::cout << m.getC() << std::endl;
std::cout << (new Base(1,9))->getC() << std::endl;
std::cout << (new Base("810","114"))->getC() << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment