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;
}
时间: 2024-08-24 17:46:26

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++语言基础 例程 应用系统开发:银行储蓄系统

贺老师的教学链接  本课讲解 说明:(1)下面的代码,只演示了利用链表作为存储结构的可选处理方法,本讲提到的其他方面的拓展,请感兴趣做下去的同学自行使用相关技术组合起来,形成一个完整的系统.(2)运行程序,登录用户名和密码,请阅读程序,从程序中找出.建议建立多文件项目,将代码拷贝到IDE中看.(3)本程序由我的2011级学生刘镇参加企业组织的实训中完成,原文在:点击打开链接 Record.h #ifndef HEADER_RECORD //条件编译 #define HEADER_RECORD #

C++语言基础 例程 C++中的输入和输出

贺老师的教学链接 程序将自行识别符号 #include <iostream> using namespace std; int main() { int a,b; char op; cin>>a>>op>>b cout<<"result: "<<'\n'; cout<<"a: "<<a<<'\n'; cout<<"b: "<

C++语言基础 例程 重载流插入运算符和流提取运算符

贺老师的教学链接  本课讲解 重载流插入运算符"<<" #include <iostream> using namespace std; class Complex { public: Complex( ) { real=0; imag=0; } Complex(double r,double i) { real=r; imag=i; } Complex operator + (Complex &c2); //运算符"+"重载为成员函

C++语言基础 例程 文本文件的读写

贺老师的教学链接  本课讲解 示例:将数据写入ASCII文件 #include <iostream> #include <fstream> #include <cstdlib> using namespace std; int main( ) { int a[10]; ofstream outfile("f1.dat",ios::out);//定义文件流对象,打开磁盘文件"f1.dat" if(!outfile) //如果打开失败

C++语言基础 例程 标准输入流

贺老师的教学链接  本课讲解 例: 输入个数不确定的成绩 #include <iostream> using namespace std; int main( ) { float grade; cout<<"enter grade:"; while(cin>>grade)//能从cin流读取数据 { if(grade>=85) cout<<grade<<" GOOD!"<<endl; if

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> #include <fstream> #include <cstdlib> using namespace std; typedef struct { int NO; char name[8]; int chinese; int math; int english; int Comprehensive; int total; } Stude

C++语言基础 例程 文件的随机读写

贺老师的教学链接  本课讲解 示例:写到尾再从头读 #include <iostream> #include <fstream> #include <cstdlib> using namespace std; int main( ) { int a[10], b[10]; fstream iofile("f1.dat",ios::in|ios::out); if(!iofile) { cerr<<"open error!&quo