Computer Applications
Answer
Some of the special properties of the constructor functions are as follows:
- It has the same name as that of its class.
- It doesn't have any return type, not even void.
- It adheres to the rules of access specifiers.
- It can be parameterized or non-parameterized.
- It is used to create and initialize objects from a class.
- It can be overloaded to provide multiple ways of creating objects with different initialisation parameters
- It is always called implicitly when an object is created using the
newkeyword.
Related Questions
When a compiler can automatically generate a constructor if it is not defined then why is it considered that writing constructors for a class is a good practice ?
'Accessibility of a constructor greatly affects the scope and visibility of their class.' Elaborate this statement.
What is a parameterized constructor ? How is it useful ?
Design a class to represent a bank account. Include the following members:
Data members
- Name of the depositor
- Account number
- Type of account
- Balance amount in the account
Methods
- To assign initial values
- To deposit an amount
- To withdraw an amount after checking balance
- To display the name and balance
- Do write proper constructor functions