GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-Apr-2006, 12:31
worms707 worms707 is offline
New Member
 
Join Date: Jan 2006
Posts: 6
worms707 is on a distinguished road

Point to Point3D Inheritance


***EDIT***

I have the following code

This is the Point header file
CPP / C++ / C Code:
#ifndef POINT_H
#define POINT_H

#include<string>
using namespace std;

//Class Point represents points in the Cartesian coordinate

class Point{
protected:
  double xCoord, yCoord;           //coordinates of the point
  string name;                     //name of the point

public:
  Point();                         //default constructor
  Point(double, double, string);    //conversion constructor
  
  double getX() const;
  double getY() const;
 
  string getName() const;
  
  void setX(double);
  void setY(double);
  
  void setName(string);
  
  double distanceFrom(Point&); 
  void printP();
};

#endif


This is the cpp file defining the Point class:
CPP / C++ / C Code:
#include <iostream>
#include <string>   
#include <cmath>     
#include "Point.h"
#include "Point3D.h"
using namespace std;


Point::Point() {	//default constructor
  xCoord = 0;
  yCoord = 0;
  name = "origin";
}


Point::Point(double x, double y, string str) {	//conversion constructor
  xCoord = x;
  yCoord = y;
  name = str;
}
  

//accessor methods
double Point::getX() const {
  return xCoord;
}


double Point::getY() const {
  return yCoord;
}


string Point::getName() const {
  return name;
}


void Point::setX(double x) {
  xCoord = x;
}


void Point::setY(double y) {
  yCoord = y;
}


void Point::setName(string str) {
  name = str;
}

//overloaded insertion operator <<
ostream& operator<<(ostream& o, const Point3D& p)
{
	return o;
}



This is the Point3D header file:
CPP / C++ / C Code:
#ifndef POINT3D_H
#define POINT3D_H

#include<string>
using namespace std;


//Class Point represents points in the Cartesian coordinate

class Point3D: public Point{
private:
  double zCoord;           //coordinates of the point
  
public:
  Point3D();                         //default constructor
  Point3D(double, double, double, string);    //conversion constructor
	
  double getZ() const;	
  void setZ(double);

  double distanceFrom(Point3D&); 
 string getName() const;
  void printP();

};


#endif



******This is the updated cpp file defining the Point3D class:

CPP / C++ / C Code:
#include <iostream>
#include <string>   
#include <cmath>  
#include "Point.h"
#include "Point3D.h"
using namespace std;

Point3D::Point3D() {	//default constructor
  zCoord = 0;
  setX(0);
  setY(0);

}


Point3D::Point3D(double x, double y, double z, string str) {	//conversion constructor
  setX(x);
  setY(y);	
  zCoord = z;
name =str;
}
  
//accessor methods
double Point3D::getZ() const {
  return zCoord;
}



void Point3D::setZ(double z) {
  zCoord = z;
}

//overloaded insertion operator <<
//ostream& operator<<(ostream& o, const Point3D& p)
//{
//	return o;
//}

double Point3D::distanceFrom(Point3D& p) {	//distance between this point & p
  return  sqrt((xCoord - p.xCoord) * (xCoord - p.xCoord) +
			   (yCoord - p.yCoord) * (yCoord - p.yCoord) +			   
			   (zCoord - p.zCoord) * (zCoord - p.zCoord));
}
string Point3D::getName() const {
  return name;
}
void Point3D::printP() {
  cout << name << " (" << xCoord << ", " << yCoord << ", " << zCoord << ")" << endl;
}



*****And this is the updated cpp file to test if it works:

CPP / C++ / C Code:
#include <iostream>
#include <string>
#include "Point.h"
#include "Point3D.h"
#include <fstream>

using namespace std;


// function prototypes 
void copy(fstream& inFile, fstream& outFile);

