码迷,mamicode.com
首页 > 其他好文 > 详细

虚函数的使用

时间:2016-01-21 18:59:17      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream>
#include<string>
#include<stdio.h>


using namespace std;

const int MAX=100;

class Person
{
protected:
    string Name;
public:
    Person();
    Person(string _name)
    {

        Name=_name;
    }

    virtual void Print()
    {
        cout<<"Person"<<" ";
        cout<<Name<<endl;
    }

};

class Student:public Person
{
protected:
    int Number;
public:
    Student(string _name,int _number):Person(_name)
    {
        Number=_number;
    }

    void Print()
    {
        cout<<"Student"<<" ";
        cout<<Name<<" ";
        cout<<Number<<endl;
    }
};

class Teacher:public Person
{
protected:
    int year;
public:
    Teacher(string _name,int _year):Person(_name)
    {
        year=_year;
    }

    void Print()
    {
        cout<<"Teacher"<<" ";
        cout<<Name<<" ";
        cout<<year<<endl;
    }
};

class Graduate:public Student
{
protected:
    string Reserach;
public:
    Graduate(string _name,int _number,string _reserach):Student(_name,_number)
    {

        Reserach=_reserach;
    }//将基类构造函数的参数都写过来

    void Print()
    {   cout<<"Graduate"<<" ";
        cout<<Name<<" ";
        cout<<Number<<" ";
        cout<<Reserach<<endl;
    }
};

int Switch(string str)
{
    int i,accept,j;
    int store[MAX];
    for(i=0;str[i]!=‘\0‘;i++)//string的实质是char,注意让他不等于‘\0‘
    {
        store[i]=str[i]-‘0‘;
    }
    accept=0;
    j=1;
    i--;
    while(i>=0)
    {
        accept+=store[i]*j;
        j*=10;
        i--;//字符转化为数字
    }
	/*
		转载请注明出处:去转盘网www.quzhuanpan.com
	*/
    return accept;
}

int main()
{

    //Person *p[MAX];浪费内存,不采用
    int total,number,year;
    string name,job,research,str;
    cin>>total;
    Person **ptr=new Person*[total];
    for(int i=0;i<total;i++)
    {
        cin>>job;
        if(job=="Person")
        {
            cin>>name;
            ptr[i]=new Person(name);
        }

       else if(job=="Student")
        {
            cin>>name;
            cin>>number;
            ptr[i]=new Student(name,number);
        }
        else if(job=="Graduate")
        {
            cin>>name;
            cin>>number;
            cin>>research;
            ptr[i]=new Graduate(name,number,research);
        }
        else if(job=="Teacher")
        {
            cin>>name;
            cin>>year;
            ptr[i]=new Teacher(name,year);
        }

        else
        {

        }

    }
	/*
		转载请注明出处:去转盘网www.quzhuanpan.com
	*/

    while(1)
    {
        cin>>str;
        if(str=="exit")
        {
            break;
        }
        else if(Switch(str)<total)
        {
            ptr[Switch(str)]->Print();
        }
        else
        {
            ;
        }
        //ptr[Switch("1")]->Print();

    }
    delete []ptr;
    return 0;
}

  虚函数的使用,做个存档

虚函数的使用

标签:

原文地址:http://www.cnblogs.com/worm0707/p/5148755.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!