|
View previous topic :: View next topic |
Author |
9. Template Classes and Functions in C++ |
ImplicitScheme
Joined: 04 Nov 2010
Posts: 6
|
Posted: Sun Dec 19, 2010 11:42 pm Post subject: Implementation |
|
|
Here is a possible implementation
Code: |
#ifndef POINT_H
#define POINT_h
#include<cmath>
#include <iostream>
using std::cin;
using std::cout;
using std::ostream;
using std::endl ;
#include<iomanip>
template <class TFirst, class TSecond = int>
class Point
{
private:
// The two coordinates
TFirst m_first;
TSecond m_second;
public:
// Constructors & destructor
Point(){} // Default constructor
Point(TFirst first, TSecond second): m_first(first) , m_second(second){} // Constructor with coordinates
Point(const Point<TFirst, TSecond>& source); // Copy constructor
void First(const TFirst& val); // Set first coordinate
void Second(const TSecond& val); // Set second coordinates
TFirst First() const; // Get first coordinates
TSecond Second() const; // Get second coordinate
double Distance(const Point<TFirst, TSecond>& p);
Point<TFirst, TSecond>& operator = (const Point<TFirst, TSecond>& source);
friend ostream& operator<< (ostream& os, const Point<TFirst, TSecond>& p);
};
#endif
template<class TFirst, class TSecond> Point<TFirst,TSecond>::Point(const Point<TFirst, TSecond>& source)
{
m_first = source.First() ;
m_second = source.Second() ;
} ;
template<class TFirst, class TSecond> void Point<TFirst,TSecond>::First(const TFirst& val)
{
m_first = val ;
} ;
template<class TFirst, class TSecond> void Point<TFirst,TSecond>::Second(const TSecond& val)
{
m_second = val ;
} ;
template<class TFirst, class TSecond> TFirst Point<TFirst,TSecond>::First() const
{
return m_first ;
} ;
template< class TFirst, class TSecond> TSecond Point<TFirst,TSecond>:: Second() const
{
return m_second ;
} ;
template<class TFirst, class TSecond> double Point<TFirst, TSecond>::Distance(const Point<TFirst, TSecond>& p)
{
double Dist ;
Dist = (p.First()-m_first)* (p.First()-m_first)+(p.Second()-m_second)*(p.Second()-m_second) ;
return sqrt(Dist) ;
};
template<class TFirst, class TSecond> Point<TFirst,TSecond> & Point<TFirst, TSecond>::operator=(const Point<TFirst, TSecond>& source)
{
if(this == &source)
return *this ;
m_first = source.First() ;
m_second = source.Second() ;
return *this ;
} ;
template<class TFirst, class TSecond> ostream & operator<<(ostream& os, const Point<TFirst, TSecond>& p)
{output << "(" <<m_first<< ","<< m_second<< ")" ;
return output ;
} ;
|
|
|
Back to top |
|
|
Cuchulainn
Joined: 18 Dec 2006
Posts: 607
Location: Amsterdam, the Netherlands
|
Posted: Wed Jan 21, 2009 12:45 pm Post subject: 9. Template Classes and Functions in C++ |
|
|
Implement the code for this class:
// Point.hpp
//
// Generic point class. The first coordinate is of one
// type and the second coordinate is of the second type.
//
// (C) Datasim Education BV 2006-2009
#ifndef Point_hpp
#define Point_hpp
#include <iostream>
using namespace std;
template <class TFirst, class TSecond = int>
class Point
{
private:
// The two coordinates
TFirst m_first;
TSecond m_second;
public:
// Constructors & destructor
Point(); // Default constructor
Point(TFirst first, TSecond second); // Constructor with coordinates
Point(const Point<TFirst, TSecond>& source); // Copy constructor
virtual ~Point(); // Destructor
// Selectors
TFirst First() const; // Get first coordinates
TSecond Second() const; // Get second coordinate
// Modifiers
void First(const TFirst& val); // Set first coordinate
void Second(const TSecond& val); // Set second coordinates
// Functions
double Distance(const Point<TFirst, TSecond>& p) const; // Calculate distance
// Assignment operator
Point<TFirst, TSecond>& operator = (const Point<TFirst, TSecond>& source);
// Print
// ONLY IN MC++ 6.0, higher versions uncommented
// template <class TFirst, class TSecond>
friend ostream& operator << (ostream& os, const Point<TFirst, TSecond>& p);
};
#endif |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|