Click here to Skip to main content
15,072,391 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
class Wall {
   private:
    double length;
    double height;

   public:

    // parameterized constructor
    Wall(double len, double hgt) {
        // initialize private variables
        length = len;
        height = hgt;
    }

    // copy constructor with a Wall object as parameter
    Wall(Wall &obj) {                //1
        // initialize private variables
        length = obj.length;
        height = obj.height;
    }
    double calculateArea() {
        return length * height;
    }
};

1) (wall &obj) how can we initialize an object inside the same class,why we are using obj address to create object, how this copy constructor works enlighten me

What I have tried:

iam just rookie in coding fields, enlighten me tigers
Posted
Updated 9-Jan-21 7:00am
v3

The copy constructor is passed another object by reference and uses its values to set its own. I prefer to use a set method and then route the copy constructor and assignment operator through it. Regardless, I hope you see that they are doing the same thing but with different arguments. Here's how that can be done :
C++
class Wall
{
public:
    Wall() {} // default constructor

    // parameterized constructor

    Wall( double len, double hgt)
    {
        Set( len, hgt );
    }

    // copy constructor with an object as parameter

    Wall( const Wall & obj )
    {
        Set( obj.length, obj.height );
    }

    // assignment operator

    Wall & operator=( const Wall & obj )
    {
        Set( obj.length, obj.height );
        return *this;
    }

    void Set( double ln, double ht )
    {
        // set values of private members

        length = ln;
        height = ht;
    }

    double calculateArea()
    {
        return length * height;
    }

private:
    double length  { 0 };
    double height  { 0 };
};
Note that usually objects that are copied are pass as constant references because they are not modified in any way. I added an assignment operator also just so you can see how that is done. This is how you can use these :
C++
    Wall w1( 6, 7 );
    Wall w2;        // w2 is constructed with default values
    w2 = w1;        // w2 is assigned values w1
    Wall w3( w2 );  // w3 is constructed with values of w2

    double area = w3.calculateArea();

    // result is 42
}
   
Comments
CPallini 8-Jan-21 2:15am
   
5.
Quote:
hy we are using obj address to create object,
Because, as a general rule, passing objects by reference is more efficient than passing them by value (when you pass an object by value, a copy of the original object must be created). See, for instance: IBM Knowledge Center - Pass by reference (C++ only)[^].
   
v3
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:
C++
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); // same measurements, so just copy the north wall
    Wall west_wall(east_wall); // same measurements as 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
C++
Wall south_wall(north_wall); // same measurements, so just copy the 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.
   
Comments
Coding With Anu 12-Jan-21 0:49am
   
@stefan_lang @rick york thanks alot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900