C++ ポケモン

include

class Pokemon {
public:
// コンストラクタ
Pokemon(const std::string& nickname) {
this->nickname_ = nickname;
// nickname_ = nickname; と省略できる
}
// デストラクタ
virtual ~Pokemon() {
}
virtual void attack() = 0;
protected:
std::string nickname_;
};

class Pikachu : public Pokemon {
public:
Pikachu(const std::string& nickname) : Pokemon(nickname) {
}
virtual ~Pikachu() {
}
void attack() override {
std::cout << nickname_ << “の十万ボルト!” << std::endl;
}
};

class Zenigame : public Pokemon {
public:
Zenigame(const std::string& nickname) : Pokemon(nickname) {
}
virtual ~Zenigame() {
}
void attack() override {
std::cout << nickname_ << “のハイドロポンプ!” << std::endl;
}
};

int main() {
int i;
Pokemon* monsters[2];
monsters[0] = new Pikachu(“サトシのピカチュウ”);
monsters[1] = new Zenigame(“サトシのゼニガメ”);
std::cin >> i;
if (i >= 0 && i < 2) { monsters[i]->attack();
}
delete monsters[0];
delete monsters[1];
}

C++ 関数の宣言

include <iostream>

using namespace std;

int numOfDivisors(int num) {
int count = 0;
for (int i = 1; i <= num; ++i) {
if (num % i == 0) {
count++;
}
}
return count;
}

int main() {
// Count the divisors for numbers from 10 to 20
for (int n = 10; n <= 20; ++n) {
int result = numOfDivisors(n);
std::cout << “Number of divisors for ” << n << ” is: ” << result << std::endl;
}
std::cout << “Hello World!\n”;
}