Difference between revisions of "Object-oriented programming"
m |
|||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
− | Object-oriented programming (OOP) is | + | Object-oriented programming (OOP) is a way of organizing code and data using objects, which are essentially self-contained units of data and methods that operate on that data. OOP is a fundamental approach to software development in many languages like Java, C++, and Python. |
+ | |||
+ | OOP relies on certain key concepts: | ||
+ | * '''Classes and Objects''': A class is a blueprint for creating objects, which are instances of that class. | ||
+ | * '''Encapsulation''': Bundling data (fields) and methods (procedures) that operate on that data within a single unit (object). | ||
+ | * '''Inheritance''': Creating new classes (subclasses) based on existing classes (superclasses), inheriting their attributes and behaviors. | ||
+ | * '''Polymorphism''': Allowing objects of different classes to be treated as objects of a common superclass, enabling code reuse and flexibility. | ||
+ | |||
+ | == Classes and Objects == | ||
+ | In their most straightforward form, classes act as blueprints for objects. It's like a how you can drive into a rural area of a city and find many new homes with the same design. The developers used one set of architectural blueprints to build all of those houses. In the same way, one class can be used to construct many objects. | ||
+ | |||
+ | An object, on the other hand, is an actual active instance of a built class in memory. In most languages, there is a keyword that tells the compiler to create an object from a class such as <c>[[new]]</c>. | ||
+ | |||
+ | Classes are mostly defined by their fields and methods. Fields are just like variables except they are tied to a class or object. Methods are similar to functions and they always have access to the fields within their class definition as if they those fields were passed as a parameter to the method. Sometimes a language will require the keyword prefix <c>[[self]]</c> when referencing a field or method within the class definition. | ||
+ | |||
+ | In C++, let's define an Animal class: | ||
+ | |||
+ | class Animal { | ||
+ | protected: | ||
+ | std::string name; | ||
+ | int age; | ||
+ | public: | ||
+ | Animal(const std::string& name, int age) | ||
+ | : name(name), age(age) {} | ||
+ | virtual ~Animal() {} | ||
+ | virtual void speak() const = 0; // Pure virtual function | ||
+ | virtual void info() const { | ||
+ | std::cout << "Name: " << name << ", Age: " << age << std::endl; | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | |||
+ | In MiniScript, the same class would be defined as: | ||
+ | <ms> | ||
+ | Animal = { name:"" } | ||
+ | |||
+ | Animal.create = function(name, age) | ||
+ | a = new Animal | ||
+ | a.name = name | ||
+ | a.age = age | ||
+ | return a | ||
+ | end function | ||
+ | |||
+ | Animal.speak = function | ||
+ | end function | ||
+ | |||
+ | Animal.info = function | ||
+ | print "Name: " + self.name + ", Age: " + self.age | ||
+ | end function | ||
+ | </ms> | ||
+ | |||
+ | |||
+ | == Encapsulation == | ||
+ | Encapsulation is more of a design technique to OOP programming and less of a technical aspect of any OOP language. When you code using this technique, it allows the programmer to organize their project by grouping all behaviour and related data into classes. When instantiated as an Object, this single container can be passed around in your code to "interact" with other objects. | ||
+ | |||
+ | When coding with encapsulation in mind, all behaviours (methods) and properties (fields) related to a class will be defined in that class. Knowing where to draw the line to include or exclude behaviour and properties tends to make or break an OOP design. Often it is better to define something using multiple classes rather than trying to make one monolithic class. A good rule of thumb is to dedicate a class to a single thing. | ||
+ | |||
+ | Looking back at our Animal class above, we can see that it has two fields (name and age) and essentially two methods (speak and info). Looking at <c>info()</c> we can see that it only uses <c>name</c> and <c>age</c>. As mentioned, that is what is defined in the class and so those are the only variables it has access to. | ||
+ | |||
+ | <blockquote> 📚 The other two methods in C++ are the constructor and destructor. In MiniScript, you are not required to have either of these. However, creating a constructor may help to declutter your code and make it easier to read.</blockquote> | ||
+ | |||
+ | |||
+ | == Inheritance == | ||
+ | |||
+ | |||
+ | |||
MiniScript supports prototype-based OOP through <c>[[new]]</c>, <c>[[self]]</c>, <c>[[super]]</c>, and <c>[[isa|__isa]]</c>. | MiniScript supports prototype-based OOP through <c>[[new]]</c>, <c>[[self]]</c>, <c>[[super]]</c>, and <c>[[isa|__isa]]</c>. | ||
− | |||
[[Category:Language]] | [[Category:Language]] |
Latest revision as of 20:42, 5 June 2025
Object-oriented programming (OOP) is a way of organizing code and data using objects, which are essentially self-contained units of data and methods that operate on that data. OOP is a fundamental approach to software development in many languages like Java, C++, and Python.
OOP relies on certain key concepts:
- Classes and Objects: A class is a blueprint for creating objects, which are instances of that class.
- Encapsulation: Bundling data (fields) and methods (procedures) that operate on that data within a single unit (object).
- Inheritance: Creating new classes (subclasses) based on existing classes (superclasses), inheriting their attributes and behaviors.
- Polymorphism: Allowing objects of different classes to be treated as objects of a common superclass, enabling code reuse and flexibility.
Classes and Objects
In their most straightforward form, classes act as blueprints for objects. It's like a how you can drive into a rural area of a city and find many new homes with the same design. The developers used one set of architectural blueprints to build all of those houses. In the same way, one class can be used to construct many objects.
An object, on the other hand, is an actual active instance of a built class in memory. In most languages, there is a keyword that tells the compiler to create an object from a class such as new
.
Classes are mostly defined by their fields and methods. Fields are just like variables except they are tied to a class or object. Methods are similar to functions and they always have access to the fields within their class definition as if they those fields were passed as a parameter to the method. Sometimes a language will require the keyword prefix self
when referencing a field or method within the class definition.
In C++, let's define an Animal class:
class Animal { protected: std::string name; int age; public: Animal(const std::string& name, int age) : name(name), age(age) {} virtual ~Animal() {} virtual void speak() const = 0; // Pure virtual function virtual void info() const { std::cout << "Name: " << name << ", Age: " << age << std::endl; } };
In MiniScript, the same class would be defined as:
Animal = { name:"" }
Animal.create = function(name, age)
a = new Animal
a.name = name
a.age = age
return a
end function
Animal.speak = function
end function
Animal.info = function
print "Name: " + self.name + ", Age: " + self.age
end function
Encapsulation
Encapsulation is more of a design technique to OOP programming and less of a technical aspect of any OOP language. When you code using this technique, it allows the programmer to organize their project by grouping all behaviour and related data into classes. When instantiated as an Object, this single container can be passed around in your code to "interact" with other objects.
When coding with encapsulation in mind, all behaviours (methods) and properties (fields) related to a class will be defined in that class. Knowing where to draw the line to include or exclude behaviour and properties tends to make or break an OOP design. Often it is better to define something using multiple classes rather than trying to make one monolithic class. A good rule of thumb is to dedicate a class to a single thing.
Looking back at our Animal class above, we can see that it has two fields (name and age) and essentially two methods (speak and info). Looking at info()
we can see that it only uses name
and age
. As mentioned, that is what is defined in the class and so those are the only variables it has access to.
📚 The other two methods in C++ are the constructor and destructor. In MiniScript, you are not required to have either of these. However, creating a constructor may help to declutter your code and make it easier to read.
Inheritance
MiniScript supports prototype-based OOP through new
, self
, super
, and __isa
.