int main(int argc, char* argv[])
{
  /* fstream inf;
  fstream outf1;
  fstream outf2;

  if (argc != 3){
    cerr << "Usage: " << argv[0] << " infile outfile";
    cerr << "\nCopy contents of infile to outfile" << endl;
    exit(1);
  }
  
  inf.open(argv[1], ios::in);
  
  // Check if the input file exists or not
  if (!inf){
    cerr << "File " << argv[1] 
         << " does not exist.  Halting program!" << endl;
    exit(1);
  }

  // open the output file in read mode first to see 
  // if the file exists or not
  //outf1.open(argv[2], ios::in);

//  if (outf1)
  //{
  //  char ans;
	
//	cerr << "\nFile " << argv[2] << " already exists!  Overwrite? (Y/N) --> ";
  //      cin >> ans;
  //  if ((ans != 'Y') && (ans != 'y'))
 //     exit(1);                     // don't overwrite
 // }
  // outfile does not exist or allows overwriting 
 // outf1.close();

  // now open the output file in output mode.
  // If the file already exists, it will be overwritten; 
  // otherwise it will be created
  outf2.open(argv[2], ios::out);
  if (!outf2) {
     cerr << "\nCan not open output file." << endl;
     exit(1);
  }
 
  // copy infile to outfile
  copy(inf, outf2);

  inf.close();
  outf2.close();
  
*/
Point3D p0;

  Point3D p1(8,5,5, "A");
  Point3D p2(3,6,10, "B");
  
 // p0.printP();
 // p1.printP();
 // p2.printP();

  cout << "The distance from Point " << p1.getName() << " to Point " 
       << p2.getName() << " is " << p1.distanceFrom(p2) << endl;

  system ("pause");
  return 0;

}


void copy(fstream& inFile, fstream& outFile)
{ //copy the contents of inFile to outFile
  char c;
  
  while ((c = inFile.get()) != EOF){
      outFile << c;
  }
}



I know that this bunch of files, but I am not allowed to combine them, since this is a test of my knowledge of inheritance.


After I figure out what is wrong, I will have to finish the code. The program will have to take values from an input file, which will contain two or more points, and the program should take two of the points and find the distance between them. After that, the program will output the results in an output file. Again, this is not shown because I am not sure how to do that.

I am getting the following errors:****FIXED****

asd error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Point3D const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@ std@@AAV01@ABVPoint3D@@@Z) already defined in Point3D.obj

asd fatal error LNK1169: one or more multiply defined symbols found


***Now my problem is getting the program to read the input files and outting them into the output files. The test cpp file has the code for getting the input and output files to open and to put the stuff in the input and copying it to the output.

Is there a way to do this, (opening input and output files) on Microsoft visual studio?

Could someone please skim through the code and find what I am doing wrong? I would be grateful.

Thank you.
  #2  
Old 24-Apr-2006, 12:55
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Point to Point3D Inheritance


Quote:
Originally Posted by worms707
I have the following code

I am getting the following errors:

asd error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Point3D const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@ std@@AAV01@ABVPoint3D@@@Z) already defined in Point3D.obj

asd fatal error LNK1169: one or more multiply defined symbols found


The way that the names get munged makes it kind of hard to get anything useful from the message, so you have to try to see if any part of it has any recognizable stuff --- like picking the fly specks out of the pepper (and I apologize to anyone who is eating as they read this).

The error indication seems to be telling us that there is some kind of operator: "<<" that is defined in such a way that it is ambiguous. I think it is telling us that it was defined in whatever file it was that gave you Point3D.obj (maybe the file name is Point3D.cpp, or some such thing). The other definition is in the file that was being linked after that one (maybe Point.cpp?)

Anyhow, I would look for file(s) that have some kind of definition(s) for a "<<" operator. Are they correct?

Regards,

Dave
  #3  
Old 24-Apr-2006, 14:42
worms707 worms707 is offline
New Member
 
Join Date: Jan 2006
Posts: 6
worms707 is on a distinguished road

Re: Point to Point3D Inheritance


To clear things up:
I fixed my old problem, but I have a new problem with the program.

I don't know how I would incorporate an input.txt file and an output.txt file.

The program takes the numbers in the input.txt file and calculates the distance between the points. Then it would output it in the output.txt file.

An example of the input.txt file would look like:

A 3 4 5
B 3 4 5

(those numbers would correspond to the x, y, z coords)

then the output would be:
The distance between A and B is 0
  #4  
Old 24-Apr-2006, 15:32
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Point to Point3D Inheritance


Quote:
Originally Posted by worms707

I don't know how I would incorporate an input.txt file and an output.txt file.

Here's a quick reference program using input and output fstreams. In a practical program, the file names would probably be obtained from user input (from command line arguments, or by prompting the user from the program).

I just did the calculations by 'brute force', instead of using your neat classes. Your classes might include overloaded operators for >> (extraction of items from ifstreams) and << (insertion of data into ofstreams). But, my usual practice is to first of all, make sure that I can do it. Then I do it "nice and pretty".

