main.cpp
[c]
#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;
}
[/c]
Animal.h
[c]
#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__) */
[/c]
Animal.cpp
[c]
#include "Animal.h"
//コンストラクタ
Animal::Animal(char* _name){
strcpy(name, _name);
}
//デストラクタ
Animal::~Animal(){
std::cout << "Destractor!!" << std::endl;
}
char *Animal::getName(){
return name;
}
[/c]