Explain constructor.

Answer Posted / k.gnaneshwar

*-> Constructor is used for initializing the variables
during object creation.
*-> that means the code required for initializing the
variable should be writen in constructor.
CONSTRUCTOR DECLARATION RULES
* Constructor look like a method.
* Constructor have no return type.
* Constructor name should be same name as the class name.

(prog)

class simpleAddition
{
int a,b; //variable declaration
simpleAddition() //constructor declaration
{
a=6;
b=3;
}
int add() // method declaration
{
return a+b;
}
}

output:- 9

Is This Answer Correct ?    2 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Write a console application and implement the ternary operator to decide whether the age a user entered after being prompted is allowed to vote or not(given that only citizens between 18 and 120 years only inclusive can vote). Use exception handling for non-numerical input.

2098


Is a games console a computer?

669


What is gui in c#?

747


what are the Disadvantages of vb

776


What is a string in c#?

647


What is console application with example?

676


What you mean by inner exception in c#?

723


What is the use of partial methods?

767


What are Memory foot print of an exe?

682


What does type safety mean?

795


How many aware interfaces are there?

633


What is jagged array?

706


Is c and c# the same?

667


What is concrete class in c# with example?

652


What does xpath return?

727