Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , , , ,

Sponsored Links



How to differentiate 2 constructors with the same parameters?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 15-03-2010
Member
 
Join Date: Nov 2009
Posts: 131
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)
But parameters in the 2 constructor are same. I don't know how to differentiate 2 constructors with the same parameters? Please help me.
Thank you.
Reply With Quote
  #2  
Old 15-03-2010
absolute55's Avatar
Member
 
Join Date: Nov 2005
Posts: 1,238
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);
Reply With Quote
  #3  
Old 15-03-2010
Reegan's Avatar
Member
 
Join Date: Oct 2005
Posts: 2,299
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;
};
After this your constructors become:
Code:
Complex( const CartCoords & cs );
Complex( const PolarCoords & cs);
You have to use:
Code:
Complex cs( CartCoords( 1, 2 ) );
Reply With Quote
  #4  
Old 15-03-2010
opaper's Avatar
Member
 
Join Date: May 2008
Posts: 2,362
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;
    }
To create a new Bicycle object called myBike, a constructor is called by the new operator:
Code:
 Bicycle myBikes = new Bicycles(30, 0, 8);
new Bicycle(30, 0, 8) creates space in memory for the object and initializes its fields.
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
Reply With Quote
  #5  
Old 15-03-2010
MindSpace's Avatar
Member
 
Join Date: Feb 2008
Posts: 1,832
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;
    }
}
Reply With Quote
  #6  
Old 15-03-2010
Modifier's Avatar
Member
 
Join Date: Jan 2008
Posts: 1,502
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 
  }
}
After this you have to write code like.
Code:
Complex (double res, double imgs, enum types = polar);
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 11:20 AM.