体验虚基类

本文通过一个简单的C++程序示例介绍了虚基类的概念及其使用方法。该程序定义了一个Student基类和一个Graduate派生类,并展示了如何利用虚继承来避免二义性问题。
/*烟台大学计算机学院学生 
*All right reserved. 
*文件名称:体验虚基类
*作者:杨飞 
*完成日期:2014年5月2日 
*版本号:v1.0 
*对任务及求解方法的描述部分:体验虚基类
*我的程序:*/  
#include <iostream>
#include <cstring>
using namespace std;
class Student
{
public:
    Student(int,string ,float);
  virtual void display();
protected:
    int num;
    string name;
    float score;
};
Student::Student(int num1,string name1,float score1)
{
    num=num1;
    name=name1;
    score=score1;
}
void Student::display()
{
    cout<<"num:"<<num<<endl;
    cout<<"name:"<<name<<endl;
    cout<<"score:"<<score<<endl;
}
class Graduate:public Student
{
public:
    Graduate(int num1,string name1,float score1,float wage1):
        Student(num1,name1,score1),wage(wage1){}
     void display();
private:
    float wage;
};
void  Graduate::display()
{
    cout<<"num:"<<num<<endl;
    cout<<"name:"<<name<<endl;
    cout<<"score:"<<score<<endl;
    cout<<"wage:"<<wage<<endl;
}
int main()
{
    cout<<"学生信息"<<endl;
    Student stu(1001,"huangren",89);
    Graduate ren(1008,"acsjx",99,1200);
    Student *pt=&stu;
    pt->display();
    cout<<"研究生信息:"<<endl;
    pt=&ren;
    pt->display();
    return 0;
}

心得体会:呵呵!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值