Thread: trouble defining member functions

  1. #1
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655

    trouble defining member functions

    yet again, assignment time, im 75% done w/this one, i have a question though, im trying to define some of the member functions in this class i was given and im kinda at a loss for what im supposed to do (kinda like usual)

    here's what it says to do in the assignment: Define all the functions specified in the Point class described above. Note that inline functions must be specified in the header file containing the class declaration “assignment7.h”. Note that you may to modify the constructor and destructor functions from the previous assignment to keep track of the static variable.
    whats he talking about inline functions? and how does that relate to the operator+, -, += etc.???

    thanks
    //including both .h && .cpp files
    guns dont kill people, abortion clinics kill people.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    I would be very worried about your college if they teach this sort of crap...
    Code:
    double& X()
    You never ever ever return a non constant reference to a private member. To do so defeats all ideals of encapsulation and object oriented programs. You should refuse to implement those functions.
    As for the rest there is nothing hard there although given half a chance i would design the interface. Search the board for examples of operator overloading and static class members.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    For short functions, you can make them "inline", which replaces all the function calls in main with the actual lines of code in the function. If the function is short that can be more efficient than going through the process of calling the function.

  4. #4
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655
    >>I would be very worried about your college if they teach this sort of crap...

    youre tellin me... this is supposed to be a c++ class and we just started on this stuff about 2 weeks ago we probably wont get to polymorphism, this whole semester has been about making code as efficient as possible...nothing to do w/the basics of c++, not learning the language, how to be efficient w/the language....how can you be efficient if you dont KNOW IT? DAMN..... i think i will refuse to do those and bring it up in class tomorrow.

    thanks for the pointers guys
    guns dont kill people, abortion clinics kill people.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    ok i feel sorry for you now so here you go. This could be better as the operator + and operator - should be nonmembers but i followed your interface....

    the .h file
    Code:
    class Point
    {
    	public:
            Point(double x = 0.0, double y = 0.0, double z = 0.0):
              x_(x),y_(y),z_(z)
              { ++ctr_;}
    
              Point(const Point& rhs):
              x_(rhs.x_),y(rhs.y_),z(rhs.z_)
              { ++ctr_;}
    		
    		~Point()
            { --ctr_;}
    
    		void X(double x) { x_ = x;}
            void Y(double y) { y_ = y;}
            void Z(double z) { z_ = z;}
    
    		//double& X();
            const double& X() const { return x_;}	
    
    		//double& Y();
            const double& Y() const { return y_;}	
    
    		//double& Z();			
            const double& Z() const {return z_;}
    		
    		Point operator+(const Point& rhs)
            {
                return Point( x_+rhs.x_,y_+rhs.y_,z_+rhs.z_);
            }
    		
    		const Point& operator+=(const Point& rhs)
            {
                x_+= rhs.x_;
                y_+= rhs.y_;
                z_+= rhs.z_;
                return *this;
            }
    
    		Point operator-(const Point& rhs); 
            { return Point( x_-rhs.x_,y_-rhs.y_,z_-rhs.z_);}
    
    		const Point& operator-=(const Point& rhs)
            {
                x_-= rhs.x_;
                y_-= rhs.y_;
                z_-= rhs.z_;
                return *this;
            }
    
            static int NumPoints(){ return ctr_;}
    		
    	private:
    		// coordinates of point in three-dimensional coordinate system
    		double x_, y_, z_;
    		static int ctr_;
    };
    the .cpp file
    Code:
    #include "assignment7.h"
    
    int Point::ctr_ = 0;
    Now read thru that. make sure you understand everything thats going on. Read your assignment again and look at the code and it should come together.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655
    wow, i wasnt expecting that, THANKS! im looking through it but i have another question

    w/this code here
    Code:
    const Point& operator+=(const Point& rhs)
            {
                x_+= rhs.x_;
                y_+= rhs.y_;
                z_+= rhs.z_;
                return *this;
            }
    and all of the other operator functions, all youre doing is translating the point by using the specific operator?
    guns dont kill people, abortion clinics kill people.

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    all we are doing with the operators is defining what operator + and - etc. means for this class.
    I used your implementations. Right or wrong i dont know. its your code just repackaged better.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655
    ahhhh got it, again, thanks for the help
    guns dont kill people, abortion clinics kill people.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers to Class Member Functions
    By Dark_Phoenix in forum C++ Programming
    Replies: 6
    Last Post: 09-02-2007, 02:21 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  4. Class member variables only readable by member functions?
    By _Elixia_ in forum C++ Programming
    Replies: 4
    Last Post: 10-10-2003, 03:52 PM
  5. How do you call member functions
    By Elite in forum C++ Programming
    Replies: 7
    Last Post: 08-05-2003, 01:03 PM