C# · Chapter 3.6 · Intermediate level

Classes & Objects (OOP — Pokemon Battle Generator!)

Classes और Objects (OOP — पोकेमॉन बैटल जनरेटर!)

Now you unlock the superpower of big games — Object-Oriented Programming (OOP)! Imagine a cookie cutter or architectural blueprint:
• class Pokemon: The Blueprint (Naksha) saying every Pokemon has a name, power, and Attack() move.
• Pokemon p2 = new Pokemon();: Uses new to spawn a real living Object (Charizard, Power 120) from that blueprint! From one blueprint, you can spawn 100 different Pokemons!

अब सीखो बड़े गेम्स की सबसे बड़ी ताकत — OOP (Classes & Objects)! इसे घर के नक्शे और असली घर से समझो:
• class Pokemon: यह सिर्फ़ नक्शा (Blueprint) है जिसमें लिखा है कि हर पोकेमॉन का एक name, एक power, और Attack() होगा।
• Pokemon p2 = new Pokemon();: new शब्द नक्शे से एक असली पोकेमॉन कैरेक्टर (Object) पैदा कर देता है — जैसे Charizard जिसकी पावर 120 है!

In the classroom you can run and edit this code in your browser, get hints and mark the chapter as done. क्लास में यह code ब्राउज़र में चलाकर बदल सकते हो।

Words and symbols in this chapter

class Pokemon { ... }
Blueprint defining what data (name, power) and actions (Attack()) every Pokemon has.
पोकेमॉन का नक्शा जिसमें उसके गुण (name, power) और एक्शन (Attack()) लिखे हैं।
public string name;
public lets Main() read & set this Pokemon's name.
public लिखने से हम बाहर से पोकेमॉन का name सेट कर सकते हैं।
new Pokemon()
new keyword constructs a brand-new Pokemon object in memory!
new शब्द मेमोरी में नया असली पोकेमॉन ऑब्जेक्ट पैदा करता है!
p2.name = "Charizard";
The dot . means 'of' — sets the name of p2 to 'Charizard'.
डॉट . का मतलब है 'का' — यानी p2 का name बराबर 'Charizard'।
p2.Attack();
Commands p2 (Charizard) to unleash its fire attack!
p2 (Charizard) को अपना फ़ायर अटैक चलाने का आदेश देता है!

Ready example

using System;

class Pokemon {
    public string name;
    public int power;
    public void Attack() {
        Console.WriteLine(name + " used Fire Blast! Power: " + power);
    }
}

class Program {
    static void Main() {
        Pokemon p2 = new Pokemon();
        p2.name = "Charizard";
        p2.power = 120;
        p2.Attack();
    }
}

What you will see: A glowing Holographic Battle Card for 🔥 Charizard (Power: 120) unleashes Fire Attack in the Game Arena!

गेम प्रिव्यू में 🔥 Charizard (Power: 120) का चमकता बैटल कार्ड और फ़ायर अटैक एनिमेशन चलेगा!

Line by line

class Pokemon { public string name; public int power; ... }
Defines the reusable Pokemon blueprint.
Pokemon का नक्शा (Class) तैयार किया।
Pokemon p2 = new Pokemon();
Spawns object p2 from the Pokemon class.
नक्शे से असली ऑब्जेक्ट p2 बनाया।
p2.name = "Charizard"; p2.power = 120;
Sets p2's name to 'Charizard' and power to 120.
p2 का नाम 'Charizard' और ताकत 120 रखी।
p2.Attack();
Triggers Charizard's Holographic Battle Card & Fire Attack!
Charizard का होलोग्राफिक बैटल कार्ड और फ़ायर अटैक चलाता है!

Common mistake

✗ Wrong

Pokemon p1 = Pokemon();

✓ Right

Pokemon p1 = new Pokemon();

In C#, you must write the new keyword to spawn an actual object from a class blueprint!

क्लास के नक्शे से असली ऑब्जेक्ट पैदा करने के लिए new शब्द लिखना ज़रूरी है (new Pokemon())!

Your task

Spawn Pokemon p2 = new Pokemon();, set p2.name = "Charizard";, p2.power = 120;, and call p2.Attack();!

Pokemon p2 = new Pokemon(); बनाओ, p2.name = "Charizard"; व p2.power = 120; रखो, और p2.Attack(); चलाओ!

  1. Keep the class Pokemon blueprint at the top.
    ऊपर class Pokemon का नक्शा रहने दो।
  2. Inside Main(), write Pokemon p2 = new Pokemon();.
    Main() में Pokemon p2 = new Pokemon(); लिखो।
  3. Set p2.name = "Charizard";, p2.power = 120;, and call p2.Attack();, then Run!
    p2.name = "Charizard";, p2.power = 120; और p2.Attack(); लिखकर Run दबाओ!
Starter code for the task
using System;

class Pokemon {
    public string name;
    public int power;
    public void Attack() {
        Console.WriteLine(name + " attacked with power " + power);
    }
}

class Program {
    static void Main() {
        // TODO 1: Pokemon p2 = new Pokemon(); se naya object banao
        // TODO 2: p2.name = "Charizard"; aur p2.power = 120; set karo
        // TODO 3: p2.Attack(); call karo
    }
}

Do this task in the classroom: your code is checked automatically and you get hints if something is missing.

Recap

Quick quiz

Which keyword creates a live Object from a C# class blueprint?

C# में class नक्शे से असली Object बनाने के लिए कौन-सा कीवर्ड लगता है?

  1. make
  2. create
  3. new
  4. spawn
Show the answer

Answer: C (new)

Pokemon p1 = new Pokemon(); spawns a new object in memory!

new कीवर्ड क्लास से नया ऑब्जेक्ट तैयार करता है!

Practice problems for this chapter

Solve them in the Practice Zone, where each answer is checked automatically and two hints are available.