Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C++ problem, and its just not funny anymore...

  • 21-12-2000 2:15am
    #1
    Registered Users, Registered Users 2 Posts: 2,660 ✭✭✭


    First of all what I would like to ask is, does anyone recommend a great book on c++ programming, as I need one. At the moment I am using "c++ how to program" by the deitels and while being pretty good it is a little inconsistent, ie in the first chapter about classes it explains some very basic member functions ie ones which operate only upon the calling objects data, but in the exercises it asks you to do a class with a member function to allow you to do a particular operation on both the calling objects data and another of the same object, with absolutely nothing even resembling a sample on how to do this. Now I have a couple of ideas on how i could do this but I have a gut feeling that what I am thinking just isn't a good way to code, and well while I'm learning I would like to get into good habits, anyway heres the question:
    Create a class called [b]Complex[/b] for performing arithmetic with complex numbers. Write a
    driver to test your class.
    
          Complex numbers have the form
    
           [b]realPart + imaginaryPart * i[/b]
    
    where i is 
           
          squareroot(-1)
    
    Use floating-point variables to represent the [b]private[/b] data of the class. Provide a constructor function
    that enables an object of this class to be initialized when it is declared. The constructor should
    contain default values in case no initializers are provided. Provide [b]public[/b] member functions for 
    each of the following:
        a) Addition of two [b]Complex[/b] numbers: The real parts are added together and the imaginary
           parts are added together.
        b) Subtraction of two [b]Complex[/b] numbers: The real part of the right operand is subtracted
           from the real part of the left operand and the imaginary part of the right operand is subtracted
           from the imaginary part of the left operand.
        c) Printing [b]Complex[/b] numbers in the form [b](a, b)[/b] where [b]a[/b] is the real part and [b]b[/b] is the
           imaginary part.
    

    I think that the constructor and the printing code will be fine it's the rest that I find difficult. What I was thinking of doing was invoking the function on one object passing the second in as an arguement and then creating a third in the function to store the calculations with, problem is I'm not sure if that is legal never mind good programming practice. I also must say that they are *******s for asking this question when obviously there are no such samples in the book.

    ta for any help.

    ps I just want ideas, and maybe code for the addition and subtraction ones but i don't really care ideas are most important.

    [This message has been edited by Baz_ (edited 21-12-2000).]


Comments

  • Registered Users, Registered Users 2 Posts: 310 ✭✭Cerberus


    Yeah, your idea is right. Just give the class name as the data type for the arguments in your function prototype.
    The reason they probably don't give an example is that your problem is really to do with functions and not with classes. Functions take objects of different data types as their arguments and your class is just another data type no different to int or char except that thay are user defined.

    Personally I find the the D&D book is a great book for reference and for explaining sh1t although I had previous programming knowledge before reading it.



  • Closed Accounts Posts: 218 ✭✭Void


    The good way to solve this problem is to use operator overloading too define addition and subtraction operators on the Complex class. This is a bit too advanced for Chapter 1 of the book though so I think Cerberus' way is better for now.

    In my maths library, for example, I have Vector, Point and Quaternion classes.
    Using operator overloading I can use them like this:

    Vector v1, v2, v3;
    float dotproduct;

    v1 = v2 + v3;
    dotproduct = v2 * v3;

    This makes the code very easy to use etc. I'm don't have my code to hand at the moment, but I'll post my implementation of the Vector structure when I get home. It's similar to the Complex class except a Vector has 3 doubles basically.



  • Registered Users, Registered Users 2 Posts: 1,176 ✭✭✭podgeen


    both ideas are good..
    when u get the first one working.. then take a go at overloading.... wont b too difficult if u've done it other way first..
    am i making sense?
    are these my feet?


  • Registered Users, Registered Users 2 Posts: 310 ✭✭Cerberus


    Yeah operator overloading would really be the best way of going about all the arithmetic with complex numbers.

    No u never make sense podgeen...
    They are not you're feet either. They're mine. You stole them off me when we was back in 'Nam on our third tour. I want them back. Wash them first though before u give them back.
    I like green trees......I'll get me coat.


  • Registered Users, Registered Users 2 Posts: 1,176 ✭✭✭podgeen


    Your fúcking gimpy posts will no longer be tolerated Podgeen. Fúck off and enlighten someone else with your juvenile humour.

    [This message has been edited by Void (edited 12-01-2001).]


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,660 ✭✭✭Baz_


    thanks void but I got all I could done during the chrimbo holidays of c++, now its back to college where I've just gotten a massive three or four month project so I won't be having much time for self studies (b@stard lecturers). I will take a look at it in the future though (prolly summer before I get a chance) but I do thank you.


  • Closed Accounts Posts: 218 ✭✭Void


    Here's an implementation of a Point class.
    Operator overloading ahoy!
    (C) Necrosoft 2000 smile.gif
    struct Point
    {
    	double x;
    	double y;
    	double z;
    
    	__forceinline Point() {}
    	
    	__forceinline Point(double _x, double _y, double _z) 
    	{
    		x=_x; y=_y; z=_z;
    	}
    	
    	__forceinline Point(double* xyz) 
    	{
    		x = xyz[0];
    		y = xyz[1];
    		z = xyz[2];
    	}
    
    	__forceinline Point& init() 
    	{
    		memset(this,0,sizeof(*this));
    		return *this;
    	}
    	
    	__forceinline Point& init(double _x, double _y, double _z) 
    	{
    		x=_x; y=_y; z=_z;
    		return *this;
    	}
    
    	__forceinline set(double _x, double _y, double _z)
    	{
    		x=_x; y=_y; z=_z;
    	}
    
    	// l'array access - very handy and useful sometimes
    	__forceinline const double& operator[]( int i ) const 
    	{
    		return (&x)[i];
    	}
    	
    	__forceinline double& operator[]( int i ) 
    	{
    		return (&x)[i];
    	}
    
    	__forceinline Point& operator = (Point& v) 
    	{
    		memcpy(this,&v,sizeof(*this));
    		return *this;
    	}
    	
    	__forceinline Point& operator = (Vector& v); 
    
    	__forceinline bool operator==(const Point& a) const 
    	{
    		Point b = *this;
    		return ((b.x == a.x) && (b.x == a.x) && (b.x = a.x));
    	}
    	};
    
    __forceinline Point operator - ( const Point& a, const Point& b) 
    {
    	return Point( a.x-b.x, a.y-b.y, a.z-b.z );
    };
    
    __forceinline Point operator + ( const Point& a, const Point& b) 
    {
    	return Point( a.x+b.x, a.y+b.y, a.z+b.z );
    };
    


    [This message has been edited by Void (edited 12-01-2001).]


Advertisement