C++ and Object-Oriented Programming
Operator Overloading; Inheritance
C-CAT
Operator Overloading
What is Operator Overloading?
Redefining existing C++ operators to work with user-defined types (classes).
Syntax:
return_type operator<symbol>(parameters) {
// implementation
}
Operators that CAN be overloaded:
+ - * / % ^ & | ~ ! < > <= >= == != && || << >> ++ -- = += -= *= /= [] () -> and more.
Operators that CANNOT be overloaded:
. :: .* ?:
Complex Number with Operator Overloading
#include <iostream>
using namespace std;
class Complex {
private:
double real, imag;
public:
Complex(double r = 0,
double i = 0) : real(r), imag(i) {}
// Overload + operator
Complex
operator+(const Complex &other) const {
return Complex(real + other.real, imag +
other.imag);
}
// Overload - operator
Complex operator-(const Complex
&other) const {
return Complex(real - other.real, imag - other.imag);
}
// Overload * operator
Complex operator*(const Complex &other) const {
return
Complex(real * other.real - imag * other.imag,
real * other.imag +
imag * other.real);
}
// Overload == operator
bool operator==(const Complex
&other) const {
return (real == other.real && imag == other.imag);
}
//
Overload << for output (as friend function)
friend ostream& operator<<(ostream &out,
const Complex &c) {
if (c.imag >= 0)
out << c.real << " + " << c.imag <<
"i";
else
out << c.real << " - " << (-c.imag) << "i";
return
out;
}
// Overload >> for input
friend istream& operator>>(istream &in,
Complex &c) {
cout << "Enter real and imaginary: ";
in >> c.real >> c.imag;
return in;
}
};
int main() {
Complex c1(3, 4), c2(1, 2);
Complex c3 = c1 + c2;
cout << c1 << " + " << c2 << " = " << c3 << endl; // 3+4i + 1+2i = 4+6i
Complex c4 = c1 * c2;
cout << c1 << " * " << c2 << " = " << c4 << endl; // (3+4i)*(1+2i) = 3+6i+4i+8i² = -5+10i
if (c1 == c2) cout << "Equal\n";
else cout << "Not equal\n";
return 0;
}
Prefix and Postfix ++ Overloading
class Counter {
private:
int value;
public:
Counter(int v = 0) : value(v) {}
// Prefix ++ (++obj)
Counter operator++() {
++value;
return *this;
}
// Postfix ++ (obj++) — int dummy parameter distinguishes from prefix
Counter operator++(int) {
Counter temp = *this; // save current state
value++;
return temp; // return old value
}
int get() { return value; }
};
Inheritance
What is Inheritance?
Inheritance is the mechanism by which a new class (derived class / child) acquires properties and behaviors from an existing class (base class / parent).
Benefits:
- Code reusability — don't rewrite existing code
- Extensibility — add new features without modifying existing
- Hierarchy — model real-world relationships
Basic Inheritance Syntax
// Base class (parent)
class Animal {
protected:
string name;
int age;
public:
Animal(string n, int a) : name(n), age(a) {}
void eat() { cout << name << " is eating\n"; }
void sleep() { cout << name << " is sleeping\n"; }
virtual void speak() { // virtual — can be overridden
cout << name << " makes a sound\n";
}
virtual ~Animal() {} // virtual destructor!
};
// Derived class (child)
class Dog : public Animal {
private:
string breed;
public:
Dog(string n, int a, string b) : Animal(n, a), breed(b) {}
// Override parent
method
void speak() override {
cout << name << " says: Woof! Woof!\n";
}
// New method only in Dog
void fetch() { cout << name << " is fetching!\n"; }
};
class Cat : public Animal {
public:
Cat(string n, int a) : Animal(n, a) {}
void
speak() override {
cout << name << " says: Meow!\n";
}
};
int main() {
Dog d("Rex", 3, "German Shepherd");
Cat c("Whiskers", 2);
d.eat(); // inherited from Animal
d.speak(); // Rex says: Woof! Woof!
d.fetch(); // Dog-specific
c.speak(); // Whiskers says: Meow!
return 0;
}
Access Specifiers in Inheritance
| Base Member | public inheritance | protected inheritance | private inheritance |
|---|---|---|---|
| public | public | protected | private |
| protected | protected | protected | private |
| private | Not inherited | Not inherited | Not inherited |
Continue learning
Related notes
Definition of AI; Need of AI
Artificial Intelligence
Introduction to Data Engineering; Big Data — The 5 V's; Types of Data
Big Data and Data Engineering
Introduction to C Programming; C Program Structure; Data Types and Variables
C Programming
What Is a Computer?; Machine Cycle: Fetch–Decode–Execute; CPU Organization
Computer Architecture
Put this topic into timed practice
Open mock tests when you want full-exam pacing, or keep drilling in practice mode.