Home / Programs / Typescript - Human Inheritance
Programming Example

Typescript - Human Inheritance

👁 105 Views
💻 Practical Program
📘 Step by Step Learning

Information & Algorithm

Typescript - Restaurant Class Create a class "Restaurant" that has

⚫ one public member "menu" to store today's menu list,

⚫ a constructor to initialize it, and

⚫ function "list()" that display today's menu list.  In list() function, log the menu.

Sample Output: ['dosa', 'idly', 'chat']

Note: Input array is passed in the constructor of the class Restaurant

Program Code

class Restaurant {
  public menu: string[];

  constructor(menu: string[]) {
    this.menu = menu;
  }

  list(): void {
    console.log(this.menu);
  }
}

// Example usage:
const todayMenu = ['dosa', 'idly', 'chat'];
const restaurant = new Restaurant(todayMenu);

// Calling the list method to display today's menu
restaurant.list();

Output

// Example usage:
const todayMenu = ['dosa', 'idly', 'chat'];
const restaurant = new Restaurant(todayMenu);

// Calling the list method to display today's menu
restaurant.list();

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.