0% found this document useful (0 votes)
9 views16 pages

Document

The document provides various examples of polymorphism in C++, illustrating the use of public and private access specifiers in classes, inheritance, and function templates. It covers concepts such as function overloading, function overriding, operator overloading, and the implementation of abstract classes and interfaces. Each example includes code snippets demonstrating the respective concepts and their outputs.
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)
9 views16 pages

Document

The document provides various examples of polymorphism in C++, illustrating the use of public and private access specifiers in classes, inheritance, and function templates. It covers concepts such as function overloading, function overriding, operator overloading, and the implementation of abstract classes and interfaces. Each example includes code snippets demonstrating the respective concepts and their outputs.
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/ 16

Here are some more examples of polymorphism in C++ using public and

private specifiers:

# Public and Private Members in Inheritance

```

class Base {

private:

int privateVar;

public:

Base(int val) : privateVar(val) {}

void printPrivateVar() { std::cout << privateVar << std::endl; }

};

class Derived : public Base {

public:

Derived(int val) : Base(val) {}

};

int main() {

Derived obj(10);

obj.printPrivateVar(); // Outputs: 10

return 0;

```

# Public and Private Members in Polymorphism

```
class Shape {

private:

int privateVar;

public:

Shape(int val) : privateVar(val) {}

virtual void draw() = 0;

};

class Circle : public Shape {

public:

Circle(int val) : Shape(val) {}

void draw() override {

std::cout << "Drawing a circle." << std::endl;

};

int main() {

Shape* shape = new Circle(10);

shape->draw(); // Outputs: Drawing a circle.

delete shape;

return 0;

```

# Public and Private Inheritance

```

class Base {
public:

void print() { std::cout << "Base class" << std::endl; }

};

class Derived : private Base {

public:

void printDerived() { print(); }

};

int main() {

Derived obj;

obj.printDerived(); // Outputs: Base class

return 0;

```

# Protected Access Specifier

```

class Base {

protected:

int protectedVar;

public:

Base(int val) : protectedVar(val) {}

};

class Derived : public Base {

public:
Derived(int val) : Base(val) {}

void printProtectedVar() { std::cout << protectedVar << std::endl; }

};

int main() {

Derived obj(10);

obj.printProtectedVar(); // Outputs: 10

return 0;

```

These examples demonstrate the use of public and private access specifiers
in C++ classes, inheritance, and polymorphism. Here are some more
examples of polymorphism in C++ using public and private specifiers:

# Public and Private Members in Inheritance

```

class Base {

private:

int privateVar;

public:

Base(int val) : privateVar(val) {}

void printPrivateVar() { std::cout << privateVar << std::endl; }

};

class Derived : public Base {

public:
Derived(int val) : Base(val) {}

};

int main() {

Derived obj(10);

obj.printPrivateVar(); // Outputs: 10

return 0;

```

# Public and Private Members in Polymorphism

```

class Shape {

private:

int privateVar;

public:

Shape(int val) : privateVar(val) {}

virtual void draw() = 0;

};

class Circle : public Shape {

public:

Circle(int val) : Shape(val) {}

void draw() override {

std::cout << "Drawing a circle." << std::endl;

};
int main() {

Shape* shape = new Circle(10);

shape->draw(); // Outputs: Drawing a circle.

delete shape;

return 0;

```

# Public and Private Inheritance

```

class Base {

public:

void print() { std::cout << "Base class" << std::endl; }

};

class Derived : private Base {

public:

void printDerived() { print(); }

};

int main() {

Derived obj;

obj.printDerived(); // Outputs: Base class

return 0;

```
# Protected Access Specifier

```

class Base {

protected:

int protectedVar;

public:

Base(int val) : protectedVar(val) {}

};

class Derived : public Base {

public:

Derived(int val) : Base(val) {}

void printProtectedVar() { std::cout << protectedVar << std::endl; }

};

int main() {

Derived obj(10);

obj.printProtectedVar(); // Outputs: 10

return 0;

```

These examples demonstrate the use of public and private access specifiers
in C++ classes, inheritance, and polymorphism.

Here are some more examples of polymorphism in C++:


# Function Templates

