I'll start and will base my answer on this
Quote:
iam just rookie in coding fields
I'm not sure though if I understand this question correctly:
Quote:
1) (wall &obj) how can we initialize an object inside the same class
I'll give the full picture: a class is a combination of
data members and
member functions. There is a special type of member function called
constructor. The syntax of constructors is special, in that they do not have a return value. The purpose of constructors is the initialization of the class object, also called
instance of the class. You can create multiple instances of the same class. Here's an example that constructs the four walls of a house:
class Wall {
...
};
int main() {
double wall_height = 10.0;
double house_width = 20.0;
double house_length = 30.0;
Wall north_wall(house_length, wall_height);
Wall east_wall(house_width, wall_height);
Wall south_wall(north_wall); Wall west_wall(east_wall); ...
}
The last four lines create four instances of the class Wall, by calling it's constructor functions. Note how you do not explicitely call these constructors as you would a normal function - they are called implicitely whenever you write code that creates an instance.
Does that answer your question?
Quote:
why we are using obj address to create object
I think you mistake the '&' symbol for an 'address-of' operator. It is not. Within the declaration of a variable or function parameter, the '&' denotes a 'reference' to an object. It is common practice in C++ to send class objects by reference when calling a function. Rick York has already elaborated on that topic in his solution.
Quote:
how this copy constructor works
The code I posted above illustrates the use of the copy constructor: since the north and south wall are equal in measurements, I chose to copy one from the other. The line
Wall south_wall(north_wall);
defines a local variable south_wall of type Wall. The compiler will now implicitely search for an appropriate constructor among the ones defined in the class Wall. Since the line above specifies one parameter of type Wall, the compiler looks for a constructor which will take one parameter of type Wall,
or a reference to it. It finds the copy constructor, which takes a reference. Then the compiler creates the appropriate machine code to call this copy constructor when the code reaches this point. At run time, the copy constructor will receive a reference to the north wall object, and copy the values stored in that object into the newly created south wall instance.