main.cpp
#include <iostream> #include "Animal.h" int main(int argc, const char * argv[]) { char name[20] = "ポチ"; Animal animal(name); Animal *dog = new Animal(name); // insert code here... std::cout << "Goody, " << animal.getName() << std::endl; std::cout << "Hello, " << dog->getName() << std::endl; //new したオブジェクトはdeleteしないとDestractorが呼ばれない delete dog; return 0; }
Animal.h
#ifndef __CPlusSample__Animal__ #define __CPlusSample__Animal__ #include <iostream> class Animal{ public: Animal(char *name); ~Animal(); char *getName(); private: char name[20]; }; #endif /* defined(__CPlusSample__Animal__) */
Animal.cpp
#include "Animal.h" //コンストラクタ Animal::Animal(char* _name){ strcpy(name, _name); } //デストラクタ Animal::~Animal(){ std::cout << "Destractor!!" << std::endl; } char *Animal::getName(){ return name; }