|
-
Nov 4th, 2002, 06:11 PM
#1
Thread Starter
Good Ol' Platypus
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 = (x1, y1)-(x2, y2) // define a line by two points
myline Line = (x1, y1, m) // 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)
-
Nov 4th, 2002, 06:36 PM
#2
Frenzied Member
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.
-
Nov 4th, 2002, 08:37 PM
#3
transcendental analytic
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.
-
Nov 4th, 2002, 10:07 PM
#4
Hyperactive Member
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
-
Nov 4th, 2002, 10:34 PM
#5
Thread Starter
Good Ol' Platypus
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)
-
Nov 4th, 2002, 11:17 PM
#6
Thread Starter
Good Ol' Platypus
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 x1, double y1, double x2, double y2); // 2x Points
nline(double x, double y, double m); // Point/Slope
nline(double m, double b); // y = mx + b
nline(int a, int b, int c); // Ax + By + C = 0
~nline();
double slope();
double yint();
};
nline::nline(double x1, double y1, double x2, double y2) {
l_m = (y2 - y1) / (x2 - x1);
l_b = y1 - (x1 * l_m);
}
nline::nline(double x, double y, double m); {
l_m = m;
l_b = y - (x * l_m);
}
nline::nline(double m, double b) {
l_m = m;
l_b = b;
}
nline::nline(int a, int b, int c) {
l_m = -a / b;
l_b = c / 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, -2, 5, 7);
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)
-
Nov 4th, 2002, 11:21 PM
#7
Frenzied Member
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.
-
Nov 4th, 2002, 11:50 PM
#8
Thread Starter
Good Ol' Platypus
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)
-
Nov 5th, 2002, 04:54 AM
#9
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.
-
Nov 5th, 2002, 04:35 PM
#10
Thread Starter
Good Ol' Platypus
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)
-
Nov 5th, 2002, 09:34 PM
#11
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|