多继承,指针在堆上开完空间以后怎么销毁
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string.h>
#include <string>
#include <memory>
using namespace std;//命名空间
class interface1
{
public:
virtual void func1(int a, int b) = 0;
virtual void func3(int a) = 0;
};
class interface2
{
public:
virtual void func2(int a, double b) = 0;
};
class Child:public interface1, public interface2
{
public:
virtual void func1(int a, int b)
{
cout << "func1" << endl;
cout << a << "||" << b << endl;
}
virtual void func3(int a)
{
cout << "func3" << endl;
cout << a << endl;
}
virtual void func2(int a, double b)
{
cout << "func2" << endl;
cout << a << "||" << b << endl;
}
};
void test(interface1* if1, interface2* if2)
{
if1->func1(10, 20);
if1->func3(10.5);
if2->func2(200, 15.6);
if (if1 != NULL)
{
delete if1;
if1 = NULL;
}
if (if2 != NULL)
{
delete if2;
if2 = NULL;
}
}
int main(void)
{
//Child child;
//test(&child, &child);
interface1* if1 = new Child;
interface2* if2 = new Child;
test(if1, if2);
if1 = NULL;
if2 = NULL;
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string.h>
#include <string>
#include <memory>
using namespace std;//命名空间
class interface1
{
public:
virtual void func1(int a, int b) = 0;
virtual void func3(int a) = 0;
virtual ~interface1() {}
};
class interface2
{
public:
virtual void func2(int a, double b) = 0;
virtual ~interface2() {}
};
class Child:public interface1, public interface2
{
public:
virtual void func1(int a, int b)
{
cout << "func1" << endl;
cout << a << "||" << b << endl;
}
virtual void func3(int a)
{
cout << "func3" << endl;
cout << a << endl;
}
virtual void func2(int a, double b)
{
cout << "func2" << endl;
cout << a << "||" << b << endl;
}
};
void test(interface1* if1, interface2* if2)
{
if1->func1(10, 20);
if1->func3(10.5);
if2->func2(200, 15.6);
if (if1 != NULL)
{
delete if1;
if1 = NULL;
}
if (if2 != NULL)
{
delete if2;
if2 = NULL;
}
}
int main(void)
{
//Child child;
//test(&child, &child);
interface1* if1 = new Child;
interface2* if2 = new Child;
test(if1, if2);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string.h>
#include <string>
#include <memory>
using namespace std;//命名空间
class interface1
{
public:
virtual void func1(int a, int b) = 0;
virtual void func3(int a) = 0;
virtual ~interface1()
{
cout << "~interface1()" << endl;
}
};
class interface2
{
public:
virtual void func2(int a, double b) = 0;
virtual ~interface2()
{
cout << "~interface2()" << endl;
}
};
class Child :public interface1, public interface2
{
public:
virtual void func1(int a, int b)
{
cout << "func1" << endl;
cout << a << "||" << b << endl;
}
virtual void func3(int a)
{
cout << "func3" << endl;
cout << a << endl;
}
virtual void func2(int a, double b)
{
cout << "func2" << endl;
cout << a << "||" << b << endl;
}
virtual ~Child()
{
cout << "~Child() " << endl;
}
};
void test(interface1* if1, interface2* if2)
{
if1->func1(10, 20);
if1->func3(10.5);
if2->func2(200, 15.6);
}
int main(void)
{
//Child child;
//test(&child, &child);
interface1* if1 = new Child;
interface2* if2 = new Child;
test(if1, if2);
delete if1;
delete if2;
return 0;
}
需要写一个在父类里面写虚析构函数,测试没有报错了