類別(Class) 是物件導向程式設計的基礎,暫且先將他想像成是我們可以自定義的資料型態,他有著自己的資料和函數。在這一小節我們將創建一個 Cat 類別:
Cat
#ifndef CLASSES_CAT_H #define CLASSES_CAT_H class Cat { public: void speak(); void jump(); }; #endif //CLASSES_CAT_H
#include <iostream> #include "Cat.h" using namespace std; void Cat::speak() { cout << "Meow!" << endl; } void Cat::jump() { cout << "Jumping to top of bookcase" << endl; }
#include <iostream> #include "Cat.h" using namespace std; int main() { Cat cat1; cat1.speak(); cat1.jump(); return 0; }
← Subroutine Developing a Program: The Particle Fire Simulation →