13 Replies - 1165 Views - Last Post: 08 October 2009 - 08:06 PM Rate Topic: -----

#1 deborah_morales1990  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 08-April 09

Calling derived class from main function

Post icon  Posted 04 October 2009 - 07:49 PM

#include <iostream>

using namespace std;
//parent
class Coordinate{
	private:
		double value1, value2;
	public:
	//constructor
		Coordinate(double, double);
		double get_distance(Coordinate);
		virtual double convert(Coordinate) = 0; 
};
//child 1
class Polar_Coordinate: public Coordinate{
	public:
		Polar_Coordinate(double x, double theta);
		
};
//child 2
class Cartesian_Coordinate: public Coordinate{
	public:
		Cartesian_Coordinate(double x, double y);
};

void main(){
	Polar_Coordinate p;// Why doesn't this line work?
	//or why doesn't this line work
	//Coordinate c;	
}


I dont undrstand why Polar_Coordinate p; doesn't work or Coordinate c;
i get this error:

error C2512: 'Polar_Coordinate' : no appropriate default constructor available
error C2259: 'Polar_Coordinate' : cannot instantiate abstract class

My program is to convert polar to certesian and vice versa. This is the code that my professor gave to me to use. Right now Im just trying to get used to using inheritance and polymorphism.

Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Calling derived class from main function

#2 jwwicks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 162
  • Joined: 31-July 08

Re: Calling derived class from main function

Posted 04 October 2009 - 10:57 PM

Hello Deborah,

View Postdeborah_morales1990, on 4 Oct, 2009 - 06:49 PM, said:

#include <iostream>

using namespace std;
//parent
class Coordinate{
	private:
		double value1, value2;
	public:
	//constructor
		Coordinate(double, double);
		double get_distance(Coordinate);
		virtual double convert(Coordinate) = 0; 
};
//child 1
class Polar_Coordinate: public Coordinate{
	public:
		Polar_Coordinate(double x, double theta);
		
};
//child 2
class Cartesian_Coordinate: public Coordinate{
	public:
		Cartesian_Coordinate(double x, double y);
};

void main(){
	Polar_Coordinate p;// Why doesn't this line work?
	//or why doesn't this line work
	//Coordinate c;	
}


I dont undrstand why Polar_Coordinate p; doesn't work or Coordinate c;
i get this error:

error C2512: 'Polar_Coordinate' : no appropriate default constructor available
error C2259: 'Polar_Coordinate' : cannot instantiate abstract class

Thank you!


The compiler is telling you that you don't have a default constructor setup for that class and if you look at the code there is no default constructor for these classes but that's not the only problem with the code. I think you're first confusing declarations, definitions and invocations/instantiations.

The first problem is that all the code does is DECLARE Coordinate, Cartesian_Coordinate and Polar_Coordinate. There is no DEFINITION so you can't INVOKE them. Let's look at the classes piece by piece:
class Coordinate{
	private:
		double value1, value2;
	public:
	//constructor
		Coordinate(double, double); // Constructor taking two doubles as arguments
		double get_distance(Coordinate);
		virtual double convert(Coordinate) = 0; //Pure virtual method, MUST be implemented
		                                        // in derived classes
};



This class has a pure virtual function meaning you can NEVER DECLARE a variable of type Coordinate. This class is purely meant as an abstract base for other classes. You still have to DEFINE the constructor and the method get_distance;

Coordinate::Coordinate(double x, double y){ value1 = x; value2 = y; }
double Coordinate::get_distance(Coordinate c){
 double ret_val = 0;
 // Implement some distance calculation code here
 // This implementation will work for x y Cartesian coordinates but not Polar calculations
 // ret_val =  sqrt( (|x2-x1|)^2+(|y2-y1|)^2 )
 return ret_val;
}



Next you should probably DEFINE Cartesian_Coordinate.

