Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@bluepost59
Last active June 24, 2018 13:13
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 bluepost59/79dbd90285d07c312a5326c4121cc86a to your computer and use it in GitHub Desktop.
Save bluepost59/79dbd90285d07c312a5326c4121cc86a to your computer and use it in GitHub Desktop.
抽象クラスを使うメリット ref: https://qiita.com/Bluepost59/items/eef6f48fdd322b0b9791
#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;
}
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()
class bycycle{
public:
int depart(void);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment