Share polymorphism in c++

heavyfish512

New member
## Đa hình trong C ++

Đa hình là một khái niệm mạnh mẽ trong lập trình hướng đối tượng (OOP).Nó cho phép bạn tạo các lớp có thể hoạt động theo những cách khác nhau tùy thuộc vào ngữ cảnh.Điều này có thể rất hữu ích để tạo mã linh hoạt và có thể mở rộng.

Trong C ++, tính đa hình đạt được thông qua hai cơ chế: ** Các hàm ảo ** và ** mẫu **.

### Hàm ảo

Một hàm ảo là một hàm có thể được ghi đè trong một lớp dẫn xuất.Điều này có nghĩa là một lớp dẫn xuất có thể cung cấp việc thực hiện chức năng riêng, sẽ được sử dụng thay vì thực hiện trong lớp cơ sở.

Ví dụ, hãy xem xét mã sau:

`` `C ++
cơ sở lớp {
công cộng:
ảo void foo () {
std :: cout << "cơ sở :: foo ()" << std :: endl;
}
};

lớp học có nguồn gốc: cơ sở công cộng {
công cộng:
void foo () ghi đè {
std :: cout << "xuất phát :: foo ()" << std :: endl;
}
};

int main () {
Cơ sở* b = cơ sở mới ();
b-> foo ();// in "cơ sở :: foo ()"

Có nguồn gốc* d = new derive ();
d-> foo ();// in "Derive :: foo ()"
}
`` `

Trong ví dụ này, hàm `foo ()` được khai báo là `ảo 'trong lớp` base`.Điều này có nghĩa là nó có thể được ghi đè trong một lớp dẫn xuất.Lớp `derive` ghi đè hàm` foo () `và khi chúng ta gọi` foo () `trên đối tượng` derive`, việc triển khai hàm `dẫn xuất 'của hàm được gọi.

### Mẫu

Mẫu là một cách để tạo các lớp và chức năng chung.Một mẫu là một bản thiết kế có thể được sử dụng để tạo các loại đối tượng khác nhau.Ví dụ: chúng ta có thể tạo một mẫu cho một vectơ có thể lưu trữ bất kỳ loại dữ liệu nào:

`` `C ++
Mẫu <Typename T>
vector lớp {
công cộng:
Vector () {}
~ Vector () {}

void push_back (t value) {
// ...
}

T & at (int index) {
// ...
}
};
`` `

Sau đó, chúng ta có thể tạo một vectơ số nguyên, một vectơ của chuỗi hoặc một vectơ của bất kỳ loại dữ liệu nào khác.

`` `C ++
Vector <Int> int_Vector;
int_vector.push_back (10);
int_vector.push_back (20);

std :: cout << int_vector.at (0) << std :: endl;// In 10

Vector <std :: String> String_Vector;
String_Vector.push_back ("Xin chào");
String_Vector.push_back ("Thế giới");

std :: cout << String_Vector.at (0) << std :: endl;// in "Xin chào"
`` `

Các mẫu có thể được sử dụng để tạo mã rất mạnh mẽ và linh hoạt.

## hashtags

* #polymorphism
* #C ++
* #lập trình hướng đối tượng
* #templates
* #Generics
=======================================
## Polymorphism in C++

Polymorphism is a powerful concept in object-oriented programming (OOP). It allows you to create classes that can behave in different ways depending on the context. This can be very useful for creating flexible and extensible code.

In C++, polymorphism is achieved through two mechanisms: **virtual functions** and **templates**.

### Virtual functions

A virtual function is a function that can be overridden in a derived class. This means that a derived class can provide its own implementation of the function, which will be used instead of the implementation in the base class.

For example, consider the following code:

```c++
class Base {
public:
virtual void foo() {
std::cout << "Base::foo()" << std::endl;
}
};

class Derived : public Base {
public:
void foo() override {
std::cout << "Derived::foo()" << std::endl;
}
};

int main() {
Base* b = new Base();
b->foo(); // prints "Base::foo()"

Derived* d = new Derived();
d->foo(); // prints "Derived::foo()"
}
```

In this example, the `foo()` function is declared as `virtual` in the `Base` class. This means that it can be overridden in a derived class. The `Derived` class overrides the `foo()` function, and when we call `foo()` on a `Derived` object, the `Derived` implementation of the function is called.

### Templates

Templates are a way to create generic classes and functions. A template is a blueprint that can be used to create different types of objects. For example, we can create a template for a vector that can store any type of data:

```c++
template <typename T>
class Vector {
public:
Vector() {}
~Vector() {}

void push_back(T value) {
// ...
}

T& at(int index) {
// ...
}
};
```

We can then create a vector of integers, a vector of strings, or a vector of any other type of data.

```c++
Vector<int> int_vector;
int_vector.push_back(10);
int_vector.push_back(20);

std::cout << int_vector.at(0) << std::endl; // prints 10

Vector<std::string> string_vector;
string_vector.push_back("hello");
string_vector.push_back("world");

std::cout << string_vector.at(0) << std::endl; // prints "hello"
```

Templates can be used to create very powerful and flexible code.

## Hashtags

* #polymorphism
* #C++
* #object-oriented-programming
* #templates
* #Generics
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top