9. Template Classes and Functions in C++

9. Template Classes and Functions in C++

Postby Cuchulainn » Wed Jan 21, 2009 1:45 pm

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
User avatar
Cuchulainn
 
Posts: 676
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Implementation

Postby ImplicitScheme » Mon Dec 20, 2010 12:42 am

Here is a possible implementation





Code: Select all
 



#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 ;



} ;

ImplicitScheme
 
Posts: 6
Joined: Thu Nov 04, 2010 7:01 pm


Return to C++ Certification and Examinations: Intermediate Level

Who is online

Users browsing this forum: No registered users and 2 guests

cron