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

C++ Exam Question Please Help

Options
  • 09-04-2013 3:27pm
    #1
    Registered Users Posts: 3,017 ✭✭✭


    Hi all,

    I have a C++ exam on monday and I would appreciate some help. I'm currently going through some past papers and I need help on this question:


    Design a C++ class to represent an object that consists of two

    members, a and b. Provide methods to overload the assignment (=),
    addition +, subtraction - , and division / operators, as given below.
    Provide a constructor that can initialise the object with specified
    values for a and b.


    Operations on the object consisting of the members a and b with

    another object with members c and d.
    Addition: (a,b) + (c,d) = (a+c, b+d)
    Subtraction (a,b)– (c,d) = (a-c, b-d)
    Multiplication: (a,b)(c,d) = (ac-bd,bc+ad)
    Division: (a,b)/(c,d) = ((ac+bd)/(c
    ^2 + d^2), (bc-ad)/ (c^2 + d^2))

    (You may recognise this as the behaviour of complex numbers.)

    I am absolutely hopeless with C++. :( The only help I can find relevant appears to be this example tutorial on classes.

    Based on that I have made a hopeless attempt below:

    [FONT=BookmanOldStyle][SIZE=2]//vectors: overloading operators[/SIZE][/FONT]
    [FONT=BookmanOldStyle][SIZE=2]#include <iostream>[/SIZE][/FONT]
    [FONT=BookmanOldStyle][SIZE=2]using namespace std;[/SIZE][/FONT]
     
    [FONT=BookmanOldStyle][SIZE=2]class CVector {[/SIZE]
    [INDENT][SIZE=2]public:[/SIZE]
    [/INDENT][/FONT]
    [INDENT][FONT=BookmanOldStyle][SIZE=2]int x,y;[/SIZE][/FONT]
    [FONT=BookmanOldStyle][SIZE=2]CVector () {};[/SIZE][/FONT]
    [FONT=BookmanOldStyle][SIZE=2]CVector (int,int);[/SIZE][/FONT]
    [FONT=BookmanOldStyle][SIZE=2]CVector operator + (CVector);[/SIZE][/FONT]
    [FONT=BookmanOldStyle][SIZE=2]CVector operator - (CVector);[/SIZE][/FONT]
    [FONT=BookmanOldStyle][SIZE=2]CVector operator * (CVector);[/SIZE][/FONT]
    [FONT=BookmanOldStyle][SIZE=2]CVector operator / (CVector);[/SIZE][/FONT]
    [/INDENT][FONT=BookmanOldStyle][SIZE=2]};[/SIZE][/FONT]
     
    [FONT=BookmanOldStyle][SIZE=2]CVector::CVector (int a, int b){[/SIZE]
    [INDENT][SIZE=2]x = a;[/SIZE]
    [/INDENT][/FONT]
    [INDENT][FONT=BookmanOldStyle][SIZE=2]y = b;[/SIZE][/FONT]
    [/INDENT][FONT=BookmanOldStyle][SIZE=2]}[/SIZE][/FONT]
     
    [FONT=BookmanOldStyle][SIZE=2]CVector CVector::operator+ (CVector param) {[/SIZE]
    [INDENT][SIZE=2]CVector temp;[/SIZE]
    [/INDENT][/FONT]
    [INDENT][FONT=BookmanOldStyle][SIZE=2]temp.x = x + param.x[/SIZE][/FONT]
    [FONT=BookmanOldStyle][SIZE=2]temp.y = y + param.y;[/SIZE][/FONT]
    [FONT=BookmanOldStyle][SIZE=2]return (temp);[/SIZE][/FONT]
    [/INDENT][FONT=BookmanOldStyle][SIZE=2]}[/SIZE][/FONT]
    [FONT=BookmanOldStyle][SIZE=2]CVector CVector::operator- (CVector param) {[/SIZE]
    [INDENT][SIZE=2]CVector temp;[/SIZE]
    [/INDENT][/FONT]
    [INDENT][FONT=BookmanOldStyle][SIZE=2]temp.x = x - param.x[/SIZE][/FONT]
    [FONT=BookmanOldStyle][SIZE=2]temp.y = y - param.y;[/SIZE][/FONT]
    [FONT=BookmanOldStyle][SIZE=2]return (temp);[/SIZE][/FONT]
    [/INDENT][FONT=BookmanOldStyle][SIZE=2]}[/SIZE][/FONT]
    [FONT=BookmanOldStyle][SIZE=2][SIZE=2]CVector CVector::operator* (CVector param) {[/SIZE]
    [INDENT][SIZE=2]CVector temp;[/SIZE]
    [/INDENT][/SIZE][/FONT][SIZE=2]
    [INDENT][FONT=BookmanOldStyle][SIZE=2]temp.x = (x*param.x) + (y*param.y);[/SIZE][/FONT]
    [FONT=BookmanOldStyle][SIZE=2]temp.y = (x*param.y) + (y*param.x);[/SIZE][/FONT]
    [FONT=BookmanOldStyle][SIZE=2]return (temp);[/SIZE][/FONT]
    [/INDENT][FONT=BookmanOldStyle]}[/FONT]
     
    [FONT=BookmanOldStyle][SIZE=2]CVector CVector::operator* (CVector param) {[/SIZE]
    [INDENT][SIZE=2]CVector temp;[/SIZE]
    [/INDENT][/FONT]
    [INDENT][FONT=BookmanOldStyle][SIZE=2]temp.x = (x*param.x) + (y*param.y)/((param.x)*(param.x) + (param.y)*(param.y));[/SIZE][/FONT]
    [FONT=BookmanOldStyle][SIZE=2]temp.y = (y*param.x) + (x*param.y)/((param.x)*(param.x) + (param.y)*(param.y));[/SIZE][/FONT]
    [FONT=BookmanOldStyle][SIZE=2]return (temp);[/SIZE][/FONT]
    [/INDENT][FONT=BookmanOldStyle]}[/FONT]
     
    [/SIZE]
    

    I have no idea if this is right and I have no idea how to complete it. I'm unsure in the example I listed to include a b c d instead of actual numbers in the main function and if that would actually work.

    Appreciate any help on how to start/complete it.


Comments

  • Registered Users Posts: 92 ✭✭jgh_


    http://courses.cms.caltech.edu/cs11/material/cpp/donnie/cpp-ops.html

    Additionally you overloaded `*` twice (I think the second one you intended to overload `/` )

    Also it says to overload the assignment operator, but never actually says what you're supposed to do with that... I recommend overloading it to print out a birthday cake.

    Also your overloaded operator methods should look more like

    const CVector operator+ ( const CVector& param ) { ... }


Advertisement