본문 바로가기
College Study/C++ (ENG)

[C++] Object-Oriented Programming : Overloading (ENG)

by 2den 2022. 1. 2.
728x90

 

Copy Constructor

How to write a copy constructor:

<class-name> ( const <class-name>& );
// Vector.h
class Vector
{
public:
    Vector(const Vector& other); // copy constructor
private:
    int mX;
    int mY;
};

// Vector.cpp
Vector::Vector(const Vector& other)
    : mX(other.mX)
    , mY(other.mY)
{
}

// Main.cpp
Vector a;    // call the constructor with no parameter
Vector b(a); // call the copy oconstructor
 

There is also an implicit copy constructor. But it performs the shallow copy. If it has pointer variables, it can cause some problems. Then write the user-defined copy constructor.

// ClassRecord.cpp
ClassRecord::ClassRecord(const ClassRecord& other)
    : mCount(other.mCount)
{
    mScores = new int[mCount];
    memcpy(mScores, other.mScores, mCount * sizeof(int));
}

// Main.cpp
// case: 5 datas in scores
ClassRecord classRecord(scores, count);
ClassRecord* classRecordCopy = new ClassRecord(classRecord);
delete classRecordCopy;
 

 

 

Function Overloading

(not really OOP)

void Print(int score);
void Print(const char* name);
void Print(float gpa, const char* name);
int Print(int score); // compile error
int Print(float gpa);
 

If there is no perfectly matching function, it calls a function whose number of correct matches is greater than the number of standard conversions. When the numbers are same, it cause a compile error because it is an ambiguous calling. (+ Function Overload Resolotion)

 

 

 

Operator Overloading : Method

Operators are methods.

Vector sum = v1 + v2;
Vector sum = v1.operator+(v2); // same with the line 1
std::cout << number;
std::cout.operator<<(number);  // same with the line 3
 

Some operators (=, (), [], ->) only can be overloaded with a member function.

M// Vector.h
class Vector
{
public:
    Vector operator+(const Vector& rhs) const;
private:
    int mX;
    int mY;
};

// Main.cpp
Vector v1(10, 20);
Vector v2(3, 17);
Vector sum = v1 + v2;

// Vector.cpp
Vector Vector::operator+(const Vector& rhs) const
{
    Vector sum;
    sum.mX = mX + rhs.mX;
    sum.mY = mY + rhs.mY;

   return sum;
}
 

How to write a member operator:

<return-type> <class-name>::operator<operator-symbol>(<argument-list>)
{
}

Vector Vector::operator-(const Vector& rhsconst;

Vector Vector::operator*(const Vector& rhs) const;

Vector Vector::operator/(const Vector& rhs) const;

 

 

 

Operator Overloading: Global Function, Friend

When the left-hand operand is in an inaccessible class (like 'std' above), it's okay to write a global function. To let the function access private members of the class, use a friend function.

class X
{
    friend class Y
    // ...
}

class Z
{
    friend void Foo(Z& z) const;
    // ...
}
 

A freind function is not a member function.

 

How to write a non-member operator overloading:

// header
friend <return-type> operator<operator-symbol>( <argument-list> );


// cpp
<return-type> operator<operator-symbol>( <argument-list>)
{
}
friend void operator<<(std::ostream& os, const Vector& rhs);
friend Vector operator*(int scalar, const Vector& lhs);
 

Return ostream so that the result of the operation between std::cout and a vector object can be operated on with std::endl.

// Vector.h
class Vector
{
    friend std::ostream& operator<<(const std::stream& os, const Vector& rhs);
public:
    // ...
private:
    // ...
};

// Vector.cpp
std::ostream& operator<<(std::ostream& os, const Vector& rhs)
{
    os << rhs.mX << ", " << rhs.mY;
    return os;
}

// Main.cpp
Vector vector1(10, 20);
std::cout << vector1 << std::endl;
 

Use const.

 

 

Limitations of Operator Overloading

1. The overloaded operator must have at least one user-defined type.

Vector operator+(const Vector& rhs) cosnt;
 

2. The overloaded operator must keep the number of operands the same.

Vector vector1;

+vector1; // impossible. + operator need 2 operands
          // but still can overload unary operator +
 

3. Can not create new operator symbol.

Vector operator@(const Vector& rhs) cosnt; // impossible
 

4. There are operators that can not be overloaded. ( . , .* , :: , ?: , etc.)

 

 

 

Assignment Operator (implicit and user-defined)

// Vector.h
class Vector
{
private:
    int mX;
    int mY;
};

// Vector.obj
class Vector
{
public:
    Vector() {}
    Vector& operator=(const Vector& rhs) // what the compiler made
    {
        mX = rhs.mX;
        mY = rhs.mY;

        return *this;
    }
};
 

Similar with the copy constructors. Shallow copy. User-defined for deep copy.

 

 

 

How to Remove Implicit Functions

(out-of-date)

- Parameterless Constructor

// Vector.h
class Vector
{
public:

private:
    Vector() {}; // not only for removing, but sometimes for design pattern using static function 'Vector::create()'

    int mX;
    int mY;
};

// Main.cpp
Vector v1; // compile error
 

- Destructor :

// Vector.h
class Vector
{
public:

private:
 

- Copy Constructor

- Assignment(=) Operator

// Vector.h
class Vector
{
public:

private:
    const Vector& operator=(const Vector& rhs);

    int mX;
    int mY;
};

// Main.cpp
Vector v1;
Vector v2 = v1; // compile error
 

 

728x90

댓글