Cartesian_Coordinate::Cartesian_Coordinate(double x, double y):Coordinate(x,y){}
double Cartesian_Coordinate::convert(Coordinate c){
 double ret_val = 0;
 // ret_val = Convert the coordinates value1 and value2 to a single double value
 return ret_val; 
}



Next you'll need to do the same for Polar_Coordinate and you'll also need to override the distance method...

Then you can INVOKE/INSTANTIATE and DECLARE variables of types Polar_Coordinate and Cartesian_Coordinate.

Hope that helps...

Jw
Was This Post Helpful? 1
  • +
  • -

#3 deborah_morales1990  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 08-April 09

Re: Calling derived class from main function

Posted 06 October 2009 - 02:09 PM

Hello Jw, thank you for reply. I made header files, and wrote what you suggested. But now I don't understand what you mean by overriding the distance method? here is my code:
cpp:
#include <iostream>
#include "main.h"

using namespace std;

void main(){
	//Polar_Coordinate pc;
	
}


main.h
#ifndef MAIN_H
#define MAIN_H

class Coordinate{
	private:
		double value1, value2;
	public:
		Coordinate(double, double);
		double get_distance(Coordinate);
		virtual double convert(Coordinate) = 0;
};
//child 1
class Polar_Coordinate: public Coordinate{
	public:
		Polar_Coordinate(double x, double theta);
};
//child 2
class Cartesian_Coordinate: public Coordinate{
	public:
		Cartesian_Coordinate(double, double);
};

#endif


Coordinate.h
#ifndef COORDINATE_H
#define COORDINATE_H
#include "main.h"

		Coordinate::Coordinate(double x, double y){
			value1 = x; 
			value2 = y; 
		}
		//by doing :: we are defining get_distance
		double Coordinate::get_distance(Coordinate c){
			double ret_val = 0;
			//code for distance calculation;
			return ret_val;
		}
		

#endif


Polar.h
#ifndef  POLAR_H
#define POLAR_H
#include "main.h"
Polar_Coordinate::Polar_Coordinate(double x, double y):Coordinate(x,y){};
	double Polar_Coordinate::convert(Coordinate c){
		double ret_val;

		return ret_val;
}

#endif


Cartesian.h
#ifndef CARTESIAN_H
#define CARTESIAN_H
#include "main.h"
	Cartesian_Coordinate::Cartesian_Coordinate(double x, double y):Coordinate(x,y){}
	double Cartesian_Coordinate::convert(Coordinate c){
		double ret_val = 0;

		return ret_val;
	}


#endif

Was This Post Helpful? 0
  • +
  • -

#4 jwwicks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 162
  • Joined: 31-July 08

Re: Calling derived class from main function

Posted 07 October 2009 - 12:15 AM

Hello Deborah,

View Postdeborah_morales1990, on 6 Oct, 2009 - 01:09 PM, said:

Hello Jw, thank you for reply. I made header files, and wrote what you suggested. But now I don't understand what you mean by overriding the distance method? here is my code:
cpp:


On second look, you might not need the override since get_distance isn't declared as virtual. All the Class specific work seems to be placed in convert method. What are the requirements for the assignment??

I'd change the layout of your code somewhat. The reason being that traditionally header files contain definitions and the implementation is stored in cpp files. So the layout would look like this...

#include <iostream>
#include <iomanip>
#include "Polar_Coordinate.h"
#include "Cartesian_Coordinate.h"

int main( int argc, char* argv[] )
{
	int ret_val = 0;

	Cartesian_Coordinate* cc = new Cartesian_Coordinate(1.0, 1.0);
	Polar_Coordinate* pc = new Polar_Coordinate(1.0, 60.0);

	return ret_val;
}



Coordinate.h
#ifndef COORDINATE_H
#define COORDINATE_H

class Coordinate{
	private:
		double value1, value2;
	public:
		Coordinate(double, double);
		double get_distance(Coordinate*);
		virtual double convert(Coordinate*) = 0;
};
#endif



