Results 1 to 6 of 6

Thread: How to differentiate 2 constructors with the same parameters?

  1. #1
    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.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    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. #3
    Join Date
    Oct 2005
    Posts
    2,393

    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 ) );

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    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;
        }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    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. #6
    Join Date
    Jan 2008
    Posts
    1,521

    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);

Similar Threads

  1. Constructors in Java help
    By cloud101 in forum Software Development
    Replies: 4
    Last Post: 18-01-2012, 05:02 PM
  2. what are constructors in C++?
    By Asis in forum Software Development
    Replies: 4
    Last Post: 29-12-2010, 08:38 AM
  3. Cannot differentiate between a primary key and a Super key
    By GOOL in forum Software Development
    Replies: 4
    Last Post: 29-12-2010, 05:54 AM
  4. How to differentiate between DDL, DML or DCL commands
    By Adamaris in forum Software Development
    Replies: 5
    Last Post: 27-02-2010, 09:46 AM
  5. What are the Constructors and Destructors?
    By RupaliP in forum Software Development
    Replies: 4
    Last Post: 27-02-2009, 07:03 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,247,392.19579 seconds with 17 queries