Document
Document
private specifiers:
```
class Base {
private:
int privateVar;
public:
};
public:
};
int main() {
Derived obj(10);
obj.printPrivateVar(); // Outputs: 10
return 0;
```
```
class Shape {
private:
int privateVar;
public:
};
public:
};
int main() {
delete shape;
return 0;
```
```
class Base {
public:
};
public:
};
int main() {
Derived obj;
return 0;
```
```
class Base {
protected:
int protectedVar;
public:
};
public:
Derived(int val) : Base(val) {}
};
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:
```
class Base {
private:
int privateVar;
public:
};
public:
Derived(int val) : Base(val) {}
};
int main() {
Derived obj(10);
obj.printPrivateVar(); // Outputs: 10
return 0;
```
```
class Shape {
private:
int privateVar;
public:
};
public:
};
int main() {
delete shape;
return 0;
```
```
class Base {
public:
};
public:
};
int main() {
Derived obj;
return 0;
```
# Protected Access Specifier
```
class Base {
protected:
int protectedVar;
public:
};
public:
};
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.
```
T max(T a, T b) {
return (a > b) ? a : b;
int main() {
return 0;
```
# Class Templates
```
class Container {
private:
T value;
public:
};
int main() {
Container<int> intContainer(10);
Container<double> doubleContainer(10.5);
Container<char> charContainer('a');
return 0;
```
# Virtual Functions
```
class Shape {
public:
};
public:
};
};
int main() {
Shape* shapes[2];
shapes[i]->draw();
delete shapes[0];
delete shapes[1];
return 0;
```
# Abstract Classes
```
class AbstractClass {
public:
public:
};
int main() {
ConcreteClass obj;
return 0;
```
# Interfaces
```
class Interface {
public:
};
public:
};
int main() {
Implementation obj;
return 0;
```
# Function Overloading
```
Class Calculator {
Public:
Return a + b;
}
Double add(double a, double b) {
Return a + b;
Return a + b + c;
};
Int main() {
Calculator calc;
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:
Public:
};
Public:
};
Int main() {
Shape* shapes[2];
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:
Std::cout << real << “ + “ << imag << “I” << std::endl;
};
Int main() {
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.