#include <string>
#include <iostream>
#include <vector>
#define CC_CONSTRUCTOR_ACCESS protected
class Node
{
public:
virtual bool runAction() =0;
virtual bool showSpritePath() =0;
virtual int getPositionX()const{return 0;}
virtual ~Node(){}
};
class Sprite :public Node
{
public:
bool runAction() override;
static Sprite* createWithPath(conststd::string &path);// create a sprite with path
bool showSpritePath() override; // print the path of a sprite
CC_CONSTRUCTOR_ACCESS:
Sprite():m_strPath(""){}
bool initWithPath(conststd::string &path);
private:
std::string m_strPath;
};
Sprite*Sprite::createWithPath(conststd::string &path)
{
auto sprite = newSprite();
if (sprite && sprite->initWithPath(path)) {
return sprite;
}
else
{
return nullptr;
}
}
boolSprite::runAction()
{
return true;
}
boolSprite::showSpritePath()
{
std::cout <<m_strPath << std::endl;
return true;
}
boolSprite::initWithPath(conststd::string &path)
{
m_strPath = path;
return true;
}
class MySprite :public Sprite
{
public:
virtual int getPositionX() {return 45;}
};
class ColorSprite :public MySprite
{
public:
virtual int getPositionX() {return 99;}
};
int main()
{
int students[]{1,2, 4, 5, 4,6};
for (auto s : students)
{
std::cout << s <<std::endl;
}
std::vector<Node*> vcSprites;
auto s = Sprite::createWithPath("fish.png");
vcSprites.push_back(s);
auto s2 = Sprite::createWithPath("cat.png");
vcSprites.push_back(s2);
for (auto s : vcSprites) {
s->showSpritePath();
}
auto mySprite = newMySprite();
mySprite->showSpritePath();
int x = mySprite->getPositionX();
std::cout << x <<std::endl;
delete mySprite;
auto colorSprite = new ColorSprite();
int y = colorSprite->getPositionX();
std::cout << y <<std::endl;
delete colorSprite;
const MySprite *p = newMySprite();
int z = p->Node::getPositionX();
std::cout << z <<std::endl;
delete p;
for (auto s : vcSprites) {
delete s;
}
vcSprites.clear();
return 0;
}
輸出結果如下:
1
2
4
5
4
6
fish.png
cat.png
45
99
0
Program ended with exit code: 0