Generic programming: Difference between revisions

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 ListCar { ... };
 
// Class contents.
template <typename T>
class MyList {
// Class contents.
};
 
ListMyList<Animal> list_of_animalsanimalList;
ListMyList<Car> list_of_carscarList;
</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>
void Swap(T& a, T& b) { // A similar, but safer and potentially faster function
// is defined in the standard library header <utility>
template <typename T>
T temp = b;
void swap(T& a, T& b) noexcept {
b = a;
a = T temp = b;
T temp b = ba;
a = {temp;
}
 
int main(int argc, char* argv[]) {
std::string world = "World!";
std::string helloworld = "Hello, World!";
Swap(world, std::string hello) = "Hello, ";
swap(world, hello);
std::cout <<println("{}{}", world <<, hello << ‘\n’); // Output is "Hello, World!".
}
</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::cout <<println("{}", max(3, 7)); // Outputs 7.
</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[] arraya = { 0, 1, 2, 3 };
MakeAtLeast<int>(arraya, 2); // Change arraya to { 2, 2, 2, 3 }
foreach (int i in arraya)
{
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)
_t = t;{
list[i] = lowest;
private T _t; }
}
}
}
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();
 
// A generic class
// a data member
class GenericTest<T>
private T _t;
{
// A static variable - will be created for each type on reflection
static CountedInstances OnePerType = new CountedInstances();
 
// simpleA data constructormember
private T _t;
public GenTest(T t)
{
_t = t;
}
}
 
// aDefault classconstructor
public classGenericTest(T CountedInstancest)
{
public_t GenTest(T= t);
//Static variable - this will be incremented once per instance
b = a;}
public static int Counter;
}
 
// a class
//simple constructor
publicclass CountedInstances()
{
{
// Static variable - this will be incremented once per instance
//increase counter by one during object instantiation
public static int CountedInstances.Counter++;
 
}
//simple Default constructor
public CountedInstances()
{
//increase Increase counter by one during object instantiation
public static int CountedInstances.Counter++;
}
}
 
public class GenTest<T>GenericExample
// 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);
// mainMain code entry point
GenTest<int> g111 = new GenTest<int>(111);
// atAt the end of execution, CountedInstances.Counter = 2
GenTest<double> g2 = new GenTest<double>(1.0);
GenTest GenericTest<int> g1 = new GenTestGenericTest<int>(1);
GenTest GenericTest<int> g11 = new GenTestGenericTest<int>(11);
GenTest GenericTest<int> g111 = new GenTestGenericTest<int>(111);
GenTest GenericTest<double> g2 = new GenTestGenericTest<double>(1.0);
}
}
</syntaxhighlight>