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

Poly

The document contains two C++ code examples demonstrating object-oriented programming concepts. The first example illustrates operator overloading with a Complex class for handling complex numbers, while the second example showcases inheritance and polymorphism with Base and Derived classes. Both examples include main functions that create objects and call their respective methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Poly

The document contains two C++ code examples demonstrating object-oriented programming concepts. The first example illustrates operator overloading with a Complex class for handling complex numbers, while the second example showcases inheritance and polymorphism with Base and Derived classes. Both examples include main functions that create objects and call their respective methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

using namespace std;

class Complex {

private:

float real;

float imag;

public:

Complex() : real(0), imag(0) {}

Complex(float r, float i) : real(r), imag(i) {}

// Operator overloading

Complex operator + (const Complex& obj) {

Complex temp;

temp.real = real + obj.real;

temp.imag = imag + obj.imag;

return temp;

void display() {

cout << "Real: " << real << " Imaginary: " << imag << endl;

};

int main() {

Complex c1(3.5, 2.5);

Complex c2(1.5, 4.5);

Complex c3;

c3 = c1 + c2;

c3.display();

return 0;

}
#include <iostream>

using namespace std;

class Base {

public:

virtual void show() {

cout << "Base class show function." << endl;

void display() {

cout << "Base class display function." << endl;

};

class Derived : public Base {

public:

void show() {

cout << "Derived class show function." << endl;

void display() {

cout << "Derived class display function." << endl;

};

int main() {

Base* basePtr;

Derived d;

basePtr = &d;

basePtr->show(); // Calls Derived class show function

basePtr->display(); // Calls Base class display function

return 0;

You might also like