Coordinate.cpp
#include "Coordinate.h"

Coordinate::Coordinate(double x, double y){
	value1 = x; 
	value2 = y; 
}

//by doing :: we are defining get_distance
double Coordinate::get_distance(Coordinate* c){
	double ret_val = 0;
	//code for distance calculation;
	return ret_val;
}



Polar_Coordinate.h
#include "Coordinate.h"

#ifndef POLAR_COORDINATE_H
#define POLAR_COORDINATE_H
class Polar_Coordinate: public Coordinate{
	public:
		Polar_Coordinate(double x, double theta);
		double convert(Coordinate* c);
};
#endif



Polar.cpp
#include "Polar_Coordinate.h"

Polar_Coordinate::Polar_Coordinate(double x, double y):Coordinate(x,y){};

double Polar_Coordinate::convert(Coordinate* c){
		double ret_val;

		return ret_val;
}



Cartesian_Coordinate.h
#include "Coordinate.h"

#ifndef CARTESIAN_COORDINATE_H
#define CARTESIAN_COORDINATE_H
class Cartesian_Coordinate: public Coordinate{
	public:
		Cartesian_Coordinate(double, double);
		double convert(Coordinate* c);
};
#endif




Cartesian.cpp
#include "Cartesian_Coordinate.h"

Cartesian_Coordinate::Cartesian_Coordinate(double x, double y):Coordinate(x,y){}

double Cartesian_Coordinate::convert(Coordinate* c){
	double ret_val = 0;

	return ret_val;
}



I didn't catch this before but you'll notice I changed the base class somewhat. As I mentioned earlier you can't instantiate a Coordinate but you can use a pointer to Coordinate. I'm not sure if the base class is an assignment or if you're just playing around but the parameters have to be pointers since Coordinate is an Abstract base with a pure virtual function.

Jw

This post has been edited by jwwicks: 07 October 2009 - 12:20 AM

Was This Post Helpful? 1
  • +
  • -

#5 deborah_morales1990  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 08-April 09

Re: Calling derived class from main function

Posted 07 October 2009 - 01:06 PM

Thank you Jw, I will look over what you wrote to me but first I would like to respond to your Question about the requirement for this assignment:


This assignment is going to give you experience writing a program with classes and inheritance. Your program will build on the program you wrote for Assignment #3. The end functionality will be similar to the functionality from Assignment #3, but the
way in which you organize your code will change. First, create a Coordinate class, which will be the parent of your Polar and Cartesian coordinate classes. Here is the definition for the classes:
class Coordinate {
private:
float value1, value2;
public:
Coordinate(float, float);
float get_distance(Coordinate);
virtual float convert(Coordinate) = 0;
};
class Polar_Coordinate : public Coordinate {
public:
Polar_Coordinate(float, float);
};
class Cartesian_Coordinate : public Coordinate {
public:
Cartesian_Coordinate(float, float);
};


Feel free to add more functions as you see necessary, but no additional data members can be added. The variables “value1” and “value2” in the Coordinate class should be used as (x, y) for Cartesian coordinates and (r, θ) for Polar coordinates.
In addition, when you use your classes, you should create instances of the Coordinate class and let polymorphism be used to call the proper function in the sub‐class.
Other than that, the functionality for your program should be the same as it was before. If you didn’t have your Assignment #3 working completely, come talk to me to get it
working so that you have a fully‐functional program before beginning Assignment #4.


-That is the assignment I already have my assignment 3 which was converting polar to cartesian and vice versa but by using classes. not with inheritance and virtual. here is my code:
#include <iostream>
#include <math.h>
#include <iomanip>
#include <limits>
#define PI 3.141592654

using namespace std;

