Q BgQuestion:

Guru
Karma Points: 2,026
Respect (90%):
posted by  Duke12 on 7/16/2008 2:19:02 AM  |  status: Live  

write a template class c++...I have code just need to fix an error with pow

Course Textbook Chapter Problem
N/A N/A N/A N/A
Question Details:

Write a template class that represents a point. The data members should consist of an x coordinate and a y coordinate. The coordinates should be of type int or float depending on how the class is instantiated. The class should contain the following methods:

  • a default constructor that sets both coordinates to 0
  • a constructor that accepts the initial values of the coordinates as parameters
  • get and set methods for both x and y
  • a print method that displays the coordinate pair in parentheses
  • a distance method that accepts another point as a parameter and returns the distance between the two points as a float

You can read about the distance formula at http://www.purplemath.com/modules/distform.htm

You may be interested in using the sqrt function that computes the square root of it argument. Here is code to test it also:
#include <iostream>
#include "point.h"

using namespace std;

int main(){
    
    //test point template with int
    point<int> a(1,1);
    cout << "a is a ";
    a.print();
    cout << endl;

    point<int> b;
    cout << "b is a ";
    b.print();
    cout << endl;

    cout << "The distance between a and b is ";
    cout << a.distance(b) << endl << endl;


    //test point template with float
    point<float> c(6.5,4.25);
    cout << "c is a ";
    c.print();
    cout << endl;

    point<float> d(0.5,-3.75);
    cout << "d is a ";
    d.print();
    cout << endl;

    cout << "The distance between c and d is ";
    cout << c.distance(d) << endl << endl;

}

Sample Output:
     a is a point(1, 1)
b is a point(0, 0)
The distance between a and b is 1.41421

c is a point(6.5, 4.25)
d is a point(0.5, -3.75)
The distance between c and d is 10

I take the time to answer your question. Please take the time to rate it.
Bonus Point Alert! Earn +4 additional karma points for helping this annual member.

AAnswers:

Answer Question
Guru
Karma Points: 2,214
posted by BLcK on 7/16/2008 2:40:26 AM  |  status: Live
Asker's Rating: Lifesaver   
Duke12's comment:
"thanks...but getting an error on pow"
Response Details:
/*Here is a working example point.h template class.*/

#include <cmath>

template <class Type>
class point
{
public:
    point(){myX = 0 ; myY = 0;}
    point(Type x, Type y){ myX = x; myY = y;}
    void getX(){return myX;}
    void getY(){return myY;}
    void setX(Type x){myX = x;}
    void setY(Type y){myY = y;}
    void print(){cout << "x = " << myX << " y = " << myY;}
    float distance(point p2)
    {
        return ( sqrt( ( pow((p2.myX - myX),2) + pow((p2.myY - myY),2) ) )  );
    }
private:
    Type myX, myY;
};

cout << "Ex nihilio nihil fit" << endl; //C++
System.out.println("Tempus fugit"); //Java
msg: .ASCII "Noli me vocare, ego te vocabo\x00"   //ASM
main:    STRO   msg,d
            .END
            ZZ
Guru
Karma Points: 2,026
posted by Duke12 on 7/16/2008 12:18:19 PM  |  status: Live
Asker's Rating: N/A-Posted by Person Asking Question   
Response Details:
thanks...but i am getting 2 pow errors:

: error C2668: 'pow' : ambiguous call to overloaded function
: could be 'long double pow(long double,int)'
: or       'float pow(float,int)'
: or       'double pow(double,int)'


The code I wrote from above:

#include <cmath>
#ifndef POINT_H
#define POINT_H

template <class T>

class point
{
private:
    T xCord, yCord;

public:
    point()
    {
        xCord=0;
        yCord=0;
    }

    point(T x, T y)
    {
        xCord=x;
        yCord=y;
    }

     void getX()
     {
         return xCord;
     }

     void getY()
     {
         return yCord;
     }

     void setX(T x)
     {
         xCord=x;
     }

     void setY(T y)
     {
         yCord=y;
     }
    
     void print()
     {
         cout << "x = " << xCord << "y = " << yCord;
     }

     float distance(point p2)
     {
         return (sqrt((pow((p2.xCord - xCord),2) + pow((p2.yCord - yCord),2))));
     }

};
#endif

I take the time to answer your question. Please take the time to rate it.
Apprentice
Karma Points: 116
posted by APPOLLO on 7/16/2008 2:06:06 PM  |  status: Live
Asker's Rating: Helpful   
Duke12's comment:
"thanks....that is what I changed it to already"
Response Details:
You can correct the above code like this:

 
float distance(point p2)
     {
         float Xvalue=  p2.xCord - xCord;
         float Yvalue= p2.yCord - yCord; 
          return  sqrt(Xvalue*Xvalue + Yvalue*Yvalue);
     }

Answer Question
Ask New Question

Join Cramster's Community

Cramster.com brings together students, educators and subject enthusiasts in an online study community. With around-the-clock expert help and a community of over 100,000 knowledgeable members, you can find the help you need, whenever you need it. Join for free today » How Cramster is different than tutoring »