Constructors
• Has same name as that of class name• Used to initialize the instant variables of the  objects
Types of constructors• Default constructor• Parameterized constructor• Copy constructor
Default constructor• Initializes instant variables of an object with  definite values readily defined• Called by default w...
Creating objects• Objects can be created in two ways      Object supplied by the compiler      Object supplied by the pr...
Object supplied by the compilerclass example{int a,b;example(){a=10;b=20;}void display(){S.o.pln(“The value of a is” +a);S...
Object supplied by the programmerclass example{int a,b;example(){a=0;b=0;}void display(){S.o.pln(“The value of a is” +a);S...
Parameterized constructor• Initializes the object  variables by passing  parametric values at  the time of creation
 Object supplied by  the compiler Object supplied by  the programmer
Object supplied by the compilerclass example{int a,b;example(int x, int y){a=x;b=y;}void display(){S.o.pln(“The value of a...
Object supplied by the programmerclass example{int a,b;example(int x, int y){a=x;b=y;}void display(){S.o.pln(“The value of...
Copy constructor• Copies the initial values of the instant  variables of an object to the instant variables  of another ob...
Types• Direct entry copy constructor• Copy constructor by passing the objects
Direct entry copy constructor•    Initial value is copied to another object without using any specific constructorclass ex...
Copy constructor by passing the                objectsclass example{int a,b;example(int x,int y){a=x;b=y;}Example(example ...
Constructor overloading• Constructors with the same name but  with different types of parameters
Encapsulation
• Wrapping up of data and function into a single class
Visibility modes• Public• Private• Protected
• A class member not declared under any  visibility are by default public• Private can be used only within the scope  of t...
Upcoming SlideShare
Loading in...5
×

Constructors

412
-1

Published on

copy constructor, overloading

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
412
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
22
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Constructors

  1. 1. Constructors
  2. 2. • Has same name as that of class name• Used to initialize the instant variables of the objects
  3. 3. Types of constructors• Default constructor• Parameterized constructor• Copy constructor
  4. 4. Default constructor• Initializes instant variables of an object with definite values readily defined• Called by default while creating an object
  5. 5. Creating objects• Objects can be created in two ways  Object supplied by the compiler  Object supplied by the programmer
  6. 6. Object supplied by the compilerclass example{int a,b;example(){a=10;b=20;}void display(){S.o.pln(“The value of a is” +a);S.o.pln(“The value of b is” +b);}}
  7. 7. Object supplied by the programmerclass example{int a,b;example(){a=0;b=0;}void display(){S.o.pln(“The value of a is” +a);S.o.pln(“The value of b is” +b);}}Class mai{Void main(){Example ob = new example();Ob.display();}}
  8. 8. Parameterized constructor• Initializes the object variables by passing parametric values at the time of creation
  9. 9.  Object supplied by the compiler Object supplied by the programmer
  10. 10. Object supplied by the compilerclass example{int a,b;example(int x, int y){a=x;b=y;}void display(){S.o.pln(“The value of a is” +a);S.o.pln(“The value of b is” +b);}}
  11. 11. Object supplied by the programmerclass example{int a,b;example(int x, int y){a=x;b=y;}void display(){S.o.pln(“The value of a is” +a);S.o.pln(“The value of b is” +b);}}Class mai{Void main(){Example ob = new example(4,5);Ob.display();}}
  12. 12. Copy constructor• Copies the initial values of the instant variables of an object to the instant variables of another object
  13. 13. Types• Direct entry copy constructor• Copy constructor by passing the objects
  14. 14. Direct entry copy constructor• Initial value is copied to another object without using any specific constructorclass example{int a,b;example(int x,int y){a=x;b=y;}}Class mai{Void main(){Example ob1= new example(5,6);Example ob2 = ob1;S.o.pln(“The value of a is” +a);S.o.pln(“The value of b is” +b);}}
  15. 15. Copy constructor by passing the objectsclass example{int a,b;example(int x,int y){a=x;b=y;}Example(example p){A=p.a;B= p.b;}}Class mai{Void main(){Example ob1= new example(5,6);Example ob2 = new example(ob1);}}
  16. 16. Constructor overloading• Constructors with the same name but with different types of parameters
  17. 17. Encapsulation
  18. 18. • Wrapping up of data and function into a single class
  19. 19. Visibility modes• Public• Private• Protected
  20. 20. • A class member not declared under any visibility are by default public• Private can be used only within the scope of the class and cannot be accessed outside the class• Protected are used as private members which can only be applied within the class but can be inherited to other class
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×