class Polar_Choice{
	private:
		double radius, theta, x, y, distance;	
	public:
		void enterRandT() {
			cout << "Please enter the radius: ";
			cin >> radius;
			cout << "Please enter the theta: ";
			cin >> theta;
		}
		double convertPtoC(){
			double theta2;
			theta2 = theta*(PI/180);
			x = radius *(cos(theta2));
			y = radius *(sin(theta2));
			cout<<"\nThe Cartesian Coordinate is ";
			cout << "(" << x << ", " << setprecision(3)<<y << ")" <<endl;
			return 0;
		}
		double distanceBWpoints(double rad, double t, double i){
			//x and y already in form //need to make rad and t in form
			double x2,y2;
			double t2;
			double theta2;
			
			if(i==0){
				theta2 = theta * (PI/180);
				x = radius * (cos(theta2));
				y = radius * (sin(theta2));
			}
			t2 = t * (PI/180);
			x2 = rad*(cos(t2));
			y2 = rad*(sin(t2));
			//cout<< x2<<endl;
			//cout <<y2<<endl;
			distance =(x2 - x)*(x2 - x) + ((y2 - y)*(y2 - y));
			distance = sqrt(distance);
			cout <<"\nThe distance between the Polar coordinates (" << radius << ", " << theta << ") and (";
			cout << rad << ", " << t << ") is " << setprecision(3) << distance<<endl;
			return 0;
		}
};

class Cartesian_Choice{
	private:
		double x,y, distance;
	public:
		void enterXandY(){
			cout << "\nPlease enter the x: ";
			cin >> x;
			cout <<"Please enter the y: ";
			cin >> y;
		}
		double convertCtoP(){
			double x2, y2;
			x2 = (x * x) + (y * y);
			x2 = sqrt(x2);
			//need to convert x and y to degrees
			x = x * (180/PI);
			y = y * (180/PI);
			y2 = atan(y/x);
			//need to convert y to degrees
			y2 = y2 *(180/PI);
			cout<<"\nThe Polar coordinate is (" << setprecision(4)<< x2 << ", " << setprecision(4)<<y2 << ")"<<endl;
			return 0;
		}
		double distanceBWpoints(double x2, double y2, double i){
			if(i==1){
				x = x *(PI/180);
				y = y *(PI/180);
				//cout<<x<<endl;
				//cout<<y<<endl;	
			}
			distance = (x2-x)*(x2-x) + (y2-y)*(y2-y);
			distance = sqrt(distance);
			cout << "\nThe distance between the Cartesion coordinates (";
			cout << x << ", " << y << ") and (" << x2 << ", " << y2 << ") is " << setprecision(4) << distance << endl;
			return 0;
		}

};
void main(){
	Polar_Choice pc;
	Cartesian_Choice cc;
	char polarOrCartesian;
	int choiceNumber = 0;
	int i=0;

	cout << "[p]  Polar Coordinates\n[c]  Cartesian Coordinates" << endl;
	cout << "\nWhat type of coordinates? ";
	cin >> polarOrCartesian;

	while(polarOrCartesian == 'p' || polarOrCartesian == 'P' || polarOrCartesian == 'C' || polarOrCartesian == 'c'){
			if(polarOrCartesian == 'p' || polarOrCartesian == 'P'){
				cout<<endl;
				pc.enterRandT();
				while(choiceNumber != 4 || choiceNumber !=3 ){
					cout << "\n[1]\tConvert to Cartesian coordinate" <<endl;
					cout << "[2]\tDistance between two points" <<endl;
					cout << "[3]\tStart over" <<endl;
					cout << "[4]\tExit\n" <<endl;
					
					cout<<"What would like to do? ";
					cin>>choiceNumber;
					if(choiceNumber == 1){
						pc.convertPtoC();
						i=1;
					}
					if(choiceNumber == 2){
						double radius2, theta2;		
								cout << "\nPlease enter the radius: ";
								cin >> radius2;
								cout << "Please enter theta: ";
								cin >> theta2;
								pc.distanceBWpoints(radius2, theta2, i);		
					}
					if(choiceNumber == 3 || choiceNumber ==4 ){
						break;
					}
				}
			}
			if(polarOrCartesian == 'c' || polarOrCartesian == 'C'){
				cc.enterXandY();
				i=0;
				while(choiceNumber !=4 || choiceNumber !=3){
					cout << "\n[1]\tConvert to Polar coordinate" <<endl;
					cout << "[2]\tDistance between two points" <<endl;
					cout << "[3]\tStart over" <<endl;
					cout << "[4]\tExit\n" <<endl;
					cout<<"What would you like to do? ";
					cin>>choiceNumber;
					
					if(choiceNumber == 1){
						cc.convertCtoP();
						i=1;
					}
					if(choiceNumber == 2){
						double x,y;
						cout<<"\nPlease enter the x: ";
						cin>> x;
						cout<<"Please enter the y: ";
						cin>>y;
						cc.distanceBWpoints(x,y,i);
					}
					if(choiceNumber == 3 || choiceNumber ==4 ){
						break;
					}
				}
			}
			if(choiceNumber == 4){
				break;
			}
			cout<<"\n[p]  Polar Coordinates\n[c]  Cartesian Coordinates\n"<<endl;
			cout << "What type of coordinates? ";
			cin>> polarOrCartesian;
	}
	cout << "\nThank you for using my program!\n";		
}
 


