Dart notes by chat gpt

 Certainly! Let's start by covering the fundamentals of the Dart programming language and then delve into object-oriented programming (OOP) concepts in Dart.


### Dart Fundamentals:


#### 1. Variables and Data Types:

In Dart, you can declare variables using the `var`, `final`, or `const` keywords. Dart has various data types, including:

   - `int` for integers.

   - `double` for floating-point numbers.

   - `String` for text.

   - `bool` for boolean values.

   - `List` and `Map` for collections.


Here's an example of variable declaration:


```dart

var age = 25;

String name = "John";

final piValue = 3.14159;

```


#### 2. Functions:

Functions are blocks of code that can be reused. You can define a function in Dart like this:


```dart

int add(int a, int b) {

  return a + b;

}

```


#### 3. Control Flow:

Dart supports common control flow structures, including `if`, `else`, `switch`, `for`, `while`, and `do-while` loops.


```dart

if (condition) {

  // Code to execute if the condition is true.

} else {

  // Code to execute if the condition is false.

}


for (var i = 0; i < 5; i++) {

  // Code to repeat five times.

}

```


#### 4. Lists and Maps:

Dart provides collections like lists and maps to work with groups of data.


```dart

List<int> numbers = [1, 2, 3, 4, 5];

Map<String, int> ages = {'Alice': 30, 'Bob': 25};

```


#### 5. Classes and Objects (OOP):

Now, let's dive into object-oriented programming concepts in Dart.


### Object-Oriented Programming (OOP) in Dart:


#### 1. Classes and Objects:

In OOP, a class is a blueprint for creating objects. An object is an instance of a class.


```dart

class Person {

  String name;

  int age;


  Person(this.name, this.age);


  void sayHello() {

    print('Hello, my name is $name and I am $age years old.');

  }

}


void main() {

  var person = Person('Alice', 30);

  person.sayHello();

}

```


#### 2. Inheritance:

Dart supports inheritance, allowing you to create new classes based on existing ones.


```dart

class Student extends Person {

  String major;


  Student(String name, int age, this.major) : super(name, age);


  void study() {

    print('$name is studying $major.');

  }

}

```


#### 3. Encapsulation:

Dart allows you to control access to class members using `public`, `private`, and `protected` keywords. By default, members are public.


```dart

class Circle {

  double _radius; // Private member


  Circle(this._radius);


  double calculateArea() {

    return 3.14159 * _radius * _radius;

  }

}

```


#### 4. Polymorphism:

Polymorphism allows objects of different classes to be treated as objects of a common superclass.


```dart

void printInfo(Person person) {

  print('Name: ${person.name}, Age: ${person.age}');

}


void main() {

  var alice = Person('Alice', 30);

  var bob = Student('Bob', 25, 'Computer Science');


  printInfo(alice); // Calls printInfo with a Person object.

  printInfo(bob);   // Calls printInfo with a Student object.

}

```


These are the fundamental concepts of Dart and object-oriented programming in Dart. Practice and experimentation are essential to solidify your understanding, so try writing some Dart code to apply these concepts in practice.

Comments

Popular posts from this blog

swapping the alternate values in given array's element ! 17/11/2022

c++ basic question

Learning stage | c++ programs