写一个c++多态的代码
时间: 2023-03-19 10:21:05 浏览: 109
#include <stdio.h>
int main()
{
// 定义基类指针
void* base_ptr;
int a = 10;
float b = 20.45;
char c = 'G';
// 将基类指针指向变量a
base_ptr = &a;
printf("整型变量a的值: %d\n", *(int*)base_ptr);
// 将基类指针指向变量b
base_ptr = &b;
printf("浮点变量b的值: %f\n", *(float*)base_ptr);
// 将基类指针指向变量c
base_ptr = &c;
printf("字符变量c的值: %c\n", *(char*)base_ptr);
return 0;
}
相关问题
c++多态代码实现
C++中多态的实现方式主要有以下几种:
1. 虚函数(Virtual Functions):通过在基类中声明虚函数,并在派生类中进行重写,实现多态性。当通过基类指针或引用调用虚函数时,会根据实际对象的类型来确定调用哪个函数。
示例代码如下:
```cpp
class Animal {
public:
virtual void sound() {
cout << "Animal makes a sound." << endl;
}
};
class Dog : public Animal {
public:
void sound() {
cout << "Dog barks." << endl;
}
};
class Cat : public Animal {
public:
void sound() {
cout << "Cat meows." << endl;
}
};
int main() {
Animal* animal1 = new Dog();
Animal* animal2 = new Cat();
animal1->sound(); // 输出:Dog barks.
animal2->sound(); // 输出:Cat meows.
delete animal1;
delete animal2;
return 0;
}
```
2. 纯虚函数(Pure Virtual Functions):在基类中声明纯虚函数,派生类必须实现该函数。纯虚函数使得基类成为抽象类,无法实例化,只能作为接口使用。
示例代码如下:
```cpp
class Shape {
public:
virtual void draw() = 0; // 纯虚函数
};
class Circle : public Shape {
public:
void draw() {
cout << "Drawing a circle." << endl;
}
};
class Rectangle : public Shape {
public:
void draw() {
cout << "Drawing a rectangle." << endl;
}
};
int main() {
Shape* shape1 = new Circle();
Shape* shape2 = new Rectangle();
shape1->draw(); // 输出:Drawing a circle.
shape2->draw(); // 输出:Drawing a rectangle.
delete shape1;
delete shape2;
return 0;
}
```
3. 虚析构函数(Virtual Destructors):当基类指针指向派生类对象时,如果基类的析构函数不是虚函数,那么只会调用基类的析构函数,而不会调用派生类的析构函数。为了正确释放派生类对象的资源,需要将基类的析构函数声明为虚函数。
示例代码如下:
```cpp
class Base {
public:
virtual ~Base() {
cout << "Base destructor." << endl;
}
};
class Derived : public Base {
public:
~Derived() {
cout << "Derived destructor." << endl;
}
};
int main() {
Base* base = new Derived();
delete base; // 输出:Derived destructor. Base destructor.
return 0;
}
```
c++ 利用多态写一段代码
以下是一个简单的C++多态示例:
```c++
#include <iostream>
using namespace std;
class Shape {
protected:
int width, height;
public:
Shape( int a = 0, int b = 0) {
width = a;
height = b;
}
virtual int area() {
cout << "Parent class area :" <<endl;
return 0;
}
};
class Rectangle: public Shape {
public:
Rectangle( int a = 0, int b = 0):Shape(a, b) { }
int area () {
cout << "Rectangle class area :" <<endl;
return (width * height);
}
};
class Triangle: public Shape{
public:
Triangle( int a = 0, int b = 0):Shape(a, b) { }
int area () {
cout << "Triangle class area :" <<endl;
return (width * height / 2);
}
};
int main() {
Shape *shape;
Rectangle rec(10,7);
Triangle tri(10,5);
shape = &rec;
// 调用 Rectangle 的 area 函数
shape->area();
shape = &tri;
// 调用 Triangle 的 area 函数
shape->area();
return 0;
}
```
输出:
```
Rectangle class area :
Triangle class area :
```
在这个示例中,我们定义了一个基类 `Shape`,并派生出两个子类 `Rectangle` 和 `Triangle`。在基类中,我们定义了一个虚函数 `area()`,并在每个子类中重写了它。在 `main()` 函数中,我们声明了一个指向 `Shape` 的指针 `shape`,并将其分别指向 `Rectangle` 和 `Triangle` 对象。然后我们调用 `shape` 的 `area()` 函数,由于 `area()` 是虚函数,程序会根据实际指向的对象类型来调用正确的函数,这就是多态的体现。