| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 883 人关注过本帖
标题:怎样在switch语句后创建类对象??求解
取消只看楼主 加入收藏
跳跳鱼
Rank: 2
等 级:论坛游民
帖 子:74
专家分:60
注 册:2011-5-4
结帖率:93.75%
收藏
已结贴  问题点数:10 回复次数:3 
怎样在switch语句后创建类对象??求解
写了一个学校管理系统,但是到最后选择的时候不太会,怎样在switch语句后创建类对象??求解啊求解
下面上代码代
程序代码:
#include <iostream>
#include <string>
using namespace std;
#define null
class record
{
public:
    record(string nam,string sex,int ca,int bir,string add,int a):name(nam),sex(sex),card(ca),birth(bir),address(add),age(a){}
    void input()
    {
        cout<<"请输入姓名:"<<endl; cin>>name;
        cout<<"请输入性别:"<<endl; cin>>sex;
        cout<<"请输入身份证号:"<<endl; cin>>card;
        cout<<"请输入生日:"<<endl; cin>>birth;
        cout<<"请输入地址:"<<endl; cin>>address;
        cout<<" 请输入年龄:"<<endl; cin>>age;
    }

    void display()
    {
        cout<<"姓名:"<<name<<" "<<"性别:"<<sex<<" "<<"身份证号:"<<card<<endl;
        cout<<"生日:"<<birth<<" "<<"地址:"<<address<<" "<<"年龄:"<<endl;
    }
private:
    string name;
    string sex;
    int card;
    int birth;
    string address;
    int age;
};
class student:public record
{
public:
    student(string nam,string sex,int ca,int bir,string add,int a,int num,int ro,string sub,int gr):record( nam, sex, ca, bir, add, a)
    {number=num;
    room=ro;
    subject=sub;
    grade=gr;
    }
    void inputstu()
    {
        input();
        cout<<"请输入学号:"<<endl; cin>>number;
        cout<<"请输入宿舍号:"<<endl; cin>>room;
        cout<<"请输入专业:"<<endl; cin>>subject;
        cout<<"请输入年级:"<<endl; cin>>grade;
    }
    void showstu()
    {
        display();
        cout<<"学号:"<<number<<" "<<"宿舍号:"<<room<<" "<<"专业:"<<subject<<" "<<"年级:"<<grade<<endl;
    }
private:
    int number;
    int room;
    string subject;
    int grade;
};
class teacher:public record
{
public:
    teacher(string nam,string sex,int ca,int bir,string add,int a,int ap,int wo,int ea,string th,string re,int yanum):
      record(nam,sex,ca,bir,add,a)
      {apartment=ap;
      work=wo;
      earn=ea;
      teach=th;
      research=re;
      yanumber=yanum;
      }
      void inputtea()
      {
          input();
          cout<<"请输入公寓:"<<endl; cin>>apartment;
          cout<<"请输入工作时间:"<<endl; cin>>work;
          cout<<"请输入所教专业:"<<endl; cin>>teach;
          cout<<"请输入科研单位:"<<endl; cin>>research;
          cout<<"请输入所带研究生数目:"<<endl; cin>>yanumber;
      }
      void showtea()
      {
          display();
          cout<<"公寓:"<<apartment<<" "<<"工作时间:"<<work<<" "<<"收入:"<<earn<<" "<<"所教科目:"<<teach<<" "<<"科研名称:"
              <<research<<" "<<"所带研究生数目:"<<yanumber<<endl;
      }
private:
    int apartment;
    int work;
    int earn;
    string teach;
    string research;
    int yanumber;
};
int main()
{   int flag=1;
    student *stu=null;teacher *tea=null;
    while(flag)
    {
        cout<<"================================================="<<endl;
        cout<<"      1 学生通道       2 教师通道                "<<endl;
        cout<<"      3 退出                                     "<<endl;
        cout<<"================================================="<<endl;
        cout<<"请输入您的选择:"<<endl;
        int choice;
        cin>>choice;
        switch(choice)
        {
    case 1: stu=new student();if(stu) *stu->inputstu();cout<<"现在核查一遍信息:"<<endl;*stu->showstu();break;
    case 2: tea=new student();if(tea) *tea->inputtea();cout<<"现在核查一遍信息:"<<endl;*tea->showtea();break;
    case 3: break;
        }
    }
    return 0;
}
搜索更多相关主题的帖子: 系统 null 学校管理 record 
2012-05-23 22:00
跳跳鱼
Rank: 2
等 级:论坛游民
帖 子:74
专家分:60
注 册:2011-5-4
收藏
得分:0 
回复 2楼 hellovfp
你好!谢谢你能给我回复!我已经把你说到的问题都改了一下,学习到了!O(∩_∩)O~但是在构造student空函数的时候老是出现问题
:\c++\MSDev98\MyProjects\实验五\院校管理程序.cpp(40) : error C2512: 'record' : no appropriate default constructor available
执行 cl.exe 时出错.
但是在构造teacher类空函数时却不会出现这种情况,你能帮我看一下吗?谢谢!
下面是代码代
程序代码:
#include <iostream>
#include <string>
using namespace std;
class record
{
public:
    record(string nam,string sex,int ca,int bir,string add,int a):name(nam),sex(sex),card(ca),birth(bir),address(add),age(a){}
    void input()
    {
        cout<<"请输入姓名:"<<endl; cin>>name;
        cout<<"请输入性别:"<<endl; cin>>sex;
        cout<<"请输入身份证号:"<<endl; cin>>card;
        cout<<"请输入生日:"<<endl; cin>>birth;
        cout<<"请输入地址:"<<endl; cin>>address;
        cout<<" 请输入年龄:"<<endl; cin>>age;
    }

