注册 登录
编程论坛 C++教室

public,private

hzj199603 发布于 2015-04-30 19:59, 507 次点击
#include <iostream>
#include <string>
using namespace std;
class Teacher
{
    string name;
    int age;
    string sec;
    string address;
    int phone;
    string title;
public:
    void getna();
    void getag();
    void getse();
    void getad();
    void getph();
    void getti();
     void display();
};
class Cadre
{
    string name;
    int age;
    string sec;
    string address;
    int phone;
    string post;
public:
    void getna();
    void getag();
    void getse();
    void getad();
    void getph();
    void getpo();
    void display_1();
};
class Teacher_Cadre:public Teacher,public Cadre  //变成 class Teacher_Cadre:public Teacher,protected Cadre
{
    int wages;
public :
    void getwa();
    void show();
};
void Teacher::getna()
{
    cout <<"putin the teacher's name:";
    cin>>name;
}
void Teacher::getag()
{
    cout <<"putin the teacher's age:";
    cin>>age;
}
void Teacher::getse()
{
    cout <<"putin the teacher's sec:";
    cin>>sec;
}
void Teacher::getad()
{
    cout <<"putin the teacher's address:";
    cin>>address;
}
void Teacher::getph()
{
    cout <<"putin the teacher's phone:";
    cin>>phone;
}
void Teacher::getti()
{
    cout <<"putin the teacher's title:";
    cin>>title;
}
void Teacher::display()
{
    cout<<"name:"<<name<<"age:"<<age<<"sec:"<<sec<<"title:"<<title<<"address:"<<address<<"phone:"<<phone;
}
void Cadre::getna()
{
    cout <<"putin the teacher's name:";
    cin>>name;
}
void Cadre::getag()
{
    cout <<"putin the teacher's age:";
    cin>>age;
}
void Cadre::getse()
{
    cout <<"putin the teacher's sec:";
    cin>>sec;
}
void Cadre::getad()
{
    cout <<"putin the teacher's address:";
    cin>>address;
}
void Cadre::getph()
{
    cout <<"putin the teacher's phone:";
    cin>>phone;
}
void Cadre::getpo()/*class Teacher_Cadre:public Teacher,public Cadre变成 class Teacher_Cadre:public Teacher,protected Cadre                                                             这里为什么不让定义 */
{
    cout <<"putin the teacher's post:";
    cin>>post;
}
void Cadre::display_1()
{
    cout<<"post:"<<post;
}
void Teacher_Cadre::getwa()
{
    cout <<"putin the Teacher_Cadre'wages:";
    cin>>wages;
}
void Teacher_Cadre::show()
{
    display();
    display_1();
    cout<<"wages:"<<wages;
}
int main()
{
    Teacher_Cadre a;
    a.Teacher::getna();
    a.Teacher::getag();
    a.Teacher::getse();
    a.Teacher::getad();
    a.Teacher::getph();
    a.getpo();//class Teacher_Cadre:public Teacher,public Cadre变成 class Teacher_Cadre:public Teacher,protected Cadre之后,怎么改就对了?
    a.getti();
    a.getwa();
    a.show();
    return 0;
}
2 回复
#2
诸葛欧阳2015-05-01 15:59
继承的话在类定义时就可以加上,你怎么搞到函数定义里了
#3
hzj1996032015-05-01 19:02
回复 2楼 诸葛欧阳
题上要求,类内声明,类外定义
1