Results 1 to 11 of 11

Thread: Multiple Constructors

  1. #1

    Thread Starter
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134

    Multiple Constructors

    Ah, now we see where C++ really shines; but can it have multiple class constructors? Say I want to have a LINE class, and declare it (and I don't yet know how classes work.. I'm learning it tonight ):
    PHP Code:
    myline Line = ***; //Is this where the constructor goes?

    // Anyway, can I have multiple constructors? Such as:

    myline Line = (x1y1)-(x2y2// define a line by two points
    myline Line = (x1y1m// define a line by one point and the slope 
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  2. #2
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Constructors actually look like this:
    Code:
    Line myLine(x1, y1, x2, y2);
    Line myLine2(x, y, m);
    And yes, you can have multiple constructors.

    Z.

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    no big deal, meaningless finesse selling OOP..
    You can overloads function names, meaning two functions can have the same name but different arguments, as in argument count, types of those arguments including const modifiers..
    constructors are functions and overloaded similarly.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Sastraxi: You can also write a copy constructor if you intend to create an object with another object as parameter or pass the class object by value.
    Code:
    //class declaration
    Class Line
    {public:
        //Constructors
        Line();
        Line(int x, int y, int m)
        Line(const Line& line);//copy constructor
        ........
    }
    How to use,

    Code:
    Line line1(0,0,1);
    Line line2(line1);//copy constructor at work
    Line *line3=new Line(line1);//copy constructor at work

  5. #5

    Thread Starter
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    All very neat little tidbits. The copy constructors will definately come in handy in this case, if say I was to create a parallel line or a perpendicular line. Let's see if I can't make a lil' project...

    Thanks guys, you're the best
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  6. #6

    Thread Starter
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Okay, I've run into some problems
    Dug out my old math texts to do this... oddly enough though I've remembered them all...
    PHP Code:
    // Lines.cpp
    #include <iostream>
    using namespace std;

    class 
    nline {
        
    double l_m;
        
    double l_b;
    public:
        
    nline(double x1double y1double x2double y2); // 2x Points
        
    nline(double xdouble ydouble m); // Point/Slope
        
    nline(double mdouble b); // y = mx + b
        
    nline(int aint bint c); // Ax + By + C = 0
        
    ~nline();
        
    double slope();
        
    double yint();
    };

    nline::nline(double x1double y1double x2double y2) {
        
    l_m = (y2 y1) / (x2 x1);
        
    l_b y1 - (x1 l_m);
    }

    nline::nline(double xdouble ydouble m); {
        
    l_m m;
        
    l_b - (l_m);
    }

    nline::nline(double mdouble b) {
        
    l_m m;
        
    l_b b;
    }

    nline::nline(int aint bint c) {
        
    l_m = -b;
        
    l_b b;
    }

    nline::~nline() {
        
    l_m 0;
        
    l_b 0;
    }

    double nline::slope() {
        return 
    l_m;
    }

    double nline::yint() {
        return 
    l_b;
    }


    int main() {
    nline line1(8, -257);
        
    cout << line1.slope << endl;
        
    cout << line1.yint << endl;
        return 
    0;
    }
    // EOF 
    And it spits out like 51 erros. What's wrong? Do classes have to be in header files or something?
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  7. #7
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    nline::nline(double x, double y, double m); {

    Take a good look =P.

    Second, if you overload the constructor, you MUST have a default constructor (one that takes no arguments). In your case, it would simply be empty.

    Z.

  8. #8

    Thread Starter
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    I hate myself Thanks again!
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You don't NEED the default constructor, but unless you supply one you can't write
    nline line;
    because that would need the default constructor.

    You don't need the destructor: when it is called you can't use the object afterwards anyway, so there is no point in setting the members to 0, it only needs time.

    I don't know what exactly you are doing but usually you don't need the precision that double gives you. float needs less memory and often suffices, especially for games.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10

    Thread Starter
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    I'm just doing it for a test, maybe I'll extend it for n-sided figures, linesegs, and points in the future.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  11. #11
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Originally posted by CornedBee
    I don't know what exactly you are doing but usually you don't need the precision that double gives you. float needs less memory and often suffices, especially for games.
    Its also faster, since a float fits in 32 bits.

    Z.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width