C++语言基础 例程 虚函数

贺老师的教学链接  本课讲解

指向基类的指针,为何只能访问来自基类成员?!

#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-07-30 08:49:13

C++语言基础 例程 虚函数的相关文章

C++语言基础 例程 虚析构函数

贺老师的教学链接  本课讲解 问题的由来 #include <iostream> using namespace std; class Point { public: Point( ) { } ~Point() { cout<<"executing Point destructor"<<endl; } }; class Circle:public Point { public: Circle( ) { } ~Circle( ) { cout<&

C++语言基础 例程 虚基类及应用

贺老师的教学链接  本课讲解 虚基类应用举例 #include <iostream> #include <cstring> using namespace std; class Person { public: Person(char *nam,char s,int a) //构造函数 { strcpy(name,nam); sex=s; age=a; } protected: //保护成员 char name[20]; char sex; int age; }; class Te

C++语言基础 例程 抽象类

贺老师的教学链接  本课讲解 实例:顶层的Shape作为抽象类 #include <iostream> using namespace std; //声明抽象基类Shape class Shape { public: virtual float area( ) const { return 0.0; //虚函数 } virtual float volume() const { return 0.0; //虚函数 } virtual void shapeName() const =0; //纯虚

C++语言基础 例程 函数中的引用

贺老师的教学链接  本课讲解 引用作为形参 #include<iostream> using namespace std; class Sample { int x; public: Sample(int a): x(a) {cout<<"A";} Sample(Sample &a): x(a.x) {cout<<"B";} int getX(){return x;} }; void disp(Sample &s)

C++语言基础 例程 内置函数

贺老师的教学链接 例:函数指定为内置函数 #include <iostream> using namespace std; inline int max(int,int,int); int main( ) { int i=10,j=20,k=30,m; m=max(i,j,k); cout<<"max="<<m<<endl; return 0; } inline int max(int a,int b,int c) { if(b>a

C++语言基础 例程 函数重载

贺老师的教学链接 重载函数:同名同体,但接口不同 #include <iostream> using namespace std; int max(int a,int b,int c); //函数声明 double max(double a,double b,double c); long max(long a,long b,long c); int main( ) { int i1,i2,i3,i; cin>>i1>>i2>>i3; //输入3个整数 i=

C++语言基础 例程 函数模板

贺老师的教学链接 使用用函数模板 #include <iostream> using namespace std; template<typename T> //模板声明,其中T为类型参数 T max(T a,T b,T c) //定义一个通用函数,用T作虚拟的类型名 { if(b>a) a=b; if(c>a) a=c; return a; } int main( ) { int i1=185,i2=-76,i3=567; double d1=56.87,d2=90.

C++语言基础 例程 有默认参数的函数

贺老师的教学链接 形参/实参.声明/调用/定义 #include <iostream> using namespace std; int max(int a, int b, int c=0);//仅声明时设默认 int main( ) { int a,b,c; cin>>a>>b>>c; cout<<"max(a,b,c)="<<max(a,b,c)<<endl; cout<<"m

C++语言基础 例程 类声明和成员函数定义的分离

贺老师的教学链接  本课讲解 1.一个程序,一个源文件的做法 #include<iostream> #include<cstring> using namespace std; class Student { private: char Name[20]; //学生姓名 double Chinese; //语文成绩 double Math; //数学成绩 public: double Average( );//计算平均成绩 double Sum( ); //计算总分 void Sh