0% found this document useful (0 votes)
184 views2 pages

C++ Unit 3

Unit 3 covers key concepts of Object-Oriented Programming in C++, focusing on operator overloading, inheritance, and abstract classes. It explains how to overload unary and binary operators with examples, and discusses the types of inheritance and access specifiers. Additionally, it highlights the importance of virtual base classes and abstract classes in managing class behavior and polymorphism.

Uploaded by

sahilsahil69221
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
184 views2 pages

C++ Unit 3

Unit 3 covers key concepts of Object-Oriented Programming in C++, focusing on operator overloading, inheritance, and abstract classes. It explains how to overload unary and binary operators with examples, and discusses the types of inheritance and access specifiers. Additionally, it highlights the importance of virtual base classes and abstract classes in managing class behavior and polymorphism.

Uploaded by

sahilsahil69221
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Unit 3 Notes: OOPs with C++

UNIT 3 (15 HOURS)

Operator Overloading:
Operator overloading allows redefining the behavior of operators for user-defined types (like classes). It gives
a special meaning to an operator for a class.

Syntax for Operator Overloading:


return_type class_name::operator op (argument_list) {
// body of the operator function
}

Overloading Unary Operators:


Unary operators operate on a single operand. Common unary operators: ++, --, -, !, etc.

Example:
class Counter {
int count;
public:
Counter() : count(0) {}
void operator++() { ++count; }
void display() { cout << "Count: " << count << endl; }
};

Overloading Binary Operators:


Binary operators work on two operands. Common binary operators: +, -, *, /, etc.

Example:
class Complex {
float real, imag;
public:
Complex() : real(0), imag(0) {}
Complex(float r, float i) : real(r), imag(i) {}
Complex operator+(Complex c) {
return Complex(real + c.real, imag + c.imag);
}
void display() { cout << real << " + " << imag << "i" << endl; }
};

Notes:
- Cannot overload: ::, ., .*, sizeof, typeid, etc.
- Precedence & associativity unchanged.

Page 1
Unit 3 Notes: OOPs with C++

Inheritance:
Inheritance enables creating new classes that reuse, extend, and modify the behavior of existing classes.

Key Concepts:
- Base class: the existing class.
- Derived class: the new class.

Syntax:
class Derived : access_specifier Base { ... };

Access Specifiers:
- public: public and protected remain public/protected.
- protected: public and protected become protected.
- private: public and protected become private.

Types of Inheritance:
- Single, Multiple, Multilevel, Hierarchical, Hybrid.

Virtual Base Class (Diamond Problem):


class A { ... };
class B : virtual public A { ... };
class C : virtual public A { ... };
class D : public B, public C { ... };

Abstract Class:
Contains at least one pure virtual function:
virtual void show() = 0;

Deep Notes:
- Operator overloading should be intuitive & consistent.
- Use friend functions when needed for two operands.
- protected inheritance: allows derived class access, but hides from outside.
- Virtual base class prevents duplication in multiple inheritance.
- Abstract classes provide common interface, used for polymorphism.

Page 2

You might also like