    void display()
    {
        cout<<"姓名:"<<name<<" "<<"性别:"<<sex<<" "<<"身份证号:"<<card<<endl;
        cout<<"生日:"<<birth<<" "<<"地址:"<<address<<" "<<"年龄:"<<endl;
    }
private:
    string name;
    string sex;
    int card;
    int birth;
    string address;
    int age;
};
class student:public record
{
public:
    student(string nam,string sex,int ca,int bir,string add,int a,int num,int ro,string sub,int gr):record( nam, sex, ca, bir, add, a)
    {number=num;
    room=ro;
    subject=sub;
    grade=gr;
    }
    student(){};                   //定义无参的构造函数
    void inputstu()
    {
        input();
        cout<<"请输入学号:"<<endl; cin>>number;
        cout<<"请输入宿舍号:"<<endl; cin>>room;
        cout<<"请输入专业:"<<endl; cin>>subject;
        cout<<"请输入年级:"<<endl; cin>>grade;
    }
    void showstu()
    {
        display();
        cout<<"学号:"<<number<<" "<<"宿舍号:"<<room<<" "<<"专业:"<<subject<<" "<<"年级:"<<grade<<endl;
    }
   
private:
    int number;
    int room;
    string subject;
    int grade;
};
class teacher:public record
{
public:
    teacher(string nam,string sex,int ca,int bir,string add,int a,int ap,int wo,int ea,string th,string re,int yanum):
      record(nam,sex,ca,bir,add,a)
      {apartment=ap;
      work=wo;
      earn=ea;
      teach=th;
      research=re;
      yanumber=yanum;
      }
      teacher(){};                  //定义无参的构造函数
      void inputtea()
      {
          input();
          cout<<"请输入公寓:"<<endl; cin>>apartment;
          cout<<"请输入工作时间:"<<endl; cin>>work;
          cout<<"请输入所教专业:"<<endl; cin>>teach;
          cout<<"请输入科研单位:"<<endl; cin>>research;
          cout<<"请输入所带研究生数目:"<<endl; cin>>yanumber;
      }
      void showtea()
      {
          display();
          cout<<"公寓:"<<apartment<<" "<<"工作时间:"<<work<<" "<<"收入:"<<earn<<" "<<"所教科目:"<<teach<<" "<<"科研名称:"
              <<research<<" "<<"所带研究生数目:"<<yanumber<<endl;
      }
private:
    int apartment;
    int work;
    int earn;
    string teach;
    string research;
    int yanumber;
};
int main()
{   int flag=1;
    student *stu=0;teacher *tea=0;
    while(flag)
    {
        cout<<"================================================="<<endl;
        cout<<"      1 学生通道       2 教师通道                "<<endl;
        cout<<"      3 退出                                     "<<endl;
        cout<<"================================================="<<endl;
        cout<<"请输入您的选择:"<<endl;
        int choice;
        cin>>choice;
        switch(choice)
        {
    case 1: stu=new student();if(stu) stu->inputstu();cout<<"现在核查一遍信息:"<<endl;stu->showstu();break;
    case 2: tea=new teacher();if(tea) tea->inputtea();cout<<"现在核查一遍信息:"<<endl;tea->showtea();break;
    case 3: break;
        }
    }
    return 0;
}
2012-05-24 12:37
跳跳鱼
Rank: 2
等 级:论坛游民
帖 子:74
专家分:60
注 册:2011-5-4
收藏
得分:0 
回复 5楼 hellovfp
好的谢谢啊!我已经改过了。。。。哎,这程序问题怎么这么多,我都不好意思了,代码某错了,但在运行的时候出现了死循环,大哥,能给指点一下不?
2012-05-24 12:58
跳跳鱼
Rank: 2
等 级:论坛游民
帖 子:74
专家分:60
注 册:2011-5-4
收藏
得分:0 
回复 7楼 hellovfp
非常感谢!!!我们老师从来没跟我们说过这些!真的感觉学到好多。。
2012-05-24 13:42
快速回复:怎样在switch语句后创建类对象??求解
数据加载中...
 
   



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

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