Thank you so much for your help, I would normally ask my professor, but he has been out of town all week and the assignment is due tomorrow.

Thank you agian, I will keeping working on it.
Was This Post Helpful? 0
  • +
  • -

#6 jwwicks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 162
  • Joined: 31-July 08

Re: Calling derived class from main function

Posted 07 October 2009 - 07:22 PM

Hello Deborah,

That's unfortunate since the declarations your instructed to use won't compile. It might have just been an oversight or typo but the parameters to those two methods HAVE to be Coordinate pointers.

class Coordinate {
private:
float value1, value2;
public:
Coordinate(float, float);
float get_distance(Coordinate);
virtual float convert(Coordinate) = 0;
};



If you get the chance you might want to ask about the above code...

Quote

Thank you so much for your help, I would normally ask my professor, but he has been out of town all week and the assignment is due tomorrow.

Thank you agian, I will keeping working on it.


So you have the code for the convert functions and polymorphism will take care of calling the correct version of convert based on the instance of the class making the call.

Jw
Was This Post Helpful? 1
  • +
  • -

#7 deborah_morales1990  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 08-April 09

Re: Calling derived class from main function

Posted 07 October 2009 - 07:29 PM

how do i get into convert if its a pointer? my x and y are floats, but the convert takes a Coordinate*

cartesian.cpp
#include <iostream>
#include "Cartesian_Coordinate.h"
using namespace std;

Cartesian_Coordinate::Cartesian_Coordinate(float x, float y):Coordinate(x,y){
	int num;
	cout<<x<<endl<<y<<endl;
	cout << "\n[1]\tConvert to Polar coordinate" <<endl;
	cout << "[2]\tDistance between two points" <<endl;
	
	cout<<"\nWhat would you like to do? ";
	cin>>num;

	if(num == 1){
		//somehow get into convert ???
	}
};

float Cartesian_Coordinate::convert(Coordinate* c){
	float ret_val = 0;
	return ret_val;
}


here is my main:
#include <iostream>
#include <iomanip>
#include "Polar_Coordinate.h"
#include "Cartesian_Coordinate.h"

using namespace std;

int main(int argc, char* argv[]){
	char polarOrCartesian;
	int ret_val = 0;
	float x, y;
	
	cout << "[p]  Polar Coordinates\n[c]  Cartesian Coordinates" << endl;
	cout << "\nWhat type of coordinates? ";
	cin >> polarOrCartesian;
	
	if(polarOrCartesian == 'p'){
		cout<<"Please enter the radius: ";
		cin>>x;
		cout<<"Please enter the theta: ";
		cin>>y;

		Polar_Coordinate* pc = new Polar_Coordinate(x,y);
	}
	if(polarOrCartesian == 'c'){
		cout<<"Please enter the x: ";
		cin>>x;
		cout<<"Please enter the y: ";
		cin>>y;

		Cartesian_Coordinate* pc = new Cartesian_Coordinate(x,y);
	}

	//Cartesian_Coordinate* cc = new Cartesian_Coordinate(1.0,1.0);
	//Polar_Coordinate* pc = new Polar_Coordinate(1.0, 60.0);
	return ret_val;
}



