Thread: Need some help/advise for Public/Private classes

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    4

    Question Need some help/advise for Public/Private classes

    Hello!

    I am considerably new in C++ and need some of your advises!

    Actually I am doing my assignment and getting some troubles in the way.
    I am using Microsoft Visual C++ (from Visual Studio .Net 2005)

    While I try to compile my code, I get the following erros:

    error C3861: 'getX': identifier not found
    error C3861: 'getY': identifier not found
    error C3861: 'resMag': identifier not found
    error C3861: 'resDir': identifier not found


    It seems to me that I have to make the classes getX, getY, resMag and resDir PUBLIC. But I do not see the proper reason for that.

    I am providig the code to help you out understand the problem better:

    Code:
    /*
       Developer  : Nirali Patel
       Course     : CSCI 201
       Project    : Assignment #1 - VECTORS
       Date       : 09/23/2006
    
       File             : vector.cpp
       Description: vector class.
    */
    
    #include <iostream>
    #include <stdlib.h>
    #include <cmath>
    
    using namespace std;
    
    class vector
    {
    private:
    	double dir; // Vector direction
    	double mag; // vector magnitude
    
    	double getX(double, double); // Get horizontal components of the vectors
                    double getY(double, double); // Get vertical components if the vectors
    	
                    double resDir(double, double); // Resultant Directions
                    double resMag(double, double); // Resultant Magnitudes
    
    public:
    	vector(); // Constructor to make the Vector NULL
                    vector(vector &temp);  // Constructor to copy the Vector
    	
    	friend vector operator + (const vector& lhs, const vector& rhs);
    	friend vector operator - (const vector& lhs, const int rhs);
    	friend vector operator * (const vector& lhs, const vector& rhs);
    
    	friend int operator == (const vector& lhs, const vector& rhs);
    	friend int operator != (const vector& lhs, const vector& rhs);
    
    	friend ostream& operator << (ostream& out, const vector& rhs);
    	friend istream& operator >> (istream& out, vector& rhs);
    };
    
    /*
    	Default Constructor
    */
    vector::vector()
    {
    	dir = 0.0; // Set vector direction to NULL
    	mag = 0.0; // Set vector magnitude to NULL
    }
    
    /*
    	Copy Constructor
    */
    vector::vector(vector &temp)
    {
    	dir = temp.dir; // Set vector direction to Temperory vector direction
    	mag = temp.mag; // Set vector mahnitude to Temperory vector magnitude
    }
    
    /*
    	Get horizontal component
    */
    double vector::getX(double ang, double dist)
    {
    	double ans;
    
    	ans = sin[ang*(M_PI/180)]*dist; // Horizontal (X) component
    
    	return ans;
    }
    
    /*
    	Get vertical component
    */
    double vector::getY(double ang, double dist)
    {
    	double ans;
    
    	ans = cos[ang*(M_PI/180)]*dist; // Vertical (Y) component
    
    	return ans;
    }
    
    /*
    	Get the resultant magnitude of 2 vectors
    */
    double vector::resMag(double x, double y)
    {
    	double ans;
    
    	ans = sqrt(pow(x,2)+pow(y,2));
    
    	return ans;
    }
    
    /*
    	Get the resultant direction of 2 vectors
    */
    double vector::resDir(double x, double y)
    {
    	double ans;
    
    	ans = atan(y/x);
    
    	return ans;
    }
    
    /*
    	Operator +
    */
    vector operator + (const vector& lhs, const vector& rhs)
    {
    	vector ans;
    
    	double lhsDir, rhsDir; 
    	double lhsMag, rhsMag;
    
    	double lhsX, rhsX;
    	double lhsY, rhsY;
    
    	double X, Y;
    
    	lhsDir = lhs.dir;
    	rhsDir = rhs.dir;
    
    	lhsMag = lhs.mag;
    	rhsMag = rhs.mag;
    
    	lhsX = getX(lhsDir, lhsMag); // Get horizontal component of LHS vector
    	rhsX = getX(rhsDir, rhsMag); // Get horizontal component of RHS vector
    
    	lhsX = getY(lhsDir, lhsMag); // Get vertical component of LHS vector
    	rhsX = getY(rhsDir, rhsMag); // Get vertical component of RHS vector
    
    	X = lhsX + rhsX; // Total of all horizontal components
    	Y = lhsY + rhsY; // Total od all vertical components
    
    	ans.mag = resMag(X, Y);
    	ans.dir = resDir(X, Y);
    
    	return ans;
    }
    Thank you

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    If client code (the code that uses an object of your class) needs to use something directly, you make it public. Otherwise, in the absence of inheritance, you make it private. This is an oversimplification though, because it doesn't take into account data hiding, where your data members should always be private and accessed in a controlled way through public member functions.
    My best code is written with the delete key.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    shouldn't this ( sin[ang*(M_PI/180)] ) be

    sin(ang*(M_PI/180)) same for cos etc
    Last edited by twomers; 09-23-2006 at 12:04 PM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    Yes, that does make sense!
    But in my case (the code above), when PUBLIC functions tries to call PRIVATE functions, I am getting an error. (Both PRIVATE and PUBLIC functions are prat of the same class ofcourse!)

    Thanks

    Quote Originally Posted by Prelude
    If client code (the code that uses an object of your class) needs to use something directly, you make it public. Otherwise, in the absence of inheritance, you make it private. This is an oversimplification though, because it doesn't take into account data hiding, where your data members should always be private and accessed in a controlled way through public member functions.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    I think that's because you are trying to call member functions without the object. Why not just make getX() and getY() free functions?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    You are right, but I have heard somewhere that there is an exception for '[' and ']' so I am just testing that... otherwise, () is there!!

    (By the way I didn't get any syntax error so far so looks like working!)

    Quote Originally Posted by twomers
    shouldn't this ( sin[ang*(M_PI/180)] ) be

    sin(ang*(M_PI/180)) same for cos etc

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    Hmm... I will try that!

    But I think I am calling functions with objects, please have a look:

    Code:
    class vector
    {
    private:
    	double dir; // Vector direction
    	double mag; // vector magnitude
    
                    double getX(double, double); // Get horizontal components of the vectors
                    double getY(double, double); // Get vertical components if the vectors
    	
                    double resDir(double, double); // Resultant Directions
                    double resMag(double, double); // Resultant Magnitudes
    And when I call function:

    Code:
            lhsX = getX(lhsDir, lhsMag); // Get horizontal component of LHS vector
    	rhsX = getX(rhsDir, rhsMag); // Get horizontal component of RHS vector
    
    	lhsX = getY(lhsDir, lhsMag); // Get vertical component of LHS vector
    	rhsX = getY(rhsDir, rhsMag); // Get vertical component of RHS vector
    Quote Originally Posted by laserlight
    I think that's because you are trying to call member functions without the object. Why not just make getX() and getY() free functions?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    No, you are calling getX() and getY() as if they were free functions. From what I see, they might as well be free functions. However, there is yet another possibility:
    Implement operator+= as a member function. Implement operator+ as a non-member non-friend function by using the member operator+=.

    EDIT:
    If you do not quite understand what I meant, here is a code snippet:
    Code:
    // member function
    vector& vector::operator+=(const vector& rhs)
    {
        // Total of all horizontal components
        double x = getX(dir, mag) + getX(rhs.dir, rhs.mag);
        // Total of all vertical components
        double y = getY(dir, mag) + getY(rhs.dir, rhs.mag);
    
        mag = resMag(x, y);
        dir = resDir(x, y);
        return *this;
    }
    
    // non-member non-friend function
    vector operator+(const vector& lhs, const vector& rhs)
    {
        return vector(lhs) += rhs;
    }
    Note that you have to correctly implement your copy constructor with the signature:
    Code:
    vector::vector(const vector& temp)
    Last edited by laserlight; 09-23-2006 at 12:31 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I would rename the class so that when you use std::vector later in your programming you won't get it confused with your vector class.

    Perhaps vector2D or something would suffice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM