Basic Structure of a Class

  1. What is Class
  2. What is Object
  3. Difference between Class and Object.
  4. Use of Classes and Object Inside and Outside of Main Class.
  5. Can Class and Object exist or use Individually?
  6. Real-World Analogy Of Class and Object.
  7. Access Modifiers (public, private, protected, etc.)
  8. Member Function(Inner and Outer class function)

Basic Structure of Class in Swift

// Define a class representing a Student
class Student {
    // Properties/Attributes
    var name: String
    var age: Int
    var grade: String
    
    // Constructor (initializer)
    init(name: String, age: Int, grade: String) {
        self.name = name
        self.age = age
        self.grade = grade
    }
    
    // Method to display information about the student
    func displayInfo() {
        print("Student Name: \(name), Age: \(age), Grade: \(grade)")
    }
    
    // Additional Attribute
    var school: String = "ABC School"
}

// Create an object of the Student class
let myStudent = Student(name: "John", age: 18, grade: "12th")

// Accessing the school attribute
print("Student's School: \(myStudent.school)")

// Call the method to display information about the student
myStudent.displayInfo()

1. What is Class

In object-oriented programming (OOP), a class is a blueprint or template for creating objects (instances). It defines the properties (attributes) and behaviours (methods) that all objects of that type will have.

2. What is Object

In object-oriented programming (OOP), an object is a fundamental concept representing a real-world entity or abstraction. An object is an instance of a class, and it encapsulates both data (attributes or properties) and behaviours (methods or functions) that operate on that data.

Or

A variable that refers to a class.

3. Difference between Class and Object.