| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 879 人关注过本帖
标题:急!求助:帮忙写个程序 小弟我刚学C++~谢谢哥姐了!
只看楼主 加入收藏
一旋无风
Rank: 2
等 级:论坛游民
帖 子:55
专家分:92
注 册:2009-11-11
收藏
得分:0 
#include <iostream>
#include<string>
using namespace std;
class base
{
public:
 base(int a,string na)
    {
        age=a;
        name=na;
    }
 ~base(){}
    virtual void display()=0;
protected:
    int age;
    string name;
};

class leader:virtual public  base
{
public:
    leader(int a,string na,string zhi,string dep):base(a,na)
    {
        zhiwei=zhi;
        depart=dep;
    }
    ~leader(){}
void display()
{
cout<<"姓名  "<<name<<"年龄  "<<age<<"职位  "<<zhiwei<<"部门  "<<depart<<endl;
}

protected:
    string zhiwei;
    string depart;
};

class engineer:virtual public base
{
public:
    engineer(int a,string na,string zhic,string car):base(a,na)
    {
        zhicheng=zhic;
        career=car;
    }
    void display()
    {
        cout<<"姓名  "<<name<<"年龄  "<<age<<"职称  "<<zhicheng<<"职业  "<<career<<endl;
    }
    ~engineer(){}
protected:
    string zhicheng;
    string career;
};

class chairman:public leader, public engineer
{
public:
    chairman(int a,string na,string zhi,string dep,string zhic,string car):base(a,na),leader(a,na,zhi,dep),engineer(a,na,zhic,car){}
void display()
{
      cout<<"姓名  "<<name<<"年龄  "<<age<<"职称  "<<zhicheng<<"职业  "<<career<<"职位  "<<zhiwei<<"部门  "<<depart<<endl;
}
};

void main()
{
   
   leader L(28,"三弟","书记","团委");
   L.display();
    engineer e(29,"二哥","助教","电子工程");
    e.display();
    chairman c(40,"一哥","主任","电子工程","教授","办公室");
   c.display();

}
以前没试的,这个试了的,可以运行
2009-11-14 01:29
一旋无风
Rank: 2
等 级:论坛游民
帖 子:55
专家分:92
注 册:2009-11-11
收藏
得分:0 
如过不用string.h的话,就按下面的来
#include <iostream>
#include<cstring>
using namespace std;
class base
{
public:
 base(int a,char* na)
    {
        age=a;
        name=new char[strlen(na)+1];//注意这里,下同
        strcpy (name,na);
    }
 ~base(){delete[] name;}
    virtual void display()=0;
protected:
    int age;
    char* name;
};

class leader:virtual public  base
{
public:
    leader(int a,char* na,char* zhi,char* dep):base(a,na)
    {
        zhiwei=new char[strlen(zhi)+1];
        strcpy (zhiwei,zhi);
        depart=new char[strlen(dep)+1];
        strcpy (depart,dep);
    }
    ~leader(){delete[] zhiwei,depart;}
void display()
{
cout<<"姓名  "<<name<<"  年龄  "<<age<<"  职位  "<<zhiwei<<"  部门  "<<depart<<endl;
}

protected:
    char* zhiwei;
    char* depart;
};

class engineer:virtual public base
{
public:
    engineer(int a,char* na,char* zhic,char* car):base(a,na)
    {
        zhicheng=new char[strlen(zhic)+1];
        strcpy (zhicheng,zhic);
        career=new char[strlen(car)+1];
        strcpy(career,car);
    }
    void display()
    {
        cout<<"姓名  "<<name<<"  年龄  "<<age<<"  职称  "<<zhicheng<<"  职业  "<<career<<endl;
    }
    ~engineer(){delete[]zhicheng,career;}
protected:
    char* zhicheng;
    char* career;
};

class chairman:public leader, public engineer
{
public:
    chairman(int a,char* na,char* zhi,char* dep,char* zhic,char* car):base(a,na),leader(a,na,zhi,dep),engineer(a,na,zhic,car){}
void display()
{
      cout<<"姓名 "<<name<<"  年龄  "<<age<<"  职称  "<<zhicheng<<"  职业  "<<career<<"  职位  "<<zhiwei<<" 部门  "<<depart<<endl;
}
};

void main()
{
   
   leader L(28,"三弟","书记","团委");
   L.display();
    engineer e(29,"二哥","助教","电子工程");
    e.display();
    chairman c(40,"一哥","主任","电子工程","教授","办公室");
   c.display();

}
2009-11-14 01:40
一旋无风
Rank: 2
等 级:论坛游民
帖 子:55
专家分:92
注 册:2009-11-11
收藏
得分:0 
leader(na,a,char* zhi,char* dep):base(a,na)
    {
        zhi=new char[strlen(zhi)+1];//改成zhiwei=new char[strlen(zhi)+1];下面的都一样
        strcpy (zhiwei,zhi);
        dep=new char[strlen(dep)+1];
        strcpy (depart,dep);
    }

 engineer(na,a,char* zhic,char* car):base(a,na)
    {
        zhic=new char[strlen(zhic)+1];
        strcpy (zhicheng,zhic);
        car=new char[strlen(car)+1];
        strcpy(career,car);
    }
这里第一次的忘改了,失误哈
还有就是忘了私有成员不可继承,要改成保护成员,要不然就要设置返回值的公有成员函数,那样的话比较复杂,对我第一次的粗心和不负责道歉哈,后来的这两个都行,你可以试试
2009-11-14 01:51
快速回复:急!求助:帮忙写个程序 小弟我刚学C++~谢谢哥姐了!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.061656 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved