From 90b5dc147d8c4ee3eb5e4c15b82750a45b1199f6 Mon Sep 17 00:00:00 2001 From: GR-tejas <95667323+GR-tejas@users.noreply.github.com> Date: Tue, 4 Feb 2025 15:00:03 +0530 Subject: [PATCH 1/2] Classes and Objects --- Pokemon/main.cpp | 154 +++++++++++++++++++++++++++++------------------ 1 file changed, 97 insertions(+), 57 deletions(-) diff --git a/Pokemon/main.cpp b/Pokemon/main.cpp index b15829ec..6008dd95 100644 --- a/Pokemon/main.cpp +++ b/Pokemon/main.cpp @@ -1,73 +1,113 @@ -#include +#include +#include using namespace std; enum class PokemonChoice { - Charmander, - Bulbasaur, - Squirtle, - InvalidChoice + CHARMANDER = 1, + BULBASAUR, + SQUIRTLE, + PIKACHU //default }; -int main() { +enum class PokemonType { + FIRE, + GRASS, + WATER, + ELECTRIC +}; + +class Pokemon { +public: + string name; + PokemonType type; + int health; + + Pokemon() { + + } - PokemonChoice chosen_pokemon = PokemonChoice::InvalidChoice; + Pokemon(string p_name, PokemonType p_type, int p_health) { + name = p_name; + type = p_type; + health = p_health; + } + + void attack() { std::cout << name << "attacks with a powerful move!\n"; } +}; +class Player { +public: string name; + Pokemon chosenPokemon; - int choice; - - cout << "Professor Oak: Hello there! Welcome to the world of Pokemon!\n"; - cout << "Professor Oak: My name is Oak. People call me the Pokemon Professor!\n"; - - cout << "Professor Oak: But enough about me. Let's talk about you!\n"; - - cout << "Professor Oak: First, tell me, what’s your name?\n"; - cin >> name; - cout << "Professor Oak: Ah, " << name<< "! What a fantastic name!\n"; - cout << "Professor Oak: You must be eager to start your adventure. But first, you will need a Pokemon of your own!\n"; - - cout << "Professor Oak: I have three Pokemon here with me. They are all quite feisty!\n"; - cout << "Professor Oak: Choose wisely...\n"; - cout << "1. Charmander - The fire type. A real hothead!\n"; - cout << "2. Bulbasaur - The grass type. Calm and collected!\n"; - cout << "3. Squirtle - The water type. Cool as a cucumber!\n"; - - cin >> choice; - - switch (choice) { - case 1: - chosen_pokemon = PokemonChoice::Charmander; - break; - case 2: - chosen_pokemon = PokemonChoice::Bulbasaur; - break; - case 3: - chosen_pokemon = PokemonChoice::Squirtle; - break; - default: - chosen_pokemon = PokemonChoice::InvalidChoice; - break; + void choosePokemon(int choice) { + switch ((PokemonChoice)choice) { + case PokemonChoice::CHARMANDER: + chosenPokemon = Pokemon("Charmander", PokemonType::FIRE, 100); + break; + case PokemonChoice::BULBASAUR: + chosenPokemon = Pokemon("Bulbasaur", PokemonType::GRASS, 100); + break; + case PokemonChoice::SQUIRTLE: + chosenPokemon = Pokemon("Squirtle", PokemonType::WATER, 100); + break; + default: + chosenPokemon = Pokemon("Pikachu", PokemonType::ELECTRIC, 100); + break; + } + cout << "Player " << name << " chose " << chosenPokemon.name << "!\n"; } +}; - switch (chosen_pokemon) { - case PokemonChoice::Charmander: - cout << "Professor Oak: A fiery choice! Charmander is yours!\n"; - break; - case PokemonChoice::Bulbasaur: - cout << "Professor Oak: A fine choice! Bulbasaur is always ready to grow on you!\n"; - break; - case PokemonChoice::Squirtle: - cout << "Professor Oak: Splendid! Squirtle will keep you cool under pressure!\n"; - break; - default: - cout << "Professor Oak: Hmm, that doesn't seem right. Let me choose for you...\n"; - chosen_pokemon = PokemonChoice::Charmander; - cout << "Professor Oak: Just kidding! Let's go with Charmander, the fiery dragon in the making!\n"; - break; +class ProfessorOak { +public: + string name; + + void greetPlayer(Player& player) { + cout << name << ": Hello there! Welcome to the world of Pokemon!\n"; + cout << name << ": My name is Oak. People call me the Pokemon Professor!\n"; + cout << name << ": But enough about me. Let's talk about you!\n"; } - cout << "Professor Oak: " << (chosen_pokemon == PokemonChoice::Charmander ? "Charmander" : chosen_pokemon == PokemonChoice::Bulbasaur ? "Bulbasaur" : "Squirtle") << " and you, " << name << ", are going to be the best of friends!\n"; + void offerPokemonChoices(Player& player) { + cout << name << ": First, tell me, what’s your name?\n"; + getline(cin, player.name); + cout << name << ": Ah, " << player.name << "! What a fantastic name!\n"; + cout << name << ": You must be eager to start your adventure. But first, you’ll need a Pokemon of your own!\n"; + + cout << name << ": I have three Pokemon here with me. They’re all quite feisty!\n"; + cout << name << ": Choose wisely...\n"; cout << "1. Charmander - The fire type. A real hothead!\n"; + cout << "2. Bulbasaur - The grass type. Calm and collected!\n"; + cout << "3. Squirtle - The water type. Cool as a cucumber!\n"; + + int choice; + cout << name << ": So, which one will it be? Enter the number of your choice: "; + cin >> choice; + player.choosePokemon(choice); + } +}; + +int main() { + + ProfessorOak professor; + Pokemon placeholderPokemon; + Player player; + + placeholderPokemon.name = "Pikachu"; + placeholderPokemon.type = PokemonType::ELECTRIC; + placeholderPokemon.health = 40; + + player.name = "Trainer"; + + professor.name = "Professor Oak"; + + professor.greetPlayer(player); + professor.offerPokemonChoices(player); + + cout << "Professor Oak: " << player.chosenPokemon.name << " and you, " << player.name << ", are going to be the best of friends!\n"; cout << "Professor Oak: Your journey begins now! Get ready to explore the vast world of Pokemon!\n"; + return 0; -} + +} \ No newline at end of file From fe60c665083b3d0309acc8e376045324b8ceb422 Mon Sep 17 00:00:00 2001 From: GR-tejas <95667323+GR-tejas@users.noreply.github.com> Date: Tue, 4 Feb 2025 15:50:49 +0530 Subject: [PATCH 2/2] Constructors and different types --- Pokemon/main.cpp | 120 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 97 insertions(+), 23 deletions(-) diff --git a/Pokemon/main.cpp b/Pokemon/main.cpp index 6008dd95..c4b24bd7 100644 --- a/Pokemon/main.cpp +++ b/Pokemon/main.cpp @@ -1,47 +1,92 @@ -#include -#include +#include +#include using namespace std; +// Define an enum for Pokemon choices enum class PokemonChoice { CHARMANDER = 1, BULBASAUR, SQUIRTLE, - PIKACHU //default + PIKACHU // Default choice }; +// Define an enum for Pokemon types enum class PokemonType { FIRE, GRASS, WATER, - ELECTRIC + ELECTRIC, + NORMAL // Added for the default constructor }; +// Pokemon class definition class Pokemon { public: + // Attributes string name; PokemonType type; int health; + // Default constructor Pokemon() { - + name = "Unknown"; + type = PokemonType::NORMAL; + health = 50; + cout << "A new Pokemon has been created with the default constructor!\n"; } + // Parameterized constructor Pokemon(string p_name, PokemonType p_type, int p_health) { name = p_name; type = p_type; health = p_health; + cout << "A new Pokemon named " << name << " has been created!\n"; + } + + // Copy constructor + Pokemon(const Pokemon& other) { + name = other.name; + type = other.type; + health = other.health; + cout << "A new Pokemon has been copied from " << other.name << "!\n"; } - void attack() { std::cout << name << "attacks with a powerful move!\n"; } + // Destructor + ~Pokemon() { + cout << name << " has been released.\n"; + } + + // Method to simulate attacking (just for demonstration) + void attack() { + cout << name << " attacks with a powerful move!\n"; + } }; +// Player class definition class Player { public: + // Attributes string name; Pokemon chosenPokemon; + // Default constructor + Player() { + name = "Trainer"; + chosenPokemon = Pokemon(); // Using the default Pokemon constructor + cout << "A new player named " << name << " has been created!\n"; + } + + // Parameterized constructor + Player(std::string p_name, Pokemon p_chosenPokemon) { + name = p_name; + chosenPokemon = p_chosenPokemon; + cout << "Player " << name << " has been created!\n"; + } + + // Method to choose a Pokemon void choosePokemon(int choice) { switch ((PokemonChoice)choice) { + case PokemonChoice::CHARMANDER: chosenPokemon = Pokemon("Charmander", PokemonType::FIRE, 100); break; @@ -55,59 +100,88 @@ class Player { chosenPokemon = Pokemon("Pikachu", PokemonType::ELECTRIC, 100); break; } + cout << "Player " << name << " chose " << chosenPokemon.name << "!\n"; } }; +// ProfessorOak class definition class ProfessorOak { public: + // Attributes string name; + // Parameterized constructor + ProfessorOak(string p_name) { + name = p_name; + } + + // Method to greet the player void greetPlayer(Player& player) { cout << name << ": Hello there! Welcome to the world of Pokemon!\n"; cout << name << ": My name is Oak. People call me the Pokemon Professor!\n"; cout << name << ": But enough about me. Let's talk about you!\n"; } + // Method to ask the player to choose a Pokemon void offerPokemonChoices(Player& player) { cout << name << ": First, tell me, what’s your name?\n"; - getline(cin, player.name); + getline(std::cin, player.name); cout << name << ": Ah, " << player.name << "! What a fantastic name!\n"; cout << name << ": You must be eager to start your adventure. But first, you’ll need a Pokemon of your own!\n"; + // Presenting Pokemon choices cout << name << ": I have three Pokemon here with me. They’re all quite feisty!\n"; - cout << name << ": Choose wisely...\n"; cout << "1. Charmander - The fire type. A real hothead!\n"; + cout << name << ": Choose wisely...\n"; + cout << "1. Charmander - The fire type. A real hothead!\n"; cout << "2. Bulbasaur - The grass type. Calm and collected!\n"; cout << "3. Squirtle - The water type. Cool as a cucumber!\n"; int choice; cout << name << ": So, which one will it be? Enter the number of your choice: "; cin >> choice; + player.choosePokemon(choice); } }; int main() { - - ProfessorOak professor; - Pokemon placeholderPokemon; - Player player; - - placeholderPokemon.name = "Pikachu"; - placeholderPokemon.type = PokemonType::ELECTRIC; - placeholderPokemon.health = 40; - - player.name = "Trainer"; - - professor.name = "Professor Oak"; - + // Task 1: Test default and parameterized constructors + Pokemon defaultPokemon; // Using default constructor + Pokemon charmander("Charmander", PokemonType::FIRE, 100); // Using parameterized constructor + + cout << "Pokemon Details:\n"; + cout << "Name: " << defaultPokemon.name << "\nType: " << (int)defaultPokemon.type << "\nHealth: " << defaultPokemon.health << "\n"; + cout << "Name: " << charmander.name << "\nType: " << (int)charmander.type << "\nHealth: " << charmander.health << "\n"; + + // Task 2: Test the copy constructor + Pokemon bulbasaur("Bulbasaur", PokemonType::GRASS, 100); // Create a Pokemon + Pokemon bulbasaurCopy = bulbasaur; // Copy the Pokemon + cout << "Original Pokemon Health: " << bulbasaur.health << "\n"; + cout << "Copied Pokemon Health: " << bulbasaurCopy.health << "\n"; + + // Modify the copy + bulbasaurCopy.health = 80; + cout << "After Modification:\n"; + cout << "Original Pokemon Health: " << bulbasaur.health << "\n"; + cout << "Copied Pokemon Health: " << bulbasaurCopy.health << "\n"; + + // Task 3: Test the destructor + { + Pokemon squirtle("Squirtle", PokemonType::WATER, 100); // Pokemon will be destroyed at the end of this scope + } // Destructor will be called here + + // Continue with the main flow of the game + ProfessorOak professor("Professor Oak"); + Player player("Ash", charmander); + + // Greet the player and offer Pokemon choices professor.greetPlayer(player); professor.offerPokemonChoices(player); + // Conclude the first chapter cout << "Professor Oak: " << player.chosenPokemon.name << " and you, " << player.name << ", are going to be the best of friends!\n"; cout << "Professor Oak: Your journey begins now! Get ready to explore the vast world of Pokemon!\n"; - return 0; - } \ No newline at end of file