Content deleted Content added
m Disambiguating links to Object-orientation (link changed to Object-oriented programming) using DisamAssist. |
No edit summary |
||
Line 80:
===In object-oriented languages===
When creating container classes in statically typed languages, it is inconvenient to write specific implementations for each datatype contained, especially if the code for each datatype is virtually identical. For example, in C++, this duplication of code can be circumvented by defining a class template:
<syntaxhighlight lang="Cpp">
class Animal { ... };
template<typename T>▼
class
// Class contents.▼
▲template <typename T>
class MyList {
▲ // Class contents.
};
</syntaxhighlight>
Above, <code>T</code> is a placeholder for whatever type is specified when the list is created. These "containers-of-type-T", commonly called [[template (programming)|templates]], allow a class to be reused with different datatypes as long as certain contracts such as [[Subtyping|subtype]]s and [[Type signature|signature]] are kept. This genericity mechanism should not be confused with ''[[polymorphism (computer science)|inclusion polymorphism]]'', which is the [[algorithm]]ic usage of exchangeable sub-classes: for instance, a list of objects of type <code>Moving_Object</code> containing objects of type <code>Animal</code> and <code>Car</code>. Templates can also be used for type-independent functions as in the <code>Swap</code> example below:
<syntaxhighlight lang="Cpp">
// "&" denotes a reference
template<typename T>▼
▲template <typename T>
T temp = b;▼
void swap(T& a, T& b) noexcept {
b = a;▼
}
int main(int argc, char* argv[]) {
std::string
swap(world, hello);
std::
}
</syntaxhighlight>
Line 207 ⟶ 216:
<syntaxhighlight lang="cpp">
template <typename T>
T max(T x, T y) {
return x < y ? y : x;
}
</syntaxhighlight>
Line 216 ⟶ 225:
<syntaxhighlight lang="cpp">
std::
</syntaxhighlight>
Line 223 ⟶ 232:
<syntaxhighlight lang="cpp">
int max(int x, int y) {
return x < y ? y : x;
}
</syntaxhighlight>
Line 385 ⟶ 394:
static void Main()
{
int[]
MakeAtLeast<int>(
foreach (int i in
{▼
Console.WriteLine(i); // Print results.
}▼
Console.ReadKey(true);
}
Line 395 ⟶ 406:
{
for (int i = 0; i < list.Length; i++)
if (list[i].CompareTo(lowest) < 0)
list[i] = lowest;
}▼
}
}
Line 408 ⟶ 423:
<syntaxhighlight lang="csharp">
using System;
// A generic class▼
public class GenTest<T>▼
{▼
// A static variable - will be created for each type on reflection▼
static CountedInstances OnePerType = new CountedInstances();▼
class GenericTest<T>
▲ private T _t;
{
private T _t;
public GenTest(T t)▼
▲ {
▲ _t = t;
▲ }
▲ }
//
public
{
//Static variable - this will be incremented once per instance▼
public static int Counter;▼
}
// a class
//simple constructor▼
{
▲ {
//increase counter by one during object instantiation▼
public static int
▲ }
public CountedInstances()
▲ {
}
}
// main code entry point▼
{
// at the end of execution, CountedInstances.Counter = 2▼
static void Main(string[] args)
GenTest<int> g1 = new GenTest<int>(1);▼
{
GenTest<int> g11 = new GenTest<int>(11);▼
GenTest<int> g111 = new GenTest<int>(111);▼
GenTest<double> g2 = new GenTest<double>(1.0);▼
}
}
</syntaxhighlight>
|