标题:刚学C++帮忙分析一下
只看楼主
梁朝斌
Rank: 4
等 级:业余侠客
帖 子:192
专家分:288
注 册:2012-10-21
结帖率:100%
已结贴  问题点数:100 回复次数:4 
刚学C++帮忙分析一下
#include <iostream.h>
#include <string.h>
class person
{
protected:
    char name[20];
    int sex;
    int    age;
public:
    Person(char *na,int ag,int  se)
    {
        strcpy(name, na);
        sex= se;
        age = ag;
    }
    void initname(char *na)
    {
        strcpy(name, na);
    }

    void initsex(int se)
    {
        sex=se;
    }

    void initage(int ag)
    {
        age=ag;
    }

    char * getname()
    {
        return name;
    }

    int getage()
    {
        return age;
    }

    int getsex()
    {
        return sex;
    }

    void printf()
    {
        cout<<"姓名:"<<name<<endl;
        cout<<"性别:"<<sex<<endl;
        cout<<"年龄:"<<age<<endl;
    }
};
class teacher:public person
{
    int post;
    int  salary;
public:
    teacher(char *na,int ag,int se,int po,int sa):person(na,ag,se)
    {
        post=po;
        salary=sa;
    }
    void printf()
    {
        person::printf();
        cout<<"职位:"<<post<<endl;
        cout<<"工资:"<<salary<<endl;
    }
};
class student:public person
{
    char xuehao[20];
    char Class[20];
public:
    student(char *na,int ag,int se,char *xh,char *cl):person(na,ag,se)
    {
        strcoy(xuehao,xh);
        strcpy(Class,cl);
    }
    printf()
    {
        person::printf();
        cout<<"学号:"<<xuehao<<endl;
        cout<<"班级:"<<Class<<endl;
    }
};
void main()
{
    teacher te("liang",22,男,教授,5000);
    student st("liang",22,男,"20112111225","11B");
    te.printf();
    cout<<"__________________"<<endl;
    st.printf();
}
        

搜索更多相关主题的帖子: void include person public return 
2013-04-24 21:44
邓士林
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:淮河河畔
等 级:贵宾
威 望:61
帖 子:2391
专家分:13384
注 册:2013-3-3
得分:100 
teacher(char *na,int ag,int se,int po,int sa);你看看你的函数参数是多么的不对应; student(char *na,int ag,int se,char *xh,char *cl);参数都不对应;还有你的Person的大小写有的都错了,我给你修改下:
#include <iostream.h>
#include <string.h>
class Person
{
protected:
    char name[20];
    char sex;
    int  age;
public:
    Person(char *na,int ag,char  se)
    {
        strcpy(name, na);
        sex= se;
        age = ag;
    }
    void initname(char *na)
    {
        strcpy(name, na);
    }

    void initsex(char se)
    {
        sex=se;
    }

    void initage(int ag)
    {
        age=ag;
    }

    char * getname()
    {
        return name;
    }

    int getage()
    {
        return age;
    }

    char getsex()
    {
        return sex;
    }

    void printf()
    {
        cout<<"姓名:"<<name<<endl;
        cout<<"性别:"<<sex<<endl;
        cout<<"年龄:"<<age<<endl;
    }
};
class teacher:public Person
{
    char *post;
    int  salary;
public:
    teacher(char *na,int ag,char se,char *po,int sa):Person(na,ag,se)
    {
        post=po;
        salary=sa;
    }
    void printf()
    {
        Person::printf();
        cout<<"职位:"<<post<<endl;
        cout<<"工资:"<<salary<<endl;
    }
};
class student:public Person
{
    char xuehao[20];
    char Class[20];
public:
    student(char *na,int ag,char se,char *xh,char *cl):Person(na,ag,se)
    {
        strcpy(xuehao,xh);
        strcpy(Class,cl);
    }
    printf()
    {
        Person::printf();
        cout<<"学号:"<<xuehao<<endl;
        cout<<"班级:"<<Class<<endl;
    }
};
void main()
{
    teacher te("liang",22,'m',"教授",5000);
    student st("liang",22,'m',"20112111225","11B");
    te.printf();
    cout<<"__________________"<<endl;
    st.printf();
}
        



[ 本帖最后由 邓士林 于 2013-4-24 22:29 编辑 ]

Maybe
2013-04-24 22:03
bme_zxl
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-4-27
得分:0 
1.类的构造函数就是直接把类名拷贝过来就可以了(为了减少像大小写这些错误)
2.为了能让看到输出结果,我一般使用,_getch(),函数,(包含在头文件<conio.h>)
3.注意性别男是字符,显示结果,我用数字0与1分别表示男女了

#include <iostream>
#include <string.h>
#include <conio.h>
using namespace std;
class person
{
protected:
    char name[20];
    int  sex;
    int  age;
public:
 person(char *na,int ag,int  se)
    {
        strcpy(name, na);
        sex= se;
        age = ag;
    }
    void initname(char *na)
    {
        strcpy(name, na);
    }

    void initsex(int se)
    {
        sex=se;
    }

    void initage(int ag)
    {
        age=ag;
    }

    char * getname()
    {
        return name;
    }

    int getage()
    {
        return age;
    }

    int getsex()
    {
        return sex;
    }

    void printf()
    {
        cout<<"姓名:"<<name<<endl;
        cout<<"性别:"<<sex<<endl;
        cout<<"年龄:"<<age<<endl;
    }
};
class teacher:public person
{
    char post[20];
    int  salary;
public:
    teacher(char *na,int ag,int se,char *po,int sa):person(na,ag,se)
    {
        strcpy(post,po);
        salary=sa;
    }
    void printf()
    {
        person::printf();
        cout<<"职位:"<<post<<endl;
        cout<<"工资:"<<salary<<endl;
    }
};
class student:public person
{
    char xuehao[20];
    char Class[20];
public:
    student(char *na,int ag,int se,char *xh,char *cl):person(na,ag,se)
    {
        strcpy(xuehao,xh);
        strcpy(Class,cl);
    }
    void printf()
    {
        person::printf();
        cout<<"学号:"<<xuehao<<endl;
        cout<<"班级:"<<Class<<endl;
    }
};
void main()
{
    teacher te("liang",22,0,"教授",5000);
    student st("liang",22,0,"20112111225","11B");
    te.printf();
    cout<<"__________________"<<endl;
    st.printf();
    _getch();
    return;
}
2013-05-02 18:13
唯一的幸福
Rank: 1
等 级:新手上路
帖 子:2
专家分:1
注 册:2013-5-3
得分:0 
高手
2013-05-03 12:57
wj317738011
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-8-12
得分:0 
自己写的还能不懂呀,我晕
2013-05-03 14:20



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-405419-1-1.html




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

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