CPP / C++ / C Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;

int main()
{
  string pointName[2];
  double xCoord[2], yCoord[2], zCoord[2];
  double distance;
  int i;

  string inFileName, outFileName;
  inFileName  = "test.txt";
  outFileName = "out.txt";

  ifstream inFile(inFileName.c_str());

  if (!inFile) {
    cout << "Can't open file " << inFileName << " for reading." << endl;
    return 0;
  }
  cout << "Opened input file " << inFileName << endl;

  for (i = 0; i < 2; i++) {
    inFile >> pointName[i] >> xCoord[i] >> yCoord[i] >> zCoord[i];
    if (!inFile) {
      cout << "There was a problem reading the point number " << i << endl;
      inFile.close();
      return 0;
    }
  }
  inFile.close();
  cout << "Points:" << endl;
  for (i = 0; i < 2; i++) {
  cout << "   "
       << pointName[i] << ":  (" << xCoord[i] << ", " << yCoord[i] << ", "
       << zCoord[i] << ")" << endl;
  }

  ofstream outFile(outFileName.c_str());
  if (!outFile) {
    cout << "Can't open file " << outFileName << " for writing" << endl;
    return 0;
  }
  cout << "Opened output file " << outFileName << endl << endl;

  distance = sqrt((xCoord[1]-xCoord[0])*(xCoord[1]-xCoord[0]) + 
                  (yCoord[1]-yCoord[0])*(yCoord[1]-yCoord[0]) + 
                  (zCoord[1]-zCoord[0])*(zCoord[1]-zCoord[0]));

  cout << "Distance: " << distance << endl;

  outFile << "The distance between " << pointName[0] << " and " << pointName[1]
          << " is " << distance << endl;

  outFile.close();

  return 0;
}

My "input.txt" file looked like this:
Code:
A 8.0 5.0 5.0 B 3.0 6.0 10.0

The program console output looked like this:
Code:
Opened input file test.txt Points: A: (8, 5, 5) B: (3, 6, 10) Opened output file out.txt Distance: 7.14143

After the program run, file out.txt looked like this:
Code:
The distance between A and B is 7.14143

Notes: I always test to make sure files are opened properly and I test the state of the stream after obtaining user input data (from files or console).

I always print out any data items that I obtained from user input (files or console).

I usually print "status report" items as files are opened, and at other significant places in the flow. That way if things go bad, I have some indicaton of how far it got before the big bomb.

Regards,

Dave
  #5  
Old 24-Apr-2006, 16:00
worms707 worms707 is offline
New Member
 
Join Date: Jan 2006
Posts: 6
worms707 is on a distinguished road

Re: Point to Point3D Inheritance


Ok... I kind of get what you were saying and I modified the program a little. I have the input and output files working.

The thing is, I don't know how I would get the program to read the values in the input.txt file.
  #6  
Old 24-Apr-2006, 16:02
davis
 
Posts: n/a

Re: Point to Point3D Inheritance


Quote:
Originally Posted by worms707
To clear things up:
I fixed my old problem, but I have a new problem with the program.

I don't know how I would incorporate an input.txt file and an output.txt file.

The program takes the numbers in the input.txt file and calculates the distance between the points. Then it would output it in the output.txt file.

An example of the input.txt file would look like:

A 3 4 5
B 3 4 5

(those numbers would correspond to the x, y, z coords)

then the output would be:
The distance between A and B is 0

Use a container of points such that you add a Point3D to the container every time you read a 3D point from the file. Open your file, then use your streaming (insertion/extraction) operators to initialize your Point3D instances.

Implement your operators "-", "+", ">", "<", "=", "==" and "!=" so that you can discern whether the points are different and if they are, the differences between them. You'll also want to implement a copy constructor and a destructor.

Your use of inheritance suggests to me that Point should probably have some behavioral specification for derived types...and at least a virtual destructor.


:davis:
  #7  
Old 24-Apr-2006, 16:09
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Point to Point3D Inheritance


Quote:
Originally Posted by worms707
Ok... I kind of get what you were saying and I modified the program a little. I have the input and output files working.

The thing is, I don't know how I would get the program to read the values in the input.txt file.

My example read the values from test.txt (since that is the name of the file used to open the inFile ifstream).

Quote:
Originally Posted by davekw7x
CPP / C++ / C Code:
    inFile >> pointName[i] >> xCoord[i] >> yCoord[i] >> zCoord[i];

