Copy constructor and assignment operators with polymorphism

Just failed an interview because of this, and I'm still confused:

class A
{
public:
    A(int a) : a_(a) {}
    
    // Copy constructor
    
    // Assignment operator
    
private:
    int a_;
};
 
class B : public A
{
public:
    B(int a, int b) : A(a), b_(b) {
    }
    
    // Copy constructor
    
    // Assignment operator
    
private:
    int b_;
};

How do you implement the copy constr/assignment op, and why does the constructor of B have to have an A initialized?



Read more here: https://stackoverflow.com/questions/65835537/copy-constructor-and-assignment-operators-with-polymorphism

Content Attribution

This content was originally published by Larry Jing at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: