Class is a description of an object (i.e. a compound datatype and it has:
1 Unique Name
2 Attributes
3 Methods
Object is an instance of a class and it has:
1 Unique ID
2 State - the value of the attributes of the class at a particular time
3 Behavior - the methods of the class in action
===
Constructors
What:
1 It is a special method for initializing the state of an object
2 It has same name as class name
3 It has no return type
4 It can be overloaded
5 It is usually public but may also be private - for singleton classes
6 Since it can overloaded it can also be chained
Why:
when a class is instantiated i.e. an object is created then the attributes of the class need to be initialized with some value (other than default which is zero or NULL).
e.g.
class A{
//x is an attribute its value is the state
private int x;
public int getx(){
return x;
}
public void setx(int t){
x=t;
}
} // end of class
//creating an object:
A obj = new A();
System.out.println(obj.getx());
===
Types of Constructors:
Normal Constructor:
1 Default
2a User Defined Parameterless
2b User Defined Parameterized
Copy Constructor:
1 Default
2 User Defined
---
1 Default Normal constructor:
What:
It is present only when no user defined normal constructor is present i.e. it is automatically created by the compiler.
When:
if there is no user defined constructor
2a User Defined Parameterless:
this is very much similar to the default
e.g.
class Data{
private int [] a;
// a user defined parameterless constructor
public Data(){
a = new int[10];
}
public void show(){
int n = a.length;
for(int i=0; i<n; i++)
System.out.prinln(a[i]);
}
} // end of class
// create an object
Data obj = new Data();
obj.show();
2b User Defined Parameterized
it can take different arguments and set the state of the object according to those values
e.g.
class Data2{
private int [] a;
// a user defined parameterless constructor
public Data2(){
a = new int[10];
//marking redundant code
int n=a.length;
for(int i=0; i<n; i++)
a[i] = random(1,11);
//end marking redundant code
}
// a user defined parameterized constructor
// it is also an overloaded version
public Data2(int t){
a = new int[t];
//marking redundant code
int n=a.length;
for(int i=0; i<n; i++)
a[i] = random(1,11);
//end marking redundant code
}
public void show(){
int n = a.length;
for(int i=0; i<n; i++)
prinln(a[i]);
}
} // end of class
// create objects
Data2 obj1 = new Data2();
Data2 obj2 = new Data2(15);
===
class Data3{
private int [] a;
// a user defined parameterless constructor
public Data3(){
//performing constructor chaining
this(10,0,0);
}
// a user defined parameterized constructor
// it is also an overloaded version
public Data3(int t){
this(t,1,6);
}
public Data3(int t, int p, int q){
a = new int[t];
int n=a.length;
for(int i=0; i<n; i++)
a[i] = random(p,q+1);
}
public void show(){
int n = a.length;
for(int i=0; i<n; i++)
prinln(a[i]);
}
} // end of class
//create object
Data3 obj1 = new Data3(); //first constructor
Data3 obj1 = new Data3(15); //second constructor
c/c++
int x;
cin >> x; // read some value from the console and put it in x
int a[3]; //an array of three elements
int *p; // this is a pointer variable
//to store the address of an integer variable
p = &x; // copy the address of x into p
cout<< x << " " << p;
p = a;
cout << p[0] << "\n"; // print the first value of array
cout << *p << "\n"; // also print the first value of array
cout << p; //address of array
p++;
cout << p[0]; // 2nd element of array
cout << *p; // also 2nd element
cout << p; // address of 2nd element
/*/////////////////java code////////////////////
int [] a = new int[3];
int [] p;
p = a;
System.out.print(p[0]);
test(p);
test(a);
//////////////////////////////
void test(int [] q)
{
System.out.print(q[0]);
}
1 Unique Name
2 Attributes
3 Methods
Object is an instance of a class and it has:
1 Unique ID
2 State - the value of the attributes of the class at a particular time
3 Behavior - the methods of the class in action
===
Constructors
What:
1 It is a special method for initializing the state of an object
2 It has same name as class name
3 It has no return type
4 It can be overloaded
5 It is usually public but may also be private - for singleton classes
6 Since it can overloaded it can also be chained
Why:
when a class is instantiated i.e. an object is created then the attributes of the class need to be initialized with some value (other than default which is zero or NULL).
e.g.
class A{
//x is an attribute its value is the state
private int x;
public int getx(){
return x;
}
public void setx(int t){
x=t;
}
} // end of class
//creating an object:
A obj = new A();
System.out.println(obj.getx());
===
Types of Constructors:
Normal Constructor:
1 Default
2a User Defined Parameterless
2b User Defined Parameterized
Copy Constructor:
1 Default
2 User Defined
---
1 Default Normal constructor:
What:
It is present only when no user defined normal constructor is present i.e. it is automatically created by the compiler.
When:
if there is no user defined constructor
2a User Defined Parameterless:
this is very much similar to the default
e.g.
class Data{
private int [] a;
// a user defined parameterless constructor
public Data(){
a = new int[10];
}
public void show(){
int n = a.length;
for(int i=0; i<n; i++)
System.out.prinln(a[i]);
}
} // end of class
// create an object
Data obj = new Data();
obj.show();
2b User Defined Parameterized
it can take different arguments and set the state of the object according to those values
e.g.
class Data2{
private int [] a;
// a user defined parameterless constructor
public Data2(){
a = new int[10];
//marking redundant code
int n=a.length;
for(int i=0; i<n; i++)
a[i] = random(1,11);
//end marking redundant code
}
// a user defined parameterized constructor
// it is also an overloaded version
public Data2(int t){
a = new int[t];
//marking redundant code
int n=a.length;
for(int i=0; i<n; i++)
a[i] = random(1,11);
//end marking redundant code
}
public void show(){
int n = a.length;
for(int i=0; i<n; i++)
prinln(a[i]);
}
} // end of class
// create objects
Data2 obj1 = new Data2();
Data2 obj2 = new Data2(15);
===
class Data3{
private int [] a;
// a user defined parameterless constructor
public Data3(){
//performing constructor chaining
this(10,0,0);
}
// a user defined parameterized constructor
// it is also an overloaded version
public Data3(int t){
this(t,1,6);
}
public Data3(int t, int p, int q){
a = new int[t];
int n=a.length;
for(int i=0; i<n; i++)
a[i] = random(p,q+1);
}
public void show(){
int n = a.length;
for(int i=0; i<n; i++)
prinln(a[i]);
}
} // end of class
//create object
Data3 obj1 = new Data3(); //first constructor
Data3 obj1 = new Data3(15); //second constructor
c/c++
int x;
cin >> x; // read some value from the console and put it in x
int a[3]; //an array of three elements
int *p; // this is a pointer variable
//to store the address of an integer variable
p = &x; // copy the address of x into p
cout<< x << " " << p;
p = a;
cout << p[0] << "\n"; // print the first value of array
cout << *p << "\n"; // also print the first value of array
cout << p; //address of array
p++;
cout << p[0]; // 2nd element of array
cout << *p; // also 2nd element
cout << p; // address of 2nd element
/*/////////////////java code////////////////////
int [] a = new int[3];
int [] p;
p = a;
System.out.print(p[0]);
test(p);
test(a);
//////////////////////////////
void test(int [] q)
{
System.out.print(q[0]);
}
No comments:
Post a Comment