Copy Constucted Data Member Problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Copy Constucted Data Member Problem

  1. #1
    Join Date
    Dec 2007
    Posts
    1

    Copy Constucted Data Member Problem

    Hello.
    I am having a problem working out how to do maths to the two sets of each coordinate in the program below.

    I want (eventually) to do 3 calculations on each set of points for each dimension.

    What I want to do is have x_ (for example) and it's copy being stored in another (i.e. not in the original x_) set of data members in each of the Dimension classes (for now - I will move these to the calculation classes myself).

    Then I would create a calculation class (probably for each separate dimension) that would work out all the answers (along with data members to store the answers in).

    I do not need help with creating the calculation class, I just need (preferably shown in code) how I would send the two versions of x_ back to it's class from main where they are being set at the moment.

    So, in summary what I require:
    • Setting up of the sets of data members in each of the dimension classes
    • Sending the set versions of x, y and z to the new data members in each of their classes


    If anyone could show me the code of how to do this and give a short explanation of how it is done I would be most appreciative.

    Thank you.

    Code:
    #include <iostream>
    using namespace std;
    
    class Point1D 
    {
    public:
      //Initialise x_ to 0
      Point1D():x_(0){}
      
      //Give x_ whatever the value of xVal is?
      Point1D(int xVal):x_(xVal){}
    
      //Copy Constructor (gah! What does this do exactly?)
      Point1D(const Point1D &rhs):x_(rhs.x_){}
      
      //Destructor (virtual because it's the base class)
      virtual ~Point1D(){}
      
      //Print out 1D
      virtual void PrintValues()
      {
        cout << endl
                  << "Point1D values: " << endl
                  << "x= " << x_ << endl;
      }
      
      
      
      
      //Gets x from private and returns its value
      int GetX()const {return x_;}
    
      //Sets x to whatever val contains - therefore x_ is indirectly editable
      void SetX(int val){x_ = val;}
    
    private:
      //Creates x_ as a private integer
      int x_;
    };
    
    
    
    
    //Start class Point2D and inherit Point1D
    class Point2D : public Point1D 
    {
    public:
      //Set the inherited Point1D and the new Point2D to 0(?)
      Point2D():Point1D(0){}
      
      //Crazy copy constructor stuff (really need to find out what this does)
      Point2D(int yVal):Point1D(0),y_(yVal){}
      Point2D(int xVal,int yVal):Point1D(xVal),y_(yVal){}
    
      //Copy Constructor (what does this do?)
      Point2D(const Point1D &rhs):Point1D(rhs.GetX()),y_(0){}
      Point2D(const Point2D &rhs):Point1D(rhs.GetX()),y_(rhs.y_){}
    
      //Deconstructor (virtual because it's a base class to 3D?)
      virtual ~Point2D(){}
      
      //Print out values from 1D (using GetX) and 2D
      virtual void PrintValues()
      {
    
        cout << endl
                  << "Point2D values: " << endl
                  << "x= " << GetX() << endl
                  << "y= " << y_ << endl;
      }
      
      //Gets y from private and returns its value
      int GetY()const {return y_;}
      //Sets y to whatever val contains - therefore y_ is indirectly editable
      void SetY(int val){y_ = val;}
    
    private:
      //Creates y_ as a private integer
      int y_;
    };
    
    
    
    
    //Start class Point3D and inherit Point2D (and therefore Point1D)
    class Point3D : public Point2D 
    {
    public:
      //Set the inherited Point2D, and the new Point3D to 0?
      Point3D():Point2D(0){}
      
      //Really need to find out what this does(?)
      Point3D(int zVal):Point2D(0),z_(zVal){}
      Point3D(int xVal,int yVal, int zVal):Point2D(xVal,yVal),z_(zVal){}
    
      //Copy Constructor (what does this do exactly?)
      Point3D(const Point1D &rhs):Point2D(rhs.GetX(),0),z_(0){}
      Point3D(const Point2D &rhs):Point2D(rhs.GetX(),rhs.GetY()),z_(0){}
      Point3D(const Point3D &rhs):Point2D(rhs.GetX(),rhs.GetY()),z_(rhs.z_){}
    
      //Destructor
      ~Point3D(){}
      
      //Print 3D Values (including 1D and 2D)
      void PrintValues()
      {
        cout << endl
                  << "Point3D values: " << endl
                  << "x= " << GetX() << endl
                  << "y= " << GetY() << endl
                  << "z= " << z_ << endl;
      }
    
      //Gets z from private and returns its value
      int GetZ()const {return z_;}
      //Sets z to whatever val contains - therefore z_ is indirectly editable
      void SetZ(int val){z_ = val;}
    
    private:
      //Creates z_ as a private integer
      int z_;
    };
    
     
          
    //Starts main
    int main() {
    
    int dimension;
    cout<<"Please press 1 for 1D, 2 for 2D, 3 for 3D and press Return\n";
    cin>>dimension;
      
    if (dimension == 1){
       //Setting and printing
       Point1D point1D(1);
       point1D.PrintValues();
       //Changing the copies and printing
       Point1D copyOfPoint1D(2);
       copyOfPoint1D.PrintValues();
       
    
       system("pause");
    }
                  
       else if (dimension == 2){
          //Setting and printing
          Point2D point2D(2,2);
          point2D.PrintValues();
          //Changing the copies and printing
          Point1D point1D(1);
          Point2D point2DFromPoint1D(point1D);
          point2DFromPoint1D.SetY(1);
          point2DFromPoint1D.PrintValues();
          system("pause");
       }
          else if (dimension == 3){
            //Setting and printing
            Point3D point3D(3,3,3);
            point3D.PrintValues();
            //Changing the copies and printing
            Point1D point1D(1);
            Point2D point2DFromPoint1D(point1D);
            point2DFromPoint1D.SetY(2);
            Point3D point3DFromPoint2D(point2DFromPoint1D);
            point3DFromPoint2D.SetZ(3);
            point3DFromPoint2D.PrintValues();
            system("pause");
          }   
    
    //If the user puts anything other than 1, 2 or 3 print an error and restart main
    else { 
         cout<<"Error\n";
       system("pause");
    }
    
    
       return 0;
    }

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    Your question isn't clear. What are the dimensions exactly? Isn't the Point object holding just two values, x and y?
    Danny Kalev

Similar Threads

  1. Replies: 30
    Last Post: 08-31-2011, 08:21 AM
  2. Data Environment - Cache Results problem
    By BernS in forum VB Classic
    Replies: 0
    Last Post: 06-20-2002, 09:27 AM
  3. Method or data member not found?
    By Rober in forum VB Classic
    Replies: 0
    Last Post: 05-24-2002, 09:43 AM
  4. Data List Control Problem
    By John in forum VB Classic
    Replies: 0
    Last Post: 10-12-2001, 05:09 PM
  5. 230k XML data Files - any problem?
    By brian in forum XML
    Replies: 1
    Last Post: 10-26-2000, 02:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center