Functions & OOP
Classes
BanglaCode supports object-oriented programming with classes using the srenikeyword (meaning "class" or "category").
Defining a Class
Use the sreni keyword to define a class:
| 1 | // Basic class definition |
| 2 | sreni Person { |
| 3 | // Constructor |
| 4 | shuru(naam, boyosh) { |
| 5 | ei.naam = naam; |
| 6 | ei.boyosh = boyosh; |
| 7 | } |
| 8 | |
| 9 | // Method |
| 10 | kaj introduce() { |
| 11 | dekho("Hi, I am", ei.naam, "and I am", ei.boyosh, "years old"); |
| 12 | } |
| 13 | } |
Creating Instances
Use the notun keyword (meaning "new") to create an instance:
| 1 | // Create instance |
| 2 | dhoro person1 = notun Person("Rahim", 25); |
| 3 | dhoro person2 = notun Person("Karim", 30); |
| 4 | |
| 5 | // Call methods |
| 6 | person1.introduce(); // Hi, I am Rahim and I am 25 years old |
| 7 | person2.introduce(); // Hi, I am Karim and I am 30 years old |
| 8 | |
| 9 | // Access properties |
| 10 | dekho(person1.naam); // Rahim |
| 11 | dekho(person2.boyosh); // 30 |
Constructor (shuru)
The constructor is defined using the shuru keyword (meaning "start" or "begin"). It's called automatically when creating a new instance:
| 1 | sreni Rectangle { |
| 2 | shuru(width, height) { |
| 3 | ei.width = width; |
| 4 | ei.height = height; |
| 5 | dekho("Created rectangle:", width, "x", height); |
| 6 | } |
| 7 | |
| 8 | kaj area() { |
| 9 | ferao ei.width * ei.height; |
| 10 | } |
| 11 | |
| 12 | kaj perimeter() { |
| 13 | ferao 2 * (ei.width + ei.height); |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | dhoro rect = notun Rectangle(10, 5); |
| 18 | // Output: Created rectangle: 10 x 5 |
| 19 | |
| 20 | dekho("Area:", rect.area()); // Area: 50 |
| 21 | dekho("Perimeter:", rect.perimeter()); // Perimeter: 30 |
The 'ei' Keyword (this)
The ei keyword (meaning "this") refers to the current instance:
| 1 | sreni Counter { |
| 2 | shuru(start) { |
| 3 | ei.count = start; |
| 4 | } |
| 5 | |
| 6 | kaj increment() { |
| 7 | ei.count = ei.count + 1; |
| 8 | ferao ei; // Return this for chaining |
| 9 | } |
| 10 | |
| 11 | kaj decrement() { |
| 12 | ei.count = ei.count - 1; |
| 13 | ferao ei; |
| 14 | } |
| 15 | |
| 16 | kaj getValue() { |
| 17 | ferao ei.count; |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | dhoro counter = notun Counter(0); |
| 22 | counter.increment().increment().increment(); |
| 23 | dekho(counter.getValue()); // 3 |
Methods
Methods are defined using the kaj keyword inside the class:
| 1 | sreni BankAccount { |
| 2 | shuru(owner, balance) { |
| 3 | ei.owner = owner; |
| 4 | ei.balance = balance; |
| 5 | } |
| 6 | |
| 7 | kaj deposit(amount) { |
| 8 | jodi (amount > 0) { |
| 9 | ei.balance = ei.balance + amount; |
| 10 | dekho("Deposited:", amount); |
| 11 | } nahole { |
| 12 | dekho("Invalid deposit amount"); |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | kaj withdraw(amount) { |
| 17 | jodi (amount > ei.balance) { |
| 18 | dekho("Insufficient funds!"); |
| 19 | ferao mittha; |
| 20 | } |
| 21 | ei.balance = ei.balance - amount; |
| 22 | dekho("Withdrew:", amount); |
| 23 | ferao sotti; |
| 24 | } |
| 25 | |
| 26 | kaj getBalance() { |
| 27 | ferao ei.balance; |
| 28 | } |
| 29 | |
| 30 | kaj transfer(toAccount, amount) { |
| 31 | jodi (ei.withdraw(amount)) { |
| 32 | toAccount.deposit(amount); |
| 33 | dekho("Transferred", amount, "to", toAccount.owner); |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | dhoro account1 = notun BankAccount("Rahim", 1000); |
| 39 | dhoro account2 = notun BankAccount("Karim", 500); |
| 40 | |
| 41 | account1.deposit(500); |
| 42 | account1.transfer(account2, 300); |
| 43 | |
| 44 | dekho(account1.getBalance()); // 1200 |
| 45 | dekho(account2.getBalance()); // 800 |
Instance Properties
| 1 | sreni Product { |
| 2 | shuru(name, price) { |
| 3 | ei.name = name; |
| 4 | ei.price = price; |
| 5 | ei.quantity = 0; // Default property |
| 6 | } |
| 7 | |
| 8 | kaj addStock(amount) { |
| 9 | ei.quantity = ei.quantity + amount; |
| 10 | } |
| 11 | |
| 12 | kaj sell(amount) { |
| 13 | jodi (amount > ei.quantity) { |
| 14 | dekho("Not enough stock!"); |
| 15 | ferao mittha; |
| 16 | } |
| 17 | ei.quantity = ei.quantity - amount; |
| 18 | ferao sotti; |
| 19 | } |
| 20 | |
| 21 | kaj getValue() { |
| 22 | ferao ei.price * ei.quantity; |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | dhoro laptop = notun Product("Laptop", 50000); |
| 27 | laptop.addStock(10); |
| 28 | |
| 29 | // Access and modify properties directly |
| 30 | dekho(laptop.name); // Laptop |
| 31 | dekho(laptop.quantity); // 10 |
| 32 | |
| 33 | laptop.price = 55000; // Modify property |
| 34 | dekho(laptop.getValue()); // 550000 |
Practical Examples
Todo List
| 1 | sreni TodoList { |
| 2 | shuru() { |
| 3 | ei.tasks = []; |
| 4 | } |
| 5 | |
| 6 | kaj add(task) { |
| 7 | dhokao(ei.tasks, { |
| 8 | text: task, |
| 9 | completed: mittha |
| 10 | }); |
| 11 | dekho("Added:", task); |
| 12 | } |
| 13 | |
| 14 | kaj complete(index) { |
| 15 | jodi (index >= 0 ebong index < dorghyo(ei.tasks)) { |
| 16 | ei.tasks[index].completed = sotti; |
| 17 | dekho("Completed:", ei.tasks[index].text); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | kaj list() { |
| 22 | ghuriye (dhoro i = 0; i < dorghyo(ei.tasks); i = i + 1) { |
| 23 | dhoro task = ei.tasks[i]; |
| 24 | dhoro status = task.completed ? "[X]" : "[ ]"; |
| 25 | dekho(status, task.text); |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | dhoro todos = notun TodoList(); |
| 31 | todos.add("Learn BanglaCode"); |
| 32 | todos.add("Build a project"); |
| 33 | todos.add("Share with friends"); |
| 34 | todos.complete(0); |
| 35 | todos.list(); |
Game Character
| 1 | sreni Character { |
| 2 | shuru(name, health, attack) { |
| 3 | ei.name = name; |
| 4 | ei.maxHealth = health; |
| 5 | ei.health = health; |
| 6 | ei.attack = attack; |
| 7 | ei.isAlive = sotti; |
| 8 | } |
| 9 | |
| 10 | kaj takeDamage(damage) { |
| 11 | ei.health = ei.health - damage; |
| 12 | dekho(ei.name, "took", damage, "damage!"); |
| 13 | |
| 14 | jodi (ei.health <= 0) { |
| 15 | ei.health = 0; |
| 16 | ei.isAlive = mittha; |
| 17 | dekho(ei.name, "has been defeated!"); |
| 18 | } nahole { |
| 19 | dekho(ei.name, "has", ei.health, "HP remaining"); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | kaj attackEnemy(enemy) { |
| 24 | jodi (ei.isAlive ebong enemy.isAlive) { |
| 25 | dekho(ei.name, "attacks", enemy.name); |
| 26 | enemy.takeDamage(ei.attack); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | kaj heal(amount) { |
| 31 | jodi (ei.isAlive) { |
| 32 | ei.health = ei.health + amount; |
| 33 | jodi (ei.health > ei.maxHealth) { |
| 34 | ei.health = ei.maxHealth; |
| 35 | } |
| 36 | dekho(ei.name, "healed to", ei.health, "HP"); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | dhoro hero = notun Character("Hero", 100, 25); |
| 42 | dhoro monster = notun Character("Goblin", 60, 15); |
| 43 | |
| 44 | hero.attackEnemy(monster); |
| 45 | monster.attackEnemy(hero); |
| 46 | hero.heal(10); |
| 47 | hero.attackEnemy(monster); |
| 48 | hero.attackEnemy(monster); |
Stack Data Structure
| 1 | sreni Stack { |
| 2 | shuru() { |
| 3 | ei.items = []; |
| 4 | } |
| 5 | |
| 6 | kaj push(item) { |
| 7 | dhokao(ei.items, item); |
| 8 | } |
| 9 | |
| 10 | kaj pop() { |
| 11 | jodi (ei.isEmpty()) { |
| 12 | dekho("Stack is empty!"); |
| 13 | ferao khali; |
| 14 | } |
| 15 | ferao berKoro(ei.items); |
| 16 | } |
| 17 | |
| 18 | kaj peek() { |
| 19 | jodi (ei.isEmpty()) { |
| 20 | ferao khali; |
| 21 | } |
| 22 | ferao ei.items[dorghyo(ei.items) - 1]; |
| 23 | } |
| 24 | |
| 25 | kaj isEmpty() { |
| 26 | ferao dorghyo(ei.items) == 0; |
| 27 | } |
| 28 | |
| 29 | kaj size() { |
| 30 | ferao dorghyo(ei.items); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | dhoro stack = notun Stack(); |
| 35 | stack.push(1); |
| 36 | stack.push(2); |
| 37 | stack.push(3); |
| 38 | |
| 39 | dekho(stack.peek()); // 3 |
| 40 | dekho(stack.pop()); // 3 |
| 41 | dekho(stack.pop()); // 2 |
| 42 | dekho(stack.size()); // 1 |