Overloading Constructors
I tried the below Pgm. Just help me for my title Qs.
# Constructors can be overloaded or not?
class MotorCycle
#def initialize()
# puts 'no argument'
# end
def initialize(just_one)
puts 'one argument'
end
puts "Just one constructor"
#puts "With Two constructor"
#obj1=MotorCycle.new();
obj2=MotorCycle.new('one');
end
output 1: Putting the # as in above code
C:\ruby\bp>ruby cs.rb
Just one constructor
one argument
With Two constructor
cs.rb:12:in `initialize': wrong number of arguments (0 for 1) (ArgumentError)
from cs.rb:12:in `new'
from cs.rb:12
C:\ruby\bp>ruby cs.rb
Just one constructor
one argument
output 2: Remove those # in above code
C:\ruby\bp>ruby cs.rbWith Two constructor
cs.rb:12:in `initialize': wrong number of arguments (0 for 1) (ArgumentError)
from cs.rb:12:in `new'
from cs.rb:12


0 Comments:
Post a Comment
<< Home