U E D R , A S I H C RSS

5인용C++스터디/템플릿

예제 1

~cpp 
#include <iostream>
using namespace std;

int absi(int value)
{
        if(value < 0)
                return -value;
        else
                return value;
}

double absd(double value)
{
        if(value < 0)
                return -value;
        else
                return value;
}

float absf(float value)
{
        if(value < 0)
                return -value;
        else
                return value;
}

void main()
{
	cout << "absi(-10) = " << absi(-10) << endl;
	cout << "absd(-10.0) = " << absd(-10.0) << endl;
	cout << "absf(-10.0f) = " << absf(-10.0f) << endl;
}

예제 2

~cpp 
#include <iostream>
using namespace std;

int abs(int value)
{
        if(value < 0)
                return -value;
        else
                return value;
}

double abs(double value)
{
        if(value < 0)
                return -value;
        else
                return value;
}

float abs(float value)
{
        if(value < 0)
                return -value;
        else
                return value;
}

void main()
{
	cout << "abs(-10) = " << abs(10) << endl;
	cout << "abs(-10.0) = " << abs(-10.0) << endl;
	cout << "abs(-10.0f) = " << abs(-10.0f) << endl;
}

예제 3

~cpp 
template<typename T>
T abs(T value)
{
        if(value < 0)
                return -value;
        else
                return value;
}

예제 4

~cpp 
#include <iostream>
using namespace std;

class Array5
{
private:
	int Items[5];
public:
	void SetItem(int index, int item);
	int GetItem(int index);
};

void Array5::SetItem(int index, int item)
{
	if(index >= 0 && index <= 4)
		Items[index] = item;
}

int Array5::GetItem(int index)
{
	if(index >= 0 && index <= 4)
		return Items[index];
	else
		return 0;
}

void main()
{
	Array5 a;
	a.SetItem(0, 10);
	a.SetItem(1, 20);
	cout << a.GetItem(0) << endl;
	cout << a.GetItem(1) << endl;
}

예제 5

~cpp 
#include <iostream>
using namespace std;

template<typename T>
class Array5
{
private:
	T Items[5];
public:
	void SetItem(int index, T item);
	T GetItem(int index);
};

template<typename T>
void Array5<T>::SetItem(int index, T item)
{
	if(index >= 0 && index <= 4)
		Items[index] = item;
}

template<typename T>
T Array5<T>::GetItem(int index)
{
	if(index >= 0 && index <= 4)
		return Items[index];
	else
		return 0;
}

void main()
{
	Array5<int> a;
	a.SetItem(0, 10);
	a.SetItem(1, 20);
	cout << a.GetItem(0) << endl;
	cout << a.GetItem(1) << endl;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:19
Processing time 0.0166 sec