For your project, you might create an overloaded ">>" operator in the class definition so that your program could simply do something like this:

CPP / C++ / C Code:
    inFile >> my3DPoint;

Now, in my simple example, I just bailed out of the program at the first sign of trouble (it's only a demonstration, after all). In your class definitions, you would probably want to be a little more civilized and throw an exception (or something). That's your call (since it's your project, not mine).


Regards,

Dave
  #8  
Old 24-Apr-2006, 16:27
worms707 worms707 is offline
New Member
 
Join Date: Jan 2006
Posts: 6
worms707 is on a distinguished road

Re: Point to Point3D Inheritance


Quote:
Originally Posted by davekw7x
My example read the values from test.txt (since that is the name of the file used to open the inFile ifstream).



For your project, you might create an overloaded ">>" operator in the class definition so that your program could simply do something like this:

CPP / C++ / C Code:
    inFile >> my3DPoint;

Now, in my simple example, I just bailed out of the program if it couldn't read the right stuff. In your class definitions, you would probably want to be a little more civilized and throw an exception (or something). That's your call (since it's your project, not mine).


Regards,

Dave

I am lost. What should I do first?
  #9  
Old 24-Apr-2006, 16:41
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Point to Point3D Inheritance


Quote:
Originally Posted by worms707
I am lost. What should I do first?

I can't possibly know what your requirements (get file name from user or use some predefined name, etc.) and project parameters (deadline, etc.) are. I also don't know what you are expected to use in order to complete the assignment (overloaded i/o operators or what?) Therefore, any guidelines that I could come up with would necessarily fall into the category of vague generalities.

Here are a few vague generalities:

If it were my project, I might take the basic class definitions, and in my main() program I would read input lines from a given file and put the name and numbers into the individual name and coordinate values of a couple of points (using the class member functions setName(), setX(), etc.). I would probably use getX(), etc., to retrieve the values from the objects and print out their values in main(). Then I would calculate the distance using the mathematical formula on the individual items. Then I would call whatever distance() function I had already defined to calculate the distance, and print out the result to make sure that it had the correct value.

If I was supposed to use an overloaded input operator to read the values, I would implement this next. I would probably practice with a simpler class that only reads things into one member so that I could make sure that I knew how to implement an overloaded operator. Then I might try it on my 2Dclass that takes a string and two numbers. Then I might try it on my 3Dclass that takes a string ant three numbers. I would debug each by testing it by itself (and printing out the values that it read in each time) and go on to whatever other tasks that the assignment requires.

If I had a problem with any particular task, I would break that part out into a separate test program that just did whatever one new thing that I was trying to accomplish and make sure that I really nailed it before trying to integrate it back into the main flow.

Regards,

Dave
  #10  
Old 24-Apr-2006, 17:20
worms707 worms707 is offline
New Member
 
Join Date: Jan 2006
Posts: 6
worms707 is on a distinguished road

Re: Point to Point3D Inheritance


Quote:
Originally Posted by davekw7x

I would read input lines from a given file and put the name and numbers into the individual name and coordinate values of a couple of points (using the class member functions setName(), setX(), etc.). I would probably use getX(), etc., to retrieve the values from the objects and print out their values in main(). Then I would calculate the distance using the mathematical formula on the individual items. Then I would call whatever distance() function I had already defined to calculate the distance, and print out the result to make sure that it had the correct value.

This is perfect. The sentence in bold is where I am having trouble.

Requirements:
The teacher is using UNIX to compile the program, so when she executes the file, she will have to execute the file and the input and output txt files.
ex:
% point3d-test input.txt output.txt

The input.txt file is someone she will make up. It will consist of two lines :
A 4 3 2
B 5 5 3
for example. The output file will be created after the program is run and it will contain the distance between the points in the input.txt file.

We are to have an overloaded << operator. I have most of it in the program already.

Due wednesday at 3pm. Other than that, everything is up for grabs. Nothing too complex.
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 3) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ inheritance illbemissingu C++ Forum 14 23-Oct-2005 17:35
Inheritance Project .. Qatar C++ Forum 1 28-Apr-2005 17:04
inheritance problem ap6118 C++ Forum 8 11-Apr-2005 11:24
Inheritance problem CaptnB C++ Forum 2 27-Jan-2005 08:09
Floating point exception error (Note-long post) crystalattice C++ Forum 16 11-Sep-2004 07:37

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:21.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.