Basic Structure of a Class
// 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()
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.
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.