为了账号安全,请及时绑定邮箱和手机立即绑定

C++父类函数前加了virtual为什么还是没办法覆盖?

C++父类函数前加了virtual为什么还是没办法覆盖?

C++
半盏香茗 2016-07-31 10:35:38
1   //main.cpp#include"rect.h"#include<iostream>using namespace std;rect::rect(double x,double y){ height = x; longs = y; cout<<"rect"<<endl; }rect::~rect(){ cout<<"~rect"<<endl;} double rect::calc(){ cout<<"rect->calc()"<<endl; return height*longs;}2    //shape.h#ifndef SHAPE_H#define SHAPE_Hclass shape{ public: shape(); ~shape(); virtual double calc(); };#endif3//shape.cpp#include"shape.h"#include<iostream>using namespace std;shape::shape(){ cout<<"shape"<<endl;}shape::~shape(){ cout<<"~shape"<<endl;} double shape::calc(){ cout<<"shape->calc()"<<endl; }  4//circle.h#ifndef CIRCLE_H#define CIRCLE_H#include"shape.h"class circle:public shape{ public: circle(double r); ~circle(); double calc(); protected: double m_dR; }; #endif5//circle.cpp#include"circle.h"#include<iostream>using namespace std;double circle::calc(){ cout<<"circle->calc()"<<endl; return 3.14*m_dR*m_dR;}circle::circle(double r){ m_dR = r; cout<<"circle"<<endl;}circle::~circle(){ cout<<"~circle"<<endl;}6/rect.h#ifndef RECT_H#define RECT_H#include"shape.h"class rect:public shape{ public: rect(double x,double y); ~rect(); double calc(); protected: double height; double longs;}; #endif7//rect.cpp#include"rect.h"#include<iostream>using namespace std;rect::rect(double x,double y){ height = x; longs = y; cout<<"rect"<<endl; }rect::~rect(){ cout<<"~rect"<<endl;} double rect::calc(){ cout<<"rect->calc()"<<endl; return height*longs;}
查看完整描述

1 回答

?
皓韵儿

TA贡献86条经验 获得超114个赞

你代码粘的有问题吧,没有主函数。。。。还是???

查看完整回答
反对 回复 2016-08-05
  • 1 回答
  • 0 关注
  • 1784 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信