#include<iostream>
using namespace std;
struct day
{
int year;
int month;
int daty;
};
class man
{
int number;
char sex;
int id;
day m;
public:
man(int a,char b,int c)
{number=a;sex=b;id=c;}
man(){}
void jin();
void chu();
};
void man::jin()
{
cout <<"请输入编号:"<<endl;
cin >>number;
cout <<endl;
cout <<"请输入性别:"<<endl;
cin >>sex;
cout <<endl;
cout <<"请输入身份证号:"<<endl;
cin >>id;
cout <<endl;
cout <<"请输入出生日期:年,月,份"<<endl;
cin >>m.year>>m.month >>m.daty ;
cout <<endl;
}
void man::chu()
{
cout <<"编号为:"<<number<<endl;
cout <<"性别为:"<<sex<<endl;
cout <<"身份证号为:"<<id<<endl;
cout <<"出生日期为:"<<m.year <<m.month <<m.daty <<endl;
}
main()
{
man k;
k.jin();
k.chu();
}
也可以用友元类实现
#include<iostream>
using namespace std;
class day
{
protected://保护成员
int year;
int month;
int daty;
public:
day(){};
void Setday();
void Show();
};
void day::Setday()
{
cout <<"请输入出生日期:年,月,份"<<endl;
cin >>year>>month >>daty ;
}
void day::Show()
{
cout <<"出生日期为:"<<year <<"/"<<month <<"/"<<daty <<endl;
}
class man:public day
{
int number;
char sex;
int id;
day m;
public:
man(int a,char b,int c,int y,int m,int d)
{number=a;sex=b;id=c;year=y;month=m;daty=d;}
man(){}
void jin();
void chu();
};
void man::jin()
{
cout <<"请输入编号:"<<endl;
cin >>number;
cout <<endl;
cout <<"请输入性别:"<<endl;
cin >>sex;
cout <<endl;
cout <<"请输入身份证号:"<<endl;
cin >>id;
cout <<endl;
Setday();
}
void man::chu()
{
cout <<"编号为:"<<number<<endl;
cout <<"性别为:"<<sex<<endl;
cout <<"身份证号为:"<<id<<endl;
Show();
}
main()
{
man k;
k.jin();
k.chu();
}
#include<iostream>
using namespace std;
class day
{
protected:
int year;
int month;
int daty;
public:
day(){};
void Setday();
void Show();
};
void day::Setday()
{
cout <<"请输入出生日期:年,月,份"<<endl;
cin >>year>>month >>daty ;
}
void day::Show()
{
cout <<"出生日期为:"<<year <<"/"<<month <<"/"<<daty <<endl;
}
class man:public day
{
int number;
char sex;
int id;
day m;
public:
man(int a,char b,int c,int y,int m,int d)
{number=a;sex=b;id=c;year=y;month=m;daty=d;}
man(){}
void jin();
void chu();
};
void man::jin()
{
cout <<"请输入编号:"<<endl;
cin >>number;
cout <<endl;
cout <<"请输入性别:"<<endl;
cin >>sex;
cout <<endl;
cout <<"请输入身份证号:"<<endl;
cin >>id;
cout <<endl;
Setday();
}
void man::chu()
{
cout <<"编号为:"<<number<<endl;
cout <<"性别为:"<<sex<<endl;
cout <<"身份证号为:"<<id<<endl;
Show();
}
main()
{
man k;
k.jin();
k.chu();
}