Начало savemanager
This commit is contained in:
@@ -68,6 +68,250 @@ bool is_memory_writable(void* addr, size_t size) {
|
||||
return mprotect((void*)page_start, size, PROT_READ | PROT_WRITE | PROT_EXEC) == 0;
|
||||
}
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Базовый класс - Животное
|
||||
class Animal {
|
||||
protected:
|
||||
string name;
|
||||
int age;
|
||||
|
||||
public:
|
||||
// Конструктор
|
||||
Animal(const string& name, int age) : name(name), age(age) {
|
||||
cout << "Animal constructor: " << name << endl;
|
||||
}
|
||||
|
||||
// Виртуальный деструктор
|
||||
virtual ~Animal() {
|
||||
cout << "Animal destructor: " << name << endl;
|
||||
}
|
||||
|
||||
// Виртуальный метод (будет переопределяться)
|
||||
virtual void makeSound() const {
|
||||
cout << name << " makes a generic animal sound" << endl;
|
||||
}
|
||||
|
||||
// Не виртуальный метод (не будет переопределяться)
|
||||
void sleep() const {
|
||||
cout << name << " is sleeping" << endl;
|
||||
}
|
||||
|
||||
string getName() const { return name; }
|
||||
int getAge() const { return age; }
|
||||
};
|
||||
|
||||
// Производный класс - Млекопитающее
|
||||
class Mammal : public Animal {
|
||||
protected:
|
||||
bool hasFur;
|
||||
|
||||
public:
|
||||
// Конструктор
|
||||
Mammal(const string& name, int age, bool hasFur)
|
||||
: Animal(name, age), hasFur(hasFur) {
|
||||
cout << "Mammal constructor: " << name << endl;
|
||||
}
|
||||
|
||||
// Деструктор
|
||||
~Mammal() override {
|
||||
cout << "Mammal destructor: " << name << endl;
|
||||
}
|
||||
|
||||
// Переопределение метода
|
||||
void makeSound() const override {
|
||||
cout << name << " makes a mammal sound" << endl;
|
||||
}
|
||||
|
||||
// Новый метод
|
||||
void feedMilk() const {
|
||||
cout << name << " is feeding milk" << endl;
|
||||
}
|
||||
};
|
||||
|
||||
// Производный класс - Птица
|
||||
class Bird : public Animal {
|
||||
protected:
|
||||
double wingspan;
|
||||
|
||||
public:
|
||||
// Конструктор
|
||||
Bird(const string& name, int age, double wingspan)
|
||||
: Animal(name, age), wingspan(wingspan) {
|
||||
cout << "Bird constructor: " << name << endl;
|
||||
}
|
||||
|
||||
// Деструктор
|
||||
~Bird() override {
|
||||
cout << "Bird destructor: " << name << endl;
|
||||
}
|
||||
|
||||
// Переопределение метода
|
||||
void makeSound() const override {
|
||||
cout << name << " chirps" << endl;
|
||||
}
|
||||
|
||||
// Новый метод
|
||||
void fly() const {
|
||||
cout << name << " is flying with wingspan " << wingspan << "m" << endl;
|
||||
}
|
||||
};
|
||||
|
||||
// Производный класс от Млекопитающего - Собака
|
||||
class Dog : public Mammal {
|
||||
private:
|
||||
string breed;
|
||||
|
||||
public:
|
||||
// Конструктор
|
||||
Dog(const string& name, int age, bool hasFur, const string& breed)
|
||||
: Mammal(name, age, hasFur), breed(breed) {
|
||||
cout << "Dog constructor: " << name << endl;
|
||||
}
|
||||
|
||||
// Деструктор
|
||||
~Dog() override {
|
||||
cout << "Dog destructor: " << name << endl;
|
||||
}
|
||||
|
||||
// Переопределение метода
|
||||
void makeSound() const override {
|
||||
cout << name << " barks: Woof! Woof!" << endl;
|
||||
}
|
||||
|
||||
// Новый метод
|
||||
void fetch() const {
|
||||
cout << name << " is fetching the ball" << endl;
|
||||
}
|
||||
};
|
||||
|
||||
// Производный класс от Млекопитающего - Кошка
|
||||
class Cat : public Mammal {
|
||||
private:
|
||||
int lives;
|
||||
|
||||
public:
|
||||
// Конструктор
|
||||
Cat(const string& name, int age, bool hasFur, int lives = 9)
|
||||
: Mammal(name, age, hasFur), lives(lives) {
|
||||
cout << "Cat constructor: " << name << endl;
|
||||
}
|
||||
|
||||
// Деструктор
|
||||
~Cat() override {
|
||||
cout << "Cat destructor: " << name << endl;
|
||||
}
|
||||
|
||||
// Переопределение метода
|
||||
void makeSound() const override {
|
||||
cout << name << " meows: Meow! Meow!" << endl;
|
||||
}
|
||||
|
||||
// Новый метод
|
||||
void purr() const {
|
||||
cout << name << " is purring" << endl;
|
||||
}
|
||||
};
|
||||
|
||||
// Производный класс от Птицы - Орел
|
||||
class Eagle : public Bird {
|
||||
private:
|
||||
double visionRange;
|
||||
|
||||
public:
|
||||
// Конструктор
|
||||
Eagle(const string& name, int age, double wingspan, double visionRange)
|
||||
: Bird(name, age, wingspan), visionRange(visionRange) {
|
||||
cout << "Eagle constructor: " << name << endl;
|
||||
}
|
||||
|
||||
// Деструктор
|
||||
~Eagle() override {
|
||||
cout << "Eagle destructor: " << name << endl;
|
||||
}
|
||||
|
||||
// Метод НЕ переопределяется (используется версия из Bird)
|
||||
// void makeSound() const override {...}
|
||||
|
||||
// Новый метод
|
||||
void hunt() const {
|
||||
cout << name << " is hunting with vision range " << visionRange << "km" << endl;
|
||||
}
|
||||
};
|
||||
|
||||
// Производный класс от Птицы - Попугай
|
||||
class Parrot : public Bird {
|
||||
private:
|
||||
bool canTalk;
|
||||
|
||||
public:
|
||||
// Конструктор
|
||||
Parrot(const string& name, int age, double wingspan, bool canTalk)
|
||||
: Bird(name, age, wingspan), canTalk(canTalk) {
|
||||
cout << "Parrot constructor: " << name << endl;
|
||||
}
|
||||
|
||||
// Деструктор
|
||||
~Parrot() override {
|
||||
cout << "Parrot destructor: " << name << endl;
|
||||
}
|
||||
|
||||
// Переопределение метода
|
||||
void makeSound() const override {
|
||||
if (canTalk) {
|
||||
cout << name << " says: Hello! Polly wants a cracker!" << endl;
|
||||
} else {
|
||||
cout << name << " squawks" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
// Новый метод
|
||||
void repeat(const string& phrase) const {
|
||||
if (canTalk) {
|
||||
cout << name << " repeats: " << phrase << endl;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Пример использования
|
||||
int main() {
|
||||
cout << "=== Creating objects ===" << endl;
|
||||
|
||||
Dog dog("Rex", 3, true, "German Shepherd");
|
||||
Cat cat("Whiskers", 2, true);
|
||||
Eagle eagle("Thor", 5, 2.1, 3.5);
|
||||
Parrot parrot("Polly", 1, 0.3, true);
|
||||
|
||||
cout << "\n=== Testing methods ===" << endl;
|
||||
|
||||
dog.makeSound(); // Переопределен в Dog
|
||||
dog.fetch(); // Уникальный метод Dog
|
||||
|
||||
cat.makeSound(); // Переопределен в Cat
|
||||
cat.purr(); // Уникальный метод Cat
|
||||
|
||||
eagle.makeSound(); // НЕ переопределен - использует версию Bird
|
||||
eagle.hunt(); // Уникальный метод Eagle
|
||||
|
||||
parrot.makeSound(); // Переопределен в Parrot
|
||||
parrot.repeat("I love C++"); // Уникальный метод Parrot
|
||||
|
||||
cout << "\n=== Testing polymorphism ===" << endl;
|
||||
Animal* animals[] = {&dog, &cat, &eagle, &parrot};
|
||||
|
||||
for (Animal* animal : animals) {
|
||||
animal->makeSound(); // Полиморфный вызов
|
||||
animal->sleep(); // Не виртуальный метод - всегда Animal::sleep()
|
||||
}
|
||||
|
||||
cout << "\n=== Objects going out of scope ===" << endl;
|
||||
// Деструкторы будут вызваны автоматически
|
||||
return 0;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
|
||||
|
||||
get_libapp_base();
|
||||
|
||||
Reference in New Issue
Block a user