这个星期四要交一个c++小程序,要求用到类和继承,主题不限,程序长200行左右。
网上下的那些应用程序都太长,最关键的是这个星期五要考试,现在忙着复习没时间。哪位大哥帮帮我,把代码发到

解决方案 »

  1.   

    侯sir的mfc深入浅出上有你要的,好几个不很长的例子
      

  2.   

    #include <iostream.h>
    //定义基类
    class Base
    {
    public:
    virtual void NameOf();//定义虚拟函数
    void InvokingClass();//非虚拟函数
    };
    //函数实现
    void Base::NameOf()
    {
    cout << "Base::nameOf()\n";
    }
    void Base::InvokingClass()
    {
    cout << "invoking by Base\n";
    }
    //定义派生类
    class Derived : public Base
    {
    public:
    virtual void NameOf();//虚拟函数
    void InvokingClass();//非虚拟函数
    };
    //函数实现
    void Derived::NameOf()
    {
    cout << "Derived::NameOF\n";
    }
    void Derived::InvokingClass()
    {
    cout << "Invoked by Derived\n";
    }class Derived2 : public Derived
    {
    public:
        void NameOf();//虚拟函数
    void InvokingClass();//非虚拟函数
    };
    void Derived2::NameOf()
    {
        cout << "Derived2::NameOf" << endl;
    }
    void Derived2::InvokingClass()
    {
        cout << "Derived2::InvokingClass" << endl;
    }
    void main()
    {
    //声明继承类对象
    Derived2 aDerived;
    //声明两个指针,一个为 Devried * 类型,一个为Base * 类型
    //将两个指针初始化为指向继承类对象
    Derived *pDerived = &aDerived;
    Derived2 *pDerived2 = &aDerived;
    Base *pBase = &aDerived;
    //调用函数
    pBase->NameOf(); //调用虚拟函数
    pBase->InvokingClass();//调用非虚拟函数
    pDerived->NameOf(); //调用虚拟函数
    pDerived->InvokingClass();//调用非虚拟函数
    pDerived2->NameOf(); //调用虚拟函数
    pDerived2->InvokingClass();//调用非虚拟函数
    }
      

  3.   

    #include <iostream.h>
    //定义基类
    class Base
    {
    public:
    virtual void NameOf();//定义虚拟函数
    void InvokingClass();//非虚拟函数
    };
    //函数实现
    void Base::NameOf()
    {
    cout << "Base::nameOf()\n";
    }
    void Base::InvokingClass()
    {
    cout << "invoking by Base\n";
    }
    //定义派生类
    class Derived : public Base
    {
    public:
    virtual void NameOf();//虚拟函数
    void InvokingClass();//非虚拟函数
    };
    //函数实现
    void Derived::NameOf()
    {
    cout << "Derived::NameOF\n";
    }
    void Derived::InvokingClass()
    {
    cout << "Invoked by Derived\n";
    }class Derived2 : public Derived
    {
    public:
        void NameOf();//虚拟函数
    void InvokingClass();//非虚拟函数
    };
    void Derived2::NameOf()
    {
        cout << "Derived2::NameOf" << endl;
    }
    void Derived2::InvokingClass()
    {
        cout << "Derived2::InvokingClass" << endl;
    }
    void main()
    {
    //声明继承类对象
    Derived2 aDerived;
    //声明两个指针,一个为 Devried * 类型,一个为Base * 类型
    //将两个指针初始化为指向继承类对象
    Derived *pDerived = &aDerived;
    Derived2 *pDerived2 = &aDerived;
    Base *pBase = &aDerived;
    //调用函数
    pBase->NameOf(); //调用虚拟函数
    pBase->InvokingClass();//调用非虚拟函数
    pDerived->NameOf(); //调用虚拟函数
    pDerived->InvokingClass();//调用非虚拟函数
    pDerived2->NameOf(); //调用虚拟函数
    pDerived2->InvokingClass();//调用非虚拟函数
    }
      

  4.   

    wistaria(听风听雨)
    写得不错啊,就用他的德了
      

  5.   

    #include<fstream.h>
    #include<string.h>
    #include<ctype.h>
    #include<stdlib.h>
    #define NULL 0
    class AVLNode{
    public:
    char *english,*chinese;
    AVLNode *left,*right;
    int balance;
    AVLNode():left(NULL),right(NULL),balance(0){}
    AVLNode(char *e,char *c,AVLNode *l=NULL,AVLNode *r=NULL):english(e),
    chinese(c),left(l),right(r){}
    };
    class AVLTree{
    private:
    int RefValue;
    int Insert(AVLNode *&tree,char *e,char *c,int &taller);
    void Delete(AVLNode *&tree,char *e,int &taller);
    void RotateLeft(AVLNode *Tree,AVLNode *&NewTree);
    void RotateRight(AVLNode *Tree,AVLNode *&NewTree);
    void LeftBalance(AVLNode *&Tree,int &taller);
    void RightBalance(AVLNode *&Tree,int &taller);
    public:
    AVLNode *root;
    AVLTree():root(NULL){}
    AVLTree(int Ref):RefValue(Ref),root(NULL){}
    void Traverse(AVLNode *ptr,ostream &out)const;
    AVLNode *Find(char *e,AVLNode *tree);
    int Insert(char *e,char *c);
    void Delete(char *e){int taller;Delete(root,e,taller);}
    friend ostream &operator<<(ostream &out,const AVLTree &Tree);
    };
    AVLNode *AVLTree::Find(char *e,AVLNode *tree){
    if(tree==NULL) return NULL;
    if(strcmp(e,tree->english)<0) return Find(e,tree->left);
    if(strcmp(e,tree->english)>0) return  Find(e,tree->right);
    return tree;
    }
    int AVLTree::Insert(char *e,char *c){
    int taller; 
    return Insert(root,e,c,taller);
    }
    void AVLTree::Delete(AVLNode *&tree,char *e,int &taller){
    if(tree==NULL){
    cout<<"&Atilde;&raquo;&Oacute;&ETH;&Otilde;&acirc;&cedil;&ouml;&micro;&yen;&acute;&Ecirc;!886 :)&iexcl;&shy;&iexcl;&shy;\n";exit(1);

    else if(strcmp(e,tree->english)<0){
    Delete(tree->left,e,taller);
    if(taller){
    switch(tree->balance){
    case -1:tree->balance=0;taller=0;break;
    case 0:tree->balance=1;break;
    case 1:LeftBalance(tree,taller);break;
    }
    }
    }
    else if(strcmp(e,tree->english)>0){
    Delete(tree->right,e,taller);
    if(taller){
    switch(tree->balance){
    case -1:RightBalance(tree,taller);break;
    case 0:tree->balance=-1;break;
    case 1:tree->balance=0;taller=0;break;
    }
    }
    }
    return;
    }
    void AVLTree::RotateLeft(AVLNode *Tree,AVLNode *&NewTree){
    NewTree=Tree->right;
    Tree->right=NewTree->left;
    NewTree->left=Tree;
    }
    void AVLTree::RotateRight(AVLNode *Tree,AVLNode *&NewTree){
    NewTree=Tree->left;
    Tree->left=NewTree->right;
    NewTree->right=Tree;
    }
    void AVLTree::LeftBalance(AVLNode *&Tree,int &taller){
    AVLNode *leftsub=Tree->left,*rightsub;
    switch(leftsub->balance){
    case -1:Tree->balance=leftsub->balance=0;
    RotateRight(Tree,Tree);taller=0;break;
    case 0:;break;//&Atilde;&raquo;&Oacute;&ETH;·&cent;&Eacute;ú&sup2;&raquo;&AElig;&frac12;&ordm;&acirc;
    case 1:rightsub=leftsub->right;
    switch(rightsub->balance){
    case -1:Tree->balance=1;leftsub->balance=0;break;
    case 0:Tree->balance=leftsub->balance=0;break;
    case 1:Tree->balance=0;leftsub->balance=-1;break;
    }
    rightsub->balance=0;
    RotateLeft(leftsub,Tree->left);
    RotateRight(Tree,Tree);taller=0;
    }
    }
    void AVLTree::RightBalance(AVLNode *&Tree,int &taller){
    AVLNode *rightsub=Tree->right,*leftsub;
    switch(rightsub->balance){
    case 1:Tree->balance=rightsub->balance=0;
    RotateLeft(Tree,Tree);taller=0;break;
    case 0:;break;
    case -1:leftsub=rightsub->left;
    switch(leftsub->balance){
    case 1:Tree->balance=-1;rightsub->balance=0;break;
    case 0:Tree->balance=rightsub->balance=0;break;
    case -1:Tree->balance=0;rightsub->balance=1;break;
    }
    leftsub->balance=0;
    RotateRight(rightsub,Tree->right);
    RotateLeft(Tree,Tree);taller=0;
    }
    }
    void AVLTree::Traverse(AVLNode *ptr,ostream &out)const{
    if(ptr!=NULL){
    Traverse(ptr->left,out);
    out<<ptr->english<<" ";
    Traverse(ptr->right,out);
    out<<ptr->english<<" ";/////////////////
    }
    }
    ostream &operator<<(ostream &out,const AVLTree &Tree){
    Tree.Traverse(Tree.root,out);
    out<<endl;
    return out;
    }
    int AVLTree::Insert(AVLNode *&tree,char *e,char *c,int &taller){
    int success;
    if(tree==NULL){
    tree=new AVLNode(e,c);
    success=tree!=NULL?1:0;
    if(success)taller=1;
    }
    else if(strcmp(e,tree->english)<0){
    success=Insert(tree->left,e,c,taller);
    if(taller)
    switch(tree->balance){
    case -1:LeftBalance(tree,taller);break;
    case 0:tree->balance=-1;break;
    case 1:tree->balance=0;taller=0;break;
    }
    }
    else if(strcmp(e,tree->english)>0){
    success=Insert(tree->right,e,c,taller);
    if(taller)
    switch(tree->balance){
    case -1:tree->balance=0;taller=0;break;
    case 0:tree->balance=1;break;
    case 1:RightBalance(tree,taller);break;
    }
    }
    return success;
    }
    void main()
    {
    ifstream infile("&Ecirc;&auml;&Egrave;&euml;&Icirc;&Auml;&frac14;&thorn;.txt");
    if(!infile){
    cout<<"&sup2;&raquo;&Auml;&Uuml;&acute;ò&iquest;&ordf;&Ecirc;&auml;&Egrave;&euml;&Icirc;&Auml;&frac14;&thorn;"<<endl;
    return;
    }
    int N;AVLTree avltree(0);
    cout<<"&Ecirc;&auml;&Egrave;&euml;&micro;&yen;&acute;&Ecirc;&cedil;&ouml;&Ecirc;&yacute;N(&iquest;&acirc;&Ouml;&ETH;&Iuml;&Ouml;&Oacute;&ETH;16&cedil;&ouml;&micro;&yen;&acute;&Ecirc;): ";cin>>N;
    if(isdigit(N))return;
    cout<<"&micro;&yen;&acute;&Ecirc;&iquest;&acirc;&Ouml;&ETH;&Iuml;&Ouml;&Oacute;&ETH;&micro;&Auml;&micro;&yen;&acute;&Ecirc;&pound;&ordm;\n";
    for(int i=0;i<N;i++){
    char *e=new char[5],*c=new char[5];
    infile.read(e,4);
    e[4]='\0';
    cout<<e<<" ";
    infile.read(c,4);
    c[4]='\0';
        avltree.Insert(e,c);
    }   
    infile.close();
    cout<<"\n&Ecirc;&auml;&Egrave;&euml;&Ograve;&ordf;&sup2;é&micro;&Auml;&micro;&yen;&acute;&Ecirc;&pound;&ordm;";
    char *en=new char[5]; cin>>en; en[4]='\0';
    avltree.root=avltree.Find(en,avltree.root);
    if(avltree.root==NULL)
    cout<<"&Atilde;&raquo;&Oacute;&ETH;&Otilde;&acirc;&cedil;&ouml;&micro;&yen;&acute;&Ecirc;&iexcl;&pound;\n";
    else 
    cout<<"&Ouml;&ETH;&Icirc;&Auml;&Ograve;&acirc;&Euml;&frac14;&Icirc;&ordf;&pound;&ordm;"<<avltree.root->chinese<<endl;
    cout<<"&Ograve;&ordf;&Eacute;&frac34;&sup3;&yacute;&micro;&yen;&acute;&Ecirc;&Acirc;&eth;(Y&raquo;òN)? --- ";
    char n;cin>>n;
    if(n=='N'||n=='n')exit(1);
    if(n=='Y'||n=='y'){
    cout<<"&Ecirc;&auml;&Egrave;&euml;&Ograve;&ordf;&Eacute;&frac34;&sup3;&yacute;&micro;&Auml;&micro;&yen;&acute;&Ecirc;:";
    char *eng=new char[5]; cin>>eng; eng[4]='\0';
    avltree.Delete(eng);
    cout<<"&Ograve;&Ntilde;&frac34;&shy;&frac12;&laquo; "<<eng<<"&Eacute;&frac34;&sup3;&yacute;\n";
    cout<<"AVL&Ecirc;÷&Ouml;&ETH;&micro;&Auml;&Ocirc;&ordf;&Euml;&Oslash;&raquo;&sup1;&Oacute;&ETH;&pound;&ordm;\n";
    cout<<avltree;
    }
    else cout<<"&Ecirc;&auml;&Egrave;&euml;&Aacute;&Euml;&acute;í&Icirc;ó·&ucirc;&ordm;&Aring;!\n";
    }
      

  6.   

    TO: huili1725(huili1725) :
    大哥,你的程序是干什么的呀,怎么就输出些字符串。可否清楚一点。
    快帮帮我呀,明天就要交了:(