thank you
Was This Post Helpful? 0
  • +
  • -

#8 jwwicks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 162
  • Joined: 31-July 08

Re: Calling derived class from main function

Posted 07 October 2009 - 08:04 PM

Hello Deborah,

Now having looked over the assignment and your previous code I'd have to say the the base class Coordinate is incorrect. The convert method should have the following signature
Coordinate* convert(Coordinate* c);



The reason being that you can't return a coordinate in a single double/float value. A Polar coordinate is converted to a Cartesian coordinate with x, y. That's two double/float values. Same the other way around.

You'll also have to have some accessor methods to get the value1 and value2 members. Something public that just returns the current value1 etc... I would say these could be in the Coordinate class.

View Postdeborah_morales1990, on 7 Oct, 2009 - 06:29 PM, said:

how do i get into convert if its a pointer? my x and y are floats, but the convert takes a Coordinate*

thank you


Accessing a member through a pointer
Polar_Coordinate* pPolar = new Polar_Coordinate(1.0, 30.0);
Cartesian_Coordinate* pCartesian = new Cartesian_Coordinate(1.0,1.0);
Polar_Coordinate* pPolar2;

double radius = pPolar->getValue1();
double theta = pPolar->getValue2();

pPolar2 = pPolar->convert(pCartesian); //Convert the Cartesian coordinate to Polar




Jw
Was This Post Helpful? 1
  • +
  • -

#9 jhay89  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 02-April 09

Re: Calling derived class from main function

Posted 07 October 2009 - 09:20 PM

Hello Deborah,

I'm in your class.

Thanks JW.

I have a question for you. In the main function why is necessary to have the main function return a value? ie " int argc, char* argv[] " What do the letters signify?
Was This Post Helpful? 0
  • +
  • -

#10 jwwicks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 162
  • Joined: 31-July 08

Re: Calling derived class from main function

Posted 07 October 2009 - 10:15 PM

Hello jhay89,

View Postjhay89, on 7 Oct, 2009 - 08:20 PM, said:

Hello Deborah,

I'm in your class.

Thanks JW.

I have a question for you. In the main function why is necessary to have the main function return a value? ie " int argc, char* argv[] " What do the letters signify?


There are two valid C/C++ main definitions. void main(void) is an anomaly that keeps popping up. Don't use it.

int main(void);
int main(int argc, char* argv[]);



The return is a signal to the operating system that no error has occurred during execution of the program. The arguments to main are used say for instance if you need to pass information from a commandline interface such as DOS or the Linux shell. Calling an executable such as xcopy.exe in DOS with the following on the commandline:

xcopy *.c temp

sets the int argc =3 and argv[0] = "c:\completepathhere\xcopy.exe" argv[1]="*.c" argv[2]="temp"

Jw
Was This Post Helpful? 1
  • +
  • -

#11 jhay89  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 02-April 09

Re: Calling derived class from main function

Posted 07 October 2009 - 10:28 PM

Polar_Coordinate* pPolar = new Polar_Coordinate(1.0, 30.0);
Cartesian_Coordinate* pCartesian = new Cartesian_Coordinate(1.0,1.0);
Polar_Coordinate* pPolar2;

double radius = pPolar->getValue1();
double theta = pPolar->getValue2();

pPolar2 = pPolar->convert(pCartesian); //Convert the Cartesian coordinate to Polar