```

template <typename T>

T max(T a, T b) {

return (a > b) ? a : b;

int main() {

int result1 = max(10, 20); // Outputs: 20

double result2 = max(10.5, 20.7); // Outputs: 20.7

char result3 = max('a', 'z'); // Outputs: z

return 0;

```

# Class Templates

```

template <typename T>

class Container {

private:

T value;

public:

Container(T val) : value(val) {}

T getValue() { return value; }

};
int main() {

Container<int> intContainer(10);

Container<double> doubleContainer(10.5);

Container<char> charContainer('a');

std::cout << intContainer.getValue() << std::endl; // Outputs: 10

std::cout << doubleContainer.getValue() << std::endl; // Outputs: 10.5

std::cout << charContainer.getValue() << std::endl; // Outputs: a

return 0;

```

# Virtual Functions

```

class Shape {

public:

virtual void draw() = 0;

};

class Circle : public Shape {

public:

void draw() override {

std::cout << "Drawing a circle." << std::endl;

};

class Rectangle : public Shape {


public:

void draw() override {

std::cout << "Drawing a rectangle." << std::endl;

};

int main() {

Shape* shapes[2];

shapes[0] = new Circle();

shapes[1] = new Rectangle();

for (int i = 0; i < 2; i++) {

shapes[i]->draw();

delete shapes[0];

delete shapes[1];

return 0;

```

# Abstract Classes

```

class AbstractClass {

public:

virtual void pureVirtualFunction() = 0;


};

class ConcreteClass : public AbstractClass {

public:

void pureVirtualFunction() override {

std::cout << "Implementing pure virtual function." << std::endl;

};

int main() {

ConcreteClass obj;

obj.pureVirtualFunction(); // Outputs: Implementing pure virtual function.

return 0;

```

# Interfaces

```

class Interface {

public:

virtual void function1() = 0;

virtual void function2() = 0;

};

class Implementation : public Interface {

public:

void function1() override {


std::cout << "Implementing function1." << std::endl;

void function2() override {

std::cout << "Implementing function2." << std::endl;

};

int main() {

Implementation obj;

obj.function1(); // Outputs: Implementing function1.

obj.function2(); // Outputs: Implementing function2.

return 0;

```

Here are some example codes that demonstrate polymorphism in C++:

# Function Overloading

```

Class Calculator {

Public:

Int add(int a, int b) {

Return a + b;

}
Double add(double a, double b) {

Return a + b;

Int add(int a, int b, int c) {

Return a + b + c;

};

Int main() {

Calculator calc;

Std::cout << calc.add(2, 3) << std::endl; // Outputs: 5

Std::cout << calc.add(2.5, 3.7) << std::endl; // Outputs: 6.2

Std::cout << calc.add(2, 3, 4) << std::endl; // Outputs: 9

Return 0;

```

In this example, the `Calculator` class has three `add` functions with
different parameter lists. This is an example of function overloading, which is
a form of polymorphism.

# Function Overriding

```

Class Shape {

Public:

Virtual void draw() = 0;


};

Class Circle : public Shape {

Public:

Void draw() override {

Std::cout << “Drawing a circle.” << std::endl;

};

Class Rectangle : public Shape {

Public:

Void draw() override {

Std::cout << “Drawing a rectangle.” << std::endl;

};

Int main() {

Shape* shapes[2];

Shapes[0] = new Circle();

Shapes[1] = new Rectangle();

For (int I = 0; I < 2; i++) {

Shapes[i]->draw();

Delete shapes[0];

Delete shapes[1];
Return 0;

```

In this example, the `Shape` class has a pure virtual function `draw()`, which
is overridden by the `Circle` and `Rectangle` classes. This is an example of
function overriding, which is a form of polymorphism.

# Operator Overloading

```

Class Complex {

Private:

Double real;

Double imag;

Public:

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

Complex operator+(const Complex& other) const {

Return Complex(real + other.real, imag + other.imag);

Void print() const {

Std::cout << real << “ + “ << imag << “I” << std::endl;

};
Int main() {

Complex c1(3, 4);

Complex c2(2, 1);

Complex c3 = c1 + c2;

C3.print(); // Outputs: 5 + 5i

Return 0;

```

In this example, the `Complex` class overloads the `+` operator to add two
complex numbers. This is an example of operator overloading, which is a
form of polymorphism.

You might also like