template <typename T>
T min(T a, T b);
template <typename T>
T min(T a, T b) {
return a < b ? a : b;
}
#include <iostream>
int main() {
std::cout << min(1, 2) << "\n";
}
min.h
min.cpp
main.cpp
template <typename T>
T min(T a, T b) {
return a < b ? a : b;
}
#include <iostream>
int main() {
std::cout << min(1, 2) << "\n";
}
min.h
main.cpp
template <typename T>
T min(T a, T b);
template <typename T>
T min(T a, T b) {
return a < b ? a : b;
}
template int min<int>(int, int);
template double min<double>(double, double);
#include <iostream>
int main() {
std::cout << min(1, 2) << "\n";
std::cout << min(1.0, 2.0) << "\n";
}
min.h
min.cpp
main.cpp
#include <vector>
template <typename T>
class Stack {
public:
Stack() {}
void pop();
void push(const T& i);
private:
std::vector<T> items_;
}
#include "stack.tpp"
template <typename T>
void Stack<T>::pop() {
items_.pop_back();
}
template <typename T>
void Stack<T>::push(const T& i) {
items_.push_back(i);
}
int main() {
Stack<int> s;
}
Stack.h
Stack.tpp
main.cpp
#include <vector>
template <typename T>
class Stack {
public:
Stack() {}
void pop();
void push(const T& i);
private:
std::vector<T> items_;
}
#include "stack.tpp"
template <typename T>
void Stack<T>::pop() {
items_.pop_back();
}
template <typename T>
void Stack<T>::push(const T& i) {
items_.push_back(i);
}
int main() {
Stack<int> s;
s.push(5);
}
Stack.h
Stack.tpp
main.cpp
#include <vector>
template <typename T>
class Stack {
public:
Stack();
~Stack();
void push(T&);
void pop();
T& top();
const T& top() const;
static int numStacks_;
private:
std::vector<T> stack_;
};
template <typename T>
int Stack<T>::numStacks_ = 0;
template <typename T>
Stack<T>::Stack() { numStacks_++; }
template <typename T>
Stack<T>:: ~Stack() { numStacks_--; }
#include <iostream>
#include "lectures/week7/stack.h"
int main() {
Stack<float> fs;
Stack<int> is1, is2, is3;
std::cout << Stack<float>::numStacks_ << "\n";
std::cout << Stack<int>::numStacks_ << "\n";
}
#include <vector>
template <typename T>
class Stack {
public:
Stack();
~Stack();
void push(T&);
void pop();
friend std::ostream& operator<<(std::ostream& os, const Stack& s);
private:
std::vector<T> stack_;
};
template <typename T>
void push(T& t) {
stack_.push_back(t);
}
template <typename T>
std::ostream& operator<<(std::ostream& os, const Stack<T>& s) {
std::cout << "My top item is " << s.stack_.back() << \n";
}
#include <iostream>
#include <string>
#include "lectures/week7/stack.h"
int main() {
Stack<std::string> ss;
ss.push("Hello");
std::cout << ss << "\n":
Stack<int> is;
is.push(5);
std::cout << is << "\n":
}
#include <vector>
template <typename T, typename CONT = std::vector<T>>
class Stack {
public:
Stack();
~Stack();
void push(T&);
void pop();
T& top();
const T& top() const;
static int numStacks_;
private:
CONT stack_;
};
template <typename T, typename CONT>
int Stack<T, CONT>::numStacks_ = 0;
template <typename T, typename CONT>
Stack<T, CONT>::Stack() { numStacks_++; }
template <typename T, typename CONT>
Stack<T, CONT>:: ~Stack() { numStacks_--; }
#include <iostream>
#include "lectures/week7/stack.h"
int main() {
Stack<float> fs;
Stack<int> is1, is2, is3;
std::cout << Stack<float>::numStacks_ << "\n";
std::cout << Stack<int>::numStacks_ << "\n";
}