指向基类的指针,为何只能访问来自基类成员?!
#include <iostream> #include <string> using namespace std; //声明基类Student class Student { public: Student(int, string,float);//声明构造函数 void display( ); //声明输出函数 protected: //受保护成员,派生类可以访问 int num; string name; float score; }; //Student类成员函数的实现 Student::Student(int n, string nam,float s) { num=n; //定义构造函数 name=nam; score=s; } void Student::display( ) //定义输出函数 { cout<<"num:"<<num<<"\nname:"<<name<<"\nscore:"<<score<<"\n\n"; } //声明公用派生类Graduate class Graduate:public Student { public: Graduate(int, string, float, float); //声明构造函数 void display( ); //声明输出函数 private: float pay; }; // Graduate类成员函数的实现 void Graduate::display( ) //定义输出函数 { cout<<"num:"<<num<<"\nname:"<<name<<"\nscore:"<<score<<"\npay="<<pay<<endl; } Graduate::Graduate(int n, string nam,float s,float p):Student(n,nam,s),pay(p) { } //主函数 int main() { Student stud1(1001,"Li",87.5); //定义Student类对象stud1 Graduate grad1(2001,"Wang",98.5,563.5); //定义Graduate类对象grad1 Student *pt=&stud1; //定义指向基类对象的指针变量pt pt->display( ); pt=&grad1; pt->display( ); return 0; }
一招虚函数,从此出樊笼
#include <iostream> #include <string> using namespace std; //声明基类Student class Student { public: Student(int, string,float);//声明构造函数 virtual void display( ); //声明输出函数 protected: //受保护成员,派生类可以访问 int num; string name; float score; }; //Student类成员函数的实现 Student::Student(int n, string nam,float s) { num=n; //定义构造函数 name=nam; score=s; } void Student::display( ) //定义输出函数 { cout<<"num:"<<num<<"\nname:"<<name<<"\nscore:"<<score<<"\n\n"; } //声明公用派生类Graduate class Graduate:public Student { public: Graduate(int, string, float, float); //声明构造函数 void display( ); //声明输出函数 private: float pay; }; // Graduate类成员函数的实现 void Graduate::display( ) //定义输出函数 { cout<<"num:"<<num<<"\nname:"<<name<<"\nscore:"<<score<<"\npay="<<pay<<endl; } Graduate::Graduate(int n, string nam,float s,float p):Student(n,nam,s),pay(p) { } //主函数 int main() { Student stud1(1001,"Li",87.5); //定义Student类对象stud1 Graduate grad1(2001,"Wang",98.5,563.5); //定义Graduate类对象grad1 Student *pt=&stud1; //定义指向基类对象的指针变量pt pt->display( ); pt=&grad1; pt->display( ); return 0; }
对比:未用虚函数
#include <iostream> using namespace std; class Circle { public: Circle(double r=0):radius(r) { } double area ( ) const { return 3.14159*radius*radius; } protected: double radius; }; class Cylinder:public Circle { public: Cylinder (double r=0,double h=0):Circle(r),height(h) {} double area() const { return 2*Circle::area( )+2*3.14159*Circle::radius*height; }; protected: double height; }; int main( ) { Circle c1(5.2); Cylinder cy1(5.2, 10); Circle *pc=&c1; cout<<pc->area()<<endl; pc = &cy1; cout<<pc->area()<<endl; return 0; }
对比:用了虚函数
#include <iostream> using namespace std; class Circle { public: Circle(double r=0):radius(r) { } double area ( ) const { return 3.14159*radius*radius; } protected: double radius; }; class Cylinder:public Circle { public: Cylinder (double r=0,double h=0):Circle(r),height(h) {} double area() const { return 2*Circle::area( )+2*3.14159*Circle::radius*height; }; protected: double height; }; int main( ) { Circle c1(5.2); Cylinder cy1(5.2, 10); Circle *pc=&c1; cout<<pc->area()<<endl; pc = &cy1; cout<<pc->area()<<endl; return 0; }
事情本就该这样——抽象的动物不会叫
#include <iostream> using namespace std; class Animal { public: virtual void cry() { cout<<"咋叫?"<<endl; } }; class Dog: Animal { public: void cry() { cout<<"汪!"<<endl; } }; class Cat: Animal{...}; class Mouse: Animal{...}; ... int main( ) { Animal *p, a; p = &a; p->cry(); Dog d; p = &d; p->cry(); Cat c; p = &c; p->cry(); Mouse m; p = &m; p->cry(); return 0; }
wxWidgets中利用虚函数
class MyApp : public wxApp { public: // Called on application startup virtual bool OnInit(); }; class WXDLLIMPEXP_CORE wxApp : public wxAppBase { public: wxApp(); virtual ~wxApp(); // override base class (pure) virtuals virtual bool Initialize(int& argc, wxChar **argv); virtual void CleanUp(); …… };
时间: 2024-10-25 05:05:12