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
2sreni 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
2dhoro person1 = notun Person("Rahim", 25);
3dhoro person2 = notun Person("Karim", 30);
4
5// Call methods
6person1.introduce(); // Hi, I am Rahim and I am 25 years old
7person2.introduce(); // Hi, I am Karim and I am 30 years old
8
9// Access properties
10dekho(person1.naam); // Rahim
11dekho(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:

1sreni 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
17dhoro rect = notun Rectangle(10, 5);
18// Output: Created rectangle: 10 x 5
19
20dekho("Area:", rect.area()); // Area: 50
21dekho("Perimeter:", rect.perimeter()); // Perimeter: 30

The 'ei' Keyword (this)

The ei keyword (meaning "this") refers to the current instance:

1sreni 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
21dhoro counter = notun Counter(0);
22counter.increment().increment().increment();
23dekho(counter.getValue()); // 3

Methods

Methods are defined using the kaj keyword inside the class:

1sreni 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
38dhoro account1 = notun BankAccount("Rahim", 1000);
39dhoro account2 = notun BankAccount("Karim", 500);
40
41account1.deposit(500);
42account1.transfer(account2, 300);
43
44dekho(account1.getBalance()); // 1200
45dekho(account2.getBalance()); // 800

Instance Properties

1sreni 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
26dhoro laptop = notun Product("Laptop", 50000);
27laptop.addStock(10);
28
29// Access and modify properties directly
30dekho(laptop.name); // Laptop
31dekho(laptop.quantity); // 10
32
33laptop.price = 55000; // Modify property
34dekho(laptop.getValue()); // 550000

Practical Examples

Todo List

1sreni 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
30dhoro todos = notun TodoList();
31todos.add("Learn BanglaCode");
32todos.add("Build a project");
33todos.add("Share with friends");
34todos.complete(0);
35todos.list();

Game Character

1sreni 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
41dhoro hero = notun Character("Hero", 100, 25);
42dhoro monster = notun Character("Goblin", 60, 15);
43
44hero.attackEnemy(monster);
45monster.attackEnemy(hero);
46hero.heal(10);
47hero.attackEnemy(monster);
48hero.attackEnemy(monster);

Stack Data Structure

1sreni 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
34dhoro stack = notun Stack();
35stack.push(1);
36stack.push(2);
37stack.push(3);
38
39dekho(stack.peek()); // 3
40dekho(stack.pop()); // 3
41dekho(stack.pop()); // 2
42dekho(stack.size()); // 1