A little question on template parent/child class...

This is a discussion on A little question on template parent/child class... within the C++ Programming forums, part of the General Programming Boards category; Suppose I want a user to enter a 2D point (x,y). How would I write a template class, in which ...

  1. #1
    JamesW
    Guest

    A little question on template parent/child class...

    Suppose I want a user to enter a 2D point (x,y). How would I write a template class, in which a user can enter a point of any types?

    Example: a user enter..
    Enter x: 3
    Enter y: 5

    or

    Enter x: 3.5
    Enter y: 5.6

    This is what I have so far...

    Code:
    class myPoint{
    public:
    myPoint(int a, int b) : x(a), y(b) {}
    ~myPoint() {}
    protected:
    int x, y;
    
    }
    
    template <class T> myPoint<T>
    {
    //something???
    }
    Any help?

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    probably not a great use of templating but anyway here you go...
    Code:
    template<typename T>
      class Point
       {
           public:
               Point( T x, T y) : x_(x),y_(y) {/*needs better error checking*/}
          private:
               T x_;
               T y_;
        };
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Weird errors.
    By Desolation in forum C++ Programming
    Replies: 20
    Last Post: 05-09-2007, 01:10 PM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21