one
作者:互联网
// Name.h class Name { public: Name(char*); Name(Name&); virtual ~Name(); void Disp(); protected: char *pname; }; // Tbirth.h class Tbirth { public: Tbirth(); Tbirth(int ,int); Tbirth(int); void Disp(); protected: int month; int year; }; // Student.h //#include"Name.h" //#include"Tbirth.h" class Student { public: Student(); Student(char*); Student(char*,int,int,int); Student(Student&); ~Student(); void Disp(); static int Getnumber(); protected: Name name; Tbirth birth; int age; static int number; }; // Name.cpp //#include "Name.h" #include<cstring> #include<iostream> using namespace std; Name::Name(char *a) { pname=new char[strlen(a)+1]; strcpy(pname,a); } Name::Name(Name &n) { pname=new char[strlen(n.pname)+1]; strcpy(pname,n.pname); } void Name::Disp() { cout<<pname<<endl; } Name::~Name() { delete []pname; } // Tbirth.cpp //#include "Tbirth.h" //#include<iostream> //using namespace std; Tbirth::Tbirth() { year=2022; month=4; } Tbirth::Tbirth(int a,int b) { year=a; month=b; } Tbirth::Tbirth(int a) { year=a; month=1; } void Tbirth::Disp() { cout<<month<<'/'<<year<<endl; } // Student.cpp //#include "Student.h" //#include<iostream> //using namespace std; int Student::number=0; Student::Student():name("noname"),birth(2001,12) { age=20; number++; } Student::Student(char *a):name(a),birth(2002,6) { age=19; number++; } Student::Student(char *a1,int a2,int a3,int a4):name(a1),birth(a2,a3) { age=a4; number++; } Student::Student(Student &s):name(s.name),birth(s.birth) { age=s.age; number++; } Student::~Student() { number--; } int Student::Getnumber() { return number; } void Student::Disp() { name.Disp(); birth.Disp(); cout<<age<<endl; } //main.cpp //#include"student.h" //#include<iostream> //using namespace std; void DispStudedntNum() //随时可以被调用以显示学生人数 { cout<<Student::Getnumber()<<endl;; //自行填上所缺代码 } void fn(Student s) { s.Disp(); DispStudedntNum(); } int main() { Tbirth birth1,birth2(1980); birth1.Disp(); birth2.Disp(); DispStudedntNum(); char name1[10],name2[10]; int y,m,age; cin>>name1; cin>>name2>>y>>m>>age; Student s1, s2(name1), s3(name2,y,m,age); DispStudedntNum(); s1.Disp(); s2.Disp(); s3.Disp(); fn(s3); DispStudedntNum(); return 0; }
// Name.h class Name { public: Name(char*); Name(Name&); virtual ~Name(); void Disp(); protected: char *pname; }; // Tbirth.h class Tbirth { public: Tbirth(); Tbirth(int ,int); Tbirth(int); void Disp(); protected: int month; int year; }; // Student.h //#include"Name.h" //#include"Tbirth.h" class Student { public: Student(); Student(char*); Student(char*,int,int,int); Student(Student&); ~Student(); void Disp(); static int Getnumber(); protected: Name name; Tbirth birth; int age; static int number; }; // Name.cpp //#include "Name.h" #include<cstring> #include<iostream> using namespace std; Name::Name(char *a) { pname=new char[strlen(a)+1]; strcpy(pname,a); } Name::Name(Name &n) { pname=new char[strlen(n.pname)+1]; strcpy(pname,n.pname); } void Name::Disp() { cout<<pname<<endl; } Name::~Name() { delete []pname; } // Tbirth.cpp //#include "Tbirth.h" //#include<iostream> //using namespace std; Tbirth::Tbirth() { year=2022; month=4; } Tbirth::Tbirth(int a,int b) { year=a; month=b; } Tbirth::Tbirth(int a) { year=a; month=1; } void Tbirth::Disp() { cout<<month<<'/'<<year<<endl; } // Student.cpp //#include "Student.h" //#include<iostream> //using namespace std; int Student::number=0; Student::Student():name("noname"),birth(2001,12) { age=20; number++; } Student::Student(char *a):name(a),birth(2002,6) { age=19; number++; } Student::Student(char *a1,int a2,int a3,int a4):name(a1),birth(a2,a3) { age=a4; number++; } Student::Student(Student &s):name(s.name),birth(s.birth) { age=s.age; number++; } Student::~Student() { number--; } int Student::Getnumber() { return number; } void Student::Disp() { name.Disp(); birth.Disp(); cout<<age<<endl; } //main.cpp //#include"student.h" //#include<iostream> //using namespace std; void DispStudedntNum() //随时可以被调用以显示学生人数 { cout<<Student::Getnumber()<<endl;; //自行填上所缺代码 } void fn(Student s) { s.Disp(); DispStudedntNum(); } int main() { Tbirth birth1,birth2(1980); birth1.Disp(); birth2.Disp(); DispStudedntNum(); char name1[10],name2[10]; int y,m,age; cin>>name1; cin>>name2>>y>>m>>age; Student s1, s2(name1), s3(name2,y,m,age); DispStudedntNum(); s1.Disp(); s2.Disp(); s3.Disp(); fn(s3); DispStudedntNum(); return 0; }
标签:,Disp,Name,int,char,Tbirth,Student 来源: https://www.cnblogs.com/niushaoshuai/p/16147551.html