Templates
Class Templates
Class templates let a class store or return values without hard-coding one value type.
Class Templates
class_templates.cpp
#include <iostream>
#include <string>
template <typename T>
class Box {
private:
T value;
public:
Box(T initial) {
value = initial;
}
T get() const {
return value;
}
};
int main() {
int count = ;
std::string label = ;
Box<int> countBox(count);
Box<std::string> labelBox(label);
std::cout << "count=" << countBox.get() << std::endl;
std::cout << "label=" << labelBox.get() << std::endl;
return 0;
}
#include <iostream>
#include <string>
template <typename T>
class Box {
private:
T value;
public:
Box(T initial) {
value = initial;
}
T get() const {
return value;
}
};
int main() {
int count = ;
std::string label = ;
Box<int> countBox(count);
Box<std::string> labelBox(label);
std::cout << "count=" << countBox.get() << std::endl;
std::cout << "label=" << labelBox.get() << std::endl;
return 0;
}
#include <iostream>
#include <string>
template <typename T>
class Box {
private:
T value;
public:
Box(T initial) {
value = initial;
}
T get() const {
return value;
}
};
int main() {
int count = ;
std::string label = ;
Box<int> countBox(count);
Box<std::string> labelBox(label);
std::cout << "count=" << countBox.get() << std::endl;
std::cout << "label=" << labelBox.get() << std::endl;
return 0;
}
#include <iostream>
#include <string>
template <typename T>
class Box {
private:
T value;
public:
Box(T initial) {
value = initial;
}
T get() const {
return value;
}
};
int main() {
int count = ;
std::string label = ;
Box<int> countBox(count);
Box<std::string> labelBox(label);
std::cout << "count=" << countBox.get() << std::endl;
std::cout << "label=" << labelBox.get() << std::endl;
return 0;
}
#include <iostream>
#include <string>
template <typename T>
class Box {
private:
T value;
public:
Box(T initial) {
value = initial;
}
T get() const {
return value;
}
};
int main() {
int count = ;
std::string label = ;
Box<int> countBox(count);
Box<std::string> labelBox(label);
std::cout << "count=" << countBox.get() << std::endl;
std::cout << "label=" << labelBox.get() << std::endl;
return 0;
}
#include <iostream>
#include <string>
template <typename T>
class Box {
private:
T value;
public:
Box(T initial) {
value = initial;
}
T get() const {
return value;
}
};
int main() {
int count = ;
std::string label = ;
Box<int> countBox(count);
Box<std::string> labelBox(label);
std::cout << "count=" << countBox.get() << std::endl;
std::cout << "label=" << labelBox.get() << std::endl;
return 0;
}
#include <iostream>
#include <string>
template <typename T>
class Box {
private:
T value;
public:
Box(T initial) {
value = initial;
}
T get() const {
return value;
}
};
int main() {
int count = ;
std::string label = ;
Box<int> countBox(count);
Box<std::string> labelBox(label);
std::cout << "count=" << countBox.get() << std::endl;
std::cout << "label=" << labelBox.get() << std::endl;
return 0;
}
#include <iostream>
#include <string>
template <typename T>
class Box {
private:
T value;
public:
Box(T initial) {
value = initial;
}
T get() const {
return value;
}
};
int main() {
int count = ;
std::string label = ;
Box<int> countBox(count);
Box<std::string> labelBox(label);
std::cout << "count=" << countBox.get() << std::endl;
std::cout << "label=" << labelBox.get() << std::endl;
return 0;
}
#include <iostream>
#include <string>
template <typename T>
class Box {
private:
T value;
public:
Box(T initial) {
value = initial;
}
T get() const {
return value;
}
};
int main() {
int count = ;
std::string label = ;
Box<int> countBox(count);
Box<std::string> labelBox(label);
std::cout << "count=" << countBox.get() << std::endl;
std::cout << "label=" << labelBox.get() << std::endl;
return 0;
}
class template
A class template defines a family of classes, such as `Box<int>` and `Box<std::string>`.