If possible can you explain this step in more detail? I am a little bit confused on how/what pCartesian is suppose to be.
Was This Post Helpful? 0
  • +
  • -

#12 deborah_morales1990  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 08-April 09

Re: Calling derived class from main function

Posted 07 October 2009 - 10:35 PM

Polar_Coordinate* pPolar = new Polar_Coordinate(1.0, 30.0);
Cartesian_Coordinate* pCartesian = new Cartesian_Coordinate(1.0,1.0);
Polar_Coordinate* pPolar2;

double radius = pPolar->getValue1();
double theta = pPolar->getValue2();

pPolar2 = pPolar->convert(pCartesian); //Convert the Cartesian coordinate to Polar


Did you make getValue1() and getValue2 functions? I tried putting the above code into my code. but the last line gives me error.
here is my code:
#include <iostream>
#include <iomanip>
#include "Polar_Coordinate.h"
#include "Cartesian_Coordinate.h"

using namespace std;

int main(int argc, char* argv[]){
	char polarOrCartesian;
	int ret_val = 0;
	float x, y;
	
	cout << "[p]  Polar Coordinates\n[c]  Cartesian Coordinates" << endl;
	cout << "\nWhat type of coordinates? ";
	cin >> polarOrCartesian;
	
	if(polarOrCartesian == 'p'){
		cout<<"Please enter the radius: ";
		cin>>x;
		cout<<"Please enter the theta: ";
		cin>>y;

		Polar_Coordinate* pPolar = new Polar_Coordinate(x,y);
		Polar_Coordinate* pPolar2;

		float x = pPolar->getValue1();
		float y = pPolar->getValue2();

		pPolar2 = pPolar->convert(pCartesian);
	
	}
	if(polarOrCartesian == 'c'){
		cout<<"Please enter the x: ";
		cin>>x;
		cout<<"Please enter the y: ";
		cin>>y;

		Cartesian_Coordinate* pCartesian = new Cartesian_Coordinate(x,y);
	}
	
	//Coordinate *p
	
	
	//cout<<"HELLO"<<&p<<endl;
	//Polar_Coordinate z;
	//p = &x;


	//Cartesian_Coordinate* cc = new Cartesian_Coordinate(1.0,1.0);
	//Polar_Coordinate* pc = new Polar_Coordinate(1.0, 60.0);
	return ret_val;
}




Polar.cpp

#include <iostream>
#include "Polar_Coordinate.h"
using namespace std;

Polar_Coordinate::Polar_Coordinate(float x, float y):Coordinate(x,y){
	int num;
	cout<<x<<endl<<y<<endl; //now we are inside
		cout << "\n[1]\tConvert to Cartesian coordinate" <<endl;
		cout << "[2]\tDistance between two points" <<endl;

		cout<<"\nWhat would you like to do? ";
		cin>>num;
		if(num == 1){
			//Coordinate = &x;
			//somehow get into convert

		}
		
};
float Polar_Coordinate::convert(Coordinate* c){
		float ret_val = 0;
		return ret_val;
}



Polar_Coordinate.h
#include "Coordinate.h"

#ifndef POLAR_COORDINATE_H
#define POLAR_COORDINATE_H

class Polar_Coordinate: public Coordinate{
public:
	Polar_Coordinate(float x, float y);
	float convert(Coordinate* c);
};

#endif


Coordinate.cpp
#include "Coordinate.h"

#ifndef POLAR_COORDINATE_H
#define POLAR_COORDINATE_H

class Polar_Coordinate: public Coordinate{
public:
	Polar_Coordinate(float x, float y);
	float convert(Coordinate* c);
};

#endif


Coordinate.h
#ifndef COORDINATE_H
#define COORDINATE_H

class Coordinate{
private: 
	float value1, value2;
public:
	Coordinate(float, float);
	float get_distance(Coordinate*);
	
	float getValue1(){
		return value1;
	}
	float getValue2(){
		return value2;
	}
	virtual float convert(Coordinate*) = 0;

};
#endif

