// test.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <cstdlib>
#include <new>
#include <iostream>
#include <string>
using namespace std;
class Pet {
string pname;
public:
Pet(Pet& p)
{
pname=p.pname;
}
Pet(const string& name) : pname(name) {}
virtual string name() const { return pname; }
virtual string description() const {
return "This is " + pname;
}
};
class Dog : public Pet {
string favoriteActivity;
public:
Dog(const string& name, const string& activity)
: Pet(name), favoriteActivity(activity) {}
string description() const {
return Pet::name() + " likes to " +
favoriteActivity;
}
operator Pet()
{
return *this;
}
};
void describe(Pet p) { // Slices the object
cout << p.description() << endl;
}
int main() {
Pet p("Alfred");
Dog d("Fluffy", "slee
C++对象切片
最新推荐文章于 2024-07-09 10:07:46 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)