Virtual function defined, how to define a destructor?
Aug 22, 2014 at 10:24pm UTC
I wrote the following program, it can be compiled and run, but there is warning saying that if virtual function is defined, there should be a destructor. How to do that I tried many different ways I can thought of, but none of them works. Please help me complete this programming, make it no warnings......
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include <iostream>
using namespace std;
class cell_c {
public :
double p;
cell_c() {p=1;}
virtual void print() {cout<<p<<endl;}
// virtual ~cell_c();
// virtual void ~print()=default;
};
class uvcell_c:public cell_c {
public :
double q;
uvcell_c() {q=0;}
inline void print() {cout<<p<<q<<endl;}
// virtual ~uvcell_c();
};
int main() {
cell_c c;
uvcell_c uv;
c.print();
uv.print();
cell_c & cuv=uv;
cuv.print();
return 0;
}
Thanks!
Aug 23, 2014 at 1:42am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#include <iostream>
class cell_c {
public :
double p;
cell_c() {p = 1;}
virtual ~cell_c() = default ; // virtual ~cell_c() {}
virtual void print() const { std::cout << p << '\n' ; }
};
class uvcell_c: public cell_c {
public :
double q;
uvcell_c() { q = 0; }
inline virtual void print() const override
{ std::cout << p << ' ' << q << '\n' ;}
};
int main() {
cell_c c;
uvcell_c uv;
c.print();
uv.print();
cell_c & cuv = uv;
cuv.print();
}
Topic archived. No new replies allowed.