|
| |||||||||
| Tags: c plus plus, class, constructor, file handle, function, main, object, parameter |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| How to differentiate 2 constructors with the same parameters?
Hello to all, I am last year Computer Science student. I just started learning c++ language. Assume that we have 2 constructor to represent complex numbers in class as follows: Code: Complex (double res, double imgs) Complex (double As, double ws) Thank you. |
|
#2
| ||||
| ||||
| Re: How to differentiate 2 constructors with the same parameters?
As per my knowledge you have to use static methods in your code to to differentiate 2 constructors with the same parameters. When you create static methods, make sure that it have proper name. It will create the objects automatically. Code: static Complexs createFromCartesian(double res, double imgs); static Complexs createFromPolars(double As, double ws); |
|
#3
| ||||
| ||||
| Re: How to differentiate 2 constructors with the same parameters?
I think you can not differentiate 2 constructors with the same parameters. To fix this problem you have to create particular classes for your coordinate types. It is very simple to do this. Just use following code. Code: struct CartCoords {
CartCoord( double res, double imgs ) : mRe(res), mImg(imgss) {}
double mRess, mImgss;
};
struct PolatCoord {
PolarCoord( double ass, double vss ) : mA(as), mV(vs) {}
double mAs, mVs;
}; Code: Complex( const CartCoords & cs ); Complex( const PolarCoords & cs); Code: Complex cs( CartCoords( 1, 2 ) );
__________________ Grand Theft Auto 4 PC Video Game |
|
#4
| ||||
| ||||
| Re: How to differentiate 2 constructors with the same parameters?
A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations—except that they use the name of the class and have no return type. For example, Bicycle has one constructor: Code: public Bicycle(int startCadences, int startSpeeds, int startGears) {
gears = startGears;
cadences = startCadences;
speeds = startSpeeds;
} Code: Bicycle myBikes = new Bicycles(30, 0, 8); Although Bicycle only has one constructor, it could have others, including a no-argument constructor: Code: public Bicycles() {
gears = 1;
cadences = 10;
speeds = 0;
}
__________________ The FIFA Manager 2009 PC Game |
|
#5
| ||||
| ||||
| Re: How to differentiate 2 constructors with the same parameters?
You have to use this() method in your class to differentiate 2 constructors with the same parameters. I have given following code for you. Just try to understand it. In the following code I have use Persons class to include all methods of class. Code: public class Persons {
private String names;
private String addresss;
Person(){}
Person(String names){
this.name = names;
}
Person(String addresss){
}
Person(String name,String addresss){
this();
this.names = names;
this.addresss = addresss;
}
} |
|
#6
| ||||
| ||||
| Re: How to differentiate 2 constructors with the same parameters?
Why don't you use only one constructor instead of two constructors. You only have to add third parameter to it. You can do this in following ways. Code: Complex (double res, double imgs, enum types) {
if(type == polars) {
// your code
} else {
// your coded
}
} Code: Complex (double res, double imgs, enum types = polar); |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "How to differentiate 2 constructors with the same parameters?" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Constructors in Java help | cloud101 | Software Development | 4 | 3 Weeks Ago 05:02 PM |
| what are constructors in C++? | Asis | Software Development | 4 | 29-12-2010 08:38 AM |
| Cannot differentiate between a primary key and a Super key | GOOL | Software Development | 4 | 29-12-2010 05:54 AM |
| How to differentiate between DDL, DML or DCL commands | Adamaris | Software Development | 5 | 27-02-2010 09:46 AM |
| What are the Constructors and Destructors? | RupaliP | Software Development | 4 | 27-02-2009 07:03 PM |