Was This Post Helpful? 0
  • +
  • -

#13 jwwicks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 162
  • Joined: 31-July 08

Re: Calling derived class from main function

Posted 07 October 2009 - 11:18 PM

Hello Deborah,

View Postdeborah_morales1990, on 7 Oct, 2009 - 09:35 PM, said:

Polar_Coordinate* pPolar = new Polar_Coordinate(1.0, 30.0);
Cartesian_Coordinate* pCartesian = new Cartesian_Coordinate(1.0,1.0);
Polar_Coordinate* pPolar2;

double radius = pPolar->getValue1();
double theta = pPolar->getValue2();

pPolar2 = pPolar->convert(pCartesian); //Convert the Cartesian coordinate to Polar


Did you make getValue1() and getValue2 functions? I tried putting the above code into my code. but the last line gives me error.
here is my code:


The above code was simply meant to illustrate the use of a pointer to access members of a class. You'll need to create the getValue1() and getValue2() methods in Coordinate.cpp and declare them as members of the class in Coordinate.h

The convert member I believe needs to change it's signature as I said. There's no way that returning a single double/float value makes sense when converting a Polar or Cartesian coordinate. At a minimum you need two doubles. Look at the previous code you wrote, you created x and y or r and theta. So you'll need to change the signature of convert to look like the following

Coordinate.h, Polar_Coordinate.h, Cartesian_Coordinate.h
#ifndef COORDINATE_H
#define COORDINATE_H

class Coordinate{
	private:
		double value1, value2;
	public:
		Coordinate(double, double);
		double get_distance(Coordinate*);
		double getValue1();
		bool setValue1( double );
		double getValue2();
		bool setValue2( double );
		virtual Coordinate* convert(Coordinate*) = 0;
};
#endif



#include "Coordinate.h"

#ifndef CARTESIAN_COORDINATE_H
#define CARTESIAN_COORDINATE_H
class Cartesian_Coordinate: public Coordinate{
	public:
		Cartesian_Coordinate(double, double);
		virtual Coordinate* convert(Coordinate*);
};
#endif



#include "Coordinate.h"

#ifndef POLAR_COORDINATE_H
#define POLAR_COORDINATE_H
class Polar_Coordinate: public Coordinate{
	public:
		Polar_Coordinate(double x, double theta);
		virtual Coordinate* convert(Coordinate*);
};
#endif



Now in Polar.cpp
#include "Polar_Coordinate.h"

Polar_Coordinate::Polar_Coordinate(double x, double y):Coordinate(x,y){};

Coordinate* Polar_Coordinate::convert(Coordinate* c){
	Coordinate* ret_val = (Coordinate*)new Polar_Coordinate(1.0,0.0);

	return ret_val;
}



Do something similar in Cartesian.cpp

Jw
Was This Post Helpful? 0
  • +
  • -

#14 deborah_morales1990  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 08-April 09

Re: Calling derived class from main function

Posted 08 October 2009 - 08:06 PM

Jw, Thank you so much for your help. Today, my teacher told me that he made a mistake. We were not supposed to use pointers in this assignment. He said it was not suppose to be a pure virtual function instead just a virtual.

class Coordinate {
private:
float value1, value2;
public:
Coordinate(float, float);
float get_distance(Coordinate);
virtual float convert();

class Polar_Coordinate : public Coordinate {
public:
Polar_Coordinate(float, float);
};
class Cartesian_Coordinate : public Coordinate {
public:
Cartesian_Coordinate(float, float);
};



that is how it is supposed to be.

I believe I am understanding more about it now. It was hard using pointers because he hadn't discussed that in class yet. I remember doing a little bit of it in C programming last semester.

Thank you agian for help!!! I truly appreciate it.
I will work on the new code above that my professor wants us to work on.

It is making a little more sense. Perhaps if I stare at the screen for another ten hours I will get it! haha :)

Thanks agian!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1