Code
Math
// Math.h
#pragma once
int max(int x, int y);
int factorial(int n);
int sum(int n);
// Math.cpp
int max(int x, int y)
{
return x > y ? x : y;
}
int factorial(int n)
{
return n <= 1 ? 1 : n * factorial(n - 1);
}
int sum(int n)
{
return n <= 0 ? 0 : n + sum(n - 1);
}
Weapon
// Weapon.h
#pragma once
class Weapon
{
public:
Weapon();
void fire();
private:
int m_bullets{ 23 };
};
// Weapon.cpp
#include "Weapon.h"
Weapon::Weapon()
{
}
void Weapon::fire()
{
if (m_bullets <= 0) return;
--m_bullets;
}
Character
// Character.h
#pragma once
class Weapon;
class Character
{
public:
Character(const char* name);
~Character();
void f1(Weapon*);
void f2(Weapon&);
void f3(const Weapon&);
Weapon f4();
void f5(Weapon);
private:
const char* m_name;
Weapon* m_weapon;
};
// Character.cpp
#include "Character.h"
#include "Weapon.h"
#include <iostream>
Character::Character(const char* name) : m_name(name)
{
m_weapon = new Weapon();
m_weapon->fire();
std::cout << sizeof(Weapon) << std::endl;
std::cout << sizeof(Weapon*) << std::endl;
std::cin.get();
}
Character::~Character()
{
delete m_weapon;
m_weapon = nullptr;
}
void Character::f1(Weapon*)
{
}
void Character::f2(Weapon&)
{
}
void Character::f3(const Weapon&)
{
}
Weapon Character::f4()
{
return Weapon();
}
void Character::f5(Weapon)
{
}
Main
// main.cpp
#include "Character.h"
int main() {
const Character character("Nux");
// int counter = 10;
// counter %= 0;
return 0;
}
Last updated