Last active
June 24, 2018 13:13
-
-
Save bluepost59/79dbd90285d07c312a5326c4121cc86a to your computer and use it in GitHub Desktop.
抽象クラスを使うメリット ref: https://qiita.com/Bluepost59/items/eef6f48fdd322b0b9791
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using std::cout; | |
using std::endl; | |
//抽象クラスvehicleの定義 | |
class vehicle{ | |
public: | |
virtual void start(void)=0; | |
virtual void stop(void)=0; | |
}; | |
//vehicleを継承したクラスcarの定義 | |
class car: public vehicle{ | |
public: | |
void start(void){ | |
cout << "car start" << endl; | |
} | |
void stop(void){ | |
cout << "car stop." << endl; | |
} | |
}; | |
//vehicleを継承したクラスmotorcycleの定義 | |
class motorcycle: public vehicle{ | |
public: | |
void start(void){ | |
cout << "motorcycle start" << endl; | |
} | |
void stop(void){ | |
cout << "motorcycle stop." << endl; | |
} | |
}; | |
//テスト部 | |
int main(void){ | |
vehicle* mycar; | |
mycar = new car(); | |
(*mycar).start(); | |
(*mycar).stop(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from abc import ABCMeta | |
from abc import abstractmethod | |
#抽象クラスvehicleの定義 | |
class vehicle(metaclass = ABCMeta): | |
@abstractmethod | |
def start(self): | |
pass | |
@abstractmethod | |
def stop(self): | |
pass | |
#vehicleを継承したクラスcarの定義 | |
class car(vehicle): | |
def start(self): | |
print("car start.") | |
def stop(self): | |
print("car stop") | |
#vehicleを継承したクラスmotorcycleの定義 | |
class motorcycle(vehicle): | |
def start(self): | |
print("moto start.") | |
def stop(self): | |
print("moto stop") | |
#テスト部 | |
if __name__ == "__main__": | |
mycar = car() | |
mycar.start() | |
mycar.stop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class bycycle{ | |
public: | |
int depart(void); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment