Can you call a constructor within a constructor?

Answer Posted / jinendra pandey

when i am tring above code getting following error
Can we call a constructor within a constructor?

Error:------------>
object.cpp: In constructor Account::Account():
object.cpp:15: error: this cannot be used as a function


#include<iostream.h>
class Account {
private:
double balance;

// 1st Constructor
public:
Account( double initBalance ) {
balance = initBalance;
}

// 2nd Constructor
public:
Account() {
this( 0.0 ); // call the 1st constructor
}
};

int main()
{
Account a;
return 0;
}

Error:------------>
object.cpp: In constructor Account::Account():
object.cpp:15: error: this cannot be used as a function

Is This Answer Correct ?    1 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is meant by object?

583


This abstract class is correct ? abstract class A { public abstract void Disp(); public abstract void B() { } public absract virtual C() { } }

567


What is java ceil?

576


What are the characteristics of Final,Finally and Finalize keywords.

708


What is data structure in java?

543






Does unicode support all languages?

531


What are the data types supported by java? What is autoboxing and unboxing?

554


Draw a UML class diagram for the code fragment given below: public class StringApplet extends Applet { private Label sampleString; private Button showTheString; private ButtonHandler bHandler; private FlowLayout layout; public StringApplet() { sampleString = new Label(" "); showTheString = new Button (" Show the String"); bHandler = new ButtonHandler(); layout = new FlowLayout(); showTheString.addActionListener(bHandler); setLayout(layout); add(sampleString); add(showTheString); } class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { samplestring.setText("Good Morning"); } } } Note: The methods need not be indicated on the diagram.

1592


What is difference between add() and addelement() in vector?

1075


What is the difference between JVM and JRE?

626


How do you sort a string in alphabetical order in java?

537


What is the purpose of void class?

615


What is bigger kb or mb?

594


Can we increase array size dynamically in java?

520


What is the use of arraylist class in java?

563