Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Using template convert rectangular coordinate to polar using single argument constructor in polar

+3 votes
asked Sep 6, 2019 by The Biz Tv Productions (180 points)
//Help to debug the program it shows error with single arument constructor in class polar

template <class T>
class rectangular

{

public:

T xco,yco;

rectangular():xco(0),yco(0){}

rectangular(T x,T y):xco(x),yco(y){}

void display_rect()

{
cout<<"rectangular coordintaes:"<<xco<<","<<yco<<endl;

}

};

template <class S>

class polar

{

S radius,angle;

public:

polar();radius(0),angle(0){}

polar(rectangular r)

{

radius=sqrt(r.xco*r.xco+r.yco*r.yco);

angle=atan(r.yco/r.xco);

}

void display_polar()

{

cout<<"polar coordinates is"<<radius<<","<<angle<<endl;

}

};

void main()

{

polar<double> p;

rectangular <double> r(4.71,30.56);

p=r;

p.display_rect();

cout<<"After conversion"<<endl;

p.display_polar()

}

1 Answer

0 votes
answered Aug 22, 2025 by Jerry Jeremiah (2,040 points)

You have:

polar();radius(0),angle(0){}

but it should really be:

polar():radius(0),angle(0){}

And then after fixing that you have:

polar(rectangular r)

Which should really be:

polar(rectangular<S> r)

And then after that you have:

void main()

Which should really be:

int main()

And then after that you have:

p.display_rect();

Which should be:

r.display_rect();

And then after that you have:

p.display_polar()

Which should really be:

p.display_polar();

And then after fixing all the typos you have:

p=r;

Which will never work because rectangular and polar are not related to each other so you can't assign them.

You do have a constructor, but you constructed p using the default constructor.  Instead, you need to delete:

polar<double> p;

And replace:

p=r;

With:

polar<double> p(r);

And then finally you need to include the header for cout, endl, sqrt and atan:

#include <cmath>
using std::sqrt;
using std::atan;
#include <iostream>
using std::cout;
using std::endl;

You really shouldn't be using std::endl - it flushes the output queue which slows down the whole program.  You probably wanted to use "\n" instead of endl.

Here is a running version: https://onlinegdb.com/eTYzZe8OR

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...