0% found this document useful (0 votes)
199 views11 pages

Constructors 10th Icse

A constructor is a special member method that initializes instance variables of a class and has the same name as the class. It is invoked when an object is created, has no return type, and is always public. There are two main types of constructors: default and parameterized, with examples provided for classes 'Prime' and 'Marks'.

Uploaded by

vishwas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
199 views11 pages

Constructors 10th Icse

A constructor is a special member method that initializes instance variables of a class and has the same name as the class. It is invoked when an object is created, has no return type, and is always public. There are two main types of constructors: default and parameterized, with examples provided for classes 'Prime' and 'Marks'.

Uploaded by

vishwas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Constructors

ICSE 10th
Constructor

• It is a member method with a name same as that of the class name


used to initialize the instance varibles of the objects
Invoking a Constructor

• A construction is invoked at the time of creating an object of that


class
• Syntax
• <class name><object name>=new <constructor>
Features
• The Constructor has same name as class name
• It automatically gets invoked while creating an object
• It has no return type – NO arthimetical or logical operation can be
performed on the constructor so it not required
• The constructor is always public
• The constructor is overloaded automatically
• Is only to intitialize the data members/instance variables
Types of Constructors
• Default
• Parameterized
• Default : Initializes the instance variables of an object with the
definite values readily available
Program 1
• Class name : Prime
• Data members : int n
• Member methods :
• Prime() : default constructor to initiaze n
• Void input(int x):to assign n with x
• Void display () to check whter the number is prime or not
Program 2
• Class name : Marks
• Data members : name,age,m1,m2,m3,maximum,average
• Member methods :
• Paramerized constructor to initiaze the data members n
• To accept the details of the student s
• To compute the average & ‘the maximum out of 3 subjects
• Display all the variables
Questions

• Difference Constructor vs method


• Difference static vs non static data members
• Features of constructor
• Constructor overloading
• Two types of constructors ( define)

You might also like