一道c++编程题(继承与派生)
1. Date类如下
class Date
{
private:
int year,month,day;
public:
Date(int year=1990,int month=1,int day=1);
Date(Date &r);
void Print();
};
Date::Date(int year,int month,int day)
{
this->year=year;
this->month=month;
this->day=day;
}
Date::Date(Date &r)
{
year=r.year;
month=r.month;
day=r.day;
}
void Date::Print()
{
cout<<year<<'-'<<month<<'-'<<day<<endl;
}
2. 在上面的工程中添加Person类,包括姓名name(字符数组,包括结束符最长20字符),性别gender(字符),生日birthday(Date类对象),人数统计变量count(静态),自行设计成员函数,保证满足以下测试要求(在main函数中调用Test1函数):
void Test1()
{
Person p1,p2("zhang",'f',Date(1991,1,1)),p3(p2);
p3.Print();//输出: “姓名:zhang;性别: 女;生日:1991-1-1;
cout<<”收支为:”<<p3.GetCashFlow()<<”元”<<endl; //输出:0元
Person *p=new Person("li",'m',Date(1990,2,2));//创建堆对象(调用有参构造)
p>Print();//输出堆对象基本信息
delete p;//释放堆对象
}
3. 从Person类以public方式派生Student类,添加学号stuId,就读时间(年)stuDuration每年学费tuition(固定为5000元),自行设计成员函数,要求满足以下测试要求:
void Test2()
{
Student s1,s2("zhang",'m',Date(1992,1,1),1001);
s2.Print(); //输出如下:
//姓名:zhang; 性别: 男; 生日:1992-1-1;
//学号:1001; 就读时间:0年
cout<<endl;
s2.SetDuration(2);
s2.Print();
//姓名:zhang; 性别: 男; 生日:1992-1-1;
//学号:1001; 就读时间:2年
cout<<endl<<"学费支出"<<s2.GetCashFlow()<<"元"<<endl;
//学费支出:10000元
Student *p=new Student; //堆对象创建(调用无参构造)
delete p;//释放堆对象
}
4. main函数如下:
void main(){
Test1();//测试Person类
Test2();//测试Student类
}
答案:#include <iostream>
using namespace std;class Date
{
private:
int year,month,day;
public:
Date(int year=1990,int month=1,int day=1);
Date(Date &r);
void Print();
};
Date::Date(int year,int month,int day)
{
this->year=year;
this->month=month;
this->day=day;
}
Date::Date(Date &r)
{
year=r.year;
month=r.month;
day=r.day;
}
void Date::Print()
{
cout<<year<<'-'<<month<<'-'<<day<<endl;
}
class Person
{
public:
Person(void);
Person(const char* _name,const char _易做图, Date _bir);
Person(const Person& per); //复制(拷贝)构造函数
Person(const Person* per); //复制(拷贝)构造函数
Person& operator=(const Person& per); //重载赋值运算符
Person& operator=(const Person* per); //重载赋值运算符
virtual ~Person(void);
virtual void Print(void);
protected:
char* name; //姓名
char gender; //性别
Date birthday; //生日
int cash; //收支
static int count;
public:
virtual int GetCashFlow()
{
return cash;
}
virtual void SetCashFlow(int _cash)
{
cash=_cash;
}
char* GetName()
{
return name;
}
void SetName(char* _name)
{
memset(name,0x00,sizeof(char)*20);
strcpy_s(name,strlen(_name)+1,_name);
}
char GetGender()
{
return gender;
}
void SetGender(char _gender)
{
gender=_gender;
}
Date GetBirthday()
{
return birthday;
}
void SetBirthday(Date _bir)
{
birthday=_bir;
}
int GetCash()
{
return cash;
}
void SetCash(unsigned int _cash)
{
cash=_cash;
}
};
int Person::count =0;
Person::Person ():gender('-'),cash(0)
{
name = new char[20];
memset(name,0x00,sizeof(char)*20);
}
Person::Person (const Person& per)
{
name = new char[20];
memset(name,0x00,sizeof(char)*20);
strcpy_s(name,strlen(per.name)+1,per.name );
this->gender = per.gender ;
this->birthday = per.birthday ;
this->cash = per.cash ;
}
Person::Person (const Person* per)
{
name = new char[20];
memset(name,0x00,sizeof(char)*20);
strcpy_s(name,strlen(per->name)+1,per->name );
this->gender = per->gender ;
this->birthday =per->birthday ;
this->cash = per->cash ;
}
Person::Person(const char* _name,const char _易做图, Date _bir)
{
name = new char[20];
memset(name,0x00,sizeof(char)*20);
strcpy_s(name,strlen(_name)+1,_name );
gender=static_cast<char>(_易做图);
birthday=_bir;
cash=0;
}
Person& Person::operator =(const Person &per)
{
if(&per==this)
{
return *this;
}
else
{
delete name;
name = new char[20];
memset(name,0x00,sizeof(char)*20);
strcpy_s(name,strlen(per.name )+1,per.name );
this->gender = per.gender ;
this->birthday = per.birthday ;
this->cash= per.cash ;
return *this;
}
}
Person& Person::operator =(const Person *per)
{
if(per==this)
{
return *this;
}
else
{
delete name;
name = new char[20];
memset(name,0x00,sizeof(char)*20);
strcpy_s(name,strlen(per->name)+1,per->name);
this->gender = per->gender ;
this->birthday = per->birthday ;
this->cash = per->cash ;
return *this;
}
}
Person::~Person()
{
if(name!=NULL)
{
delete name;
}
}
void Person::Print()
{
cout<<endl;
wcout<<"姓名:"<<name<<" 性别:"<<gender<<" 生日:";
birthday.Print();
}
class Student:public Person
{
public:
Student(void);
Student(const char* _name,const char _易做图,Date _bir,int _stuid);
Student(const Student& stu);
Student(const Student* stu);
Student& operator=(const Student& stu);
Student& operator=(const Student* stu);
void Print(void);
~Student(void);
private:
int stuId; //学号
unsigned int stuDuration; //学长
const unsigned int tuition; //学费
public:
void SetDuration(unsigned int _dur)
{
stuDuration=_dur;
}
unsigned int GetDuration()
{
return stuDuration;
}
int GetCashFlow()
{
return tuition*stuDuration;
}
void SetStuId(int stuid)
{
stuId=stuid;
}
int GetStuId()
{
return stuId;
}
unsigned int GetTuition()
{
return tuition;
}
};
Student::Student():tuition(5000),stuId(-1),stuDuration(0)
{
}
Student::Student(const char *_name, const char _易做图, Date _bir, int _stuid):Person(_name,_易做图,_bir),tuition(5000),stuId(_stuid),stuDuration(0)
{
}
Student::Student (const Student& stu):Person(stu),tuition(stu.tuition)
{
stuId=stu.stuId ;
stuDuration= stu.stuDuration ;
}
Student::Student(const Student* stu):Person(stu),tuition(stu->tuition)
{
stuId=stu->stuId ;
stuDuration= stu->stuDuration ;
}
Student& Student::operator =(const Student &stu)
{
if(&stu==this)
{
return *this;
}
else
{
this->Person::operator =(stu);
this->stuId = stu.stuId ;
this->stuDuration=stu.stuDuration ;
//this->tuition = stu.tuition ; //const 修饰,所有实例其属性值一样,不用且无法复制
return *this;
}
}
Student& Student::operator =(const Student *stu)
{
if(stu==this)
{
return *this;
}
else
{
this->Person::operator =(stu);
this->stuId = stu->stuId ;
this->stuDuration=stu->stuDuration ;
return *this;
}
}
void Student::Print()
{
cout<<endl;
Person::Print();
cout<<"学号:"<<stuId<<" 就读时间:"<<stuDuration<<"年"<<endl;
}
Student::~Student()
{
}
void Test1()
{
Person p1,p2("zhang",'f',Date(1991,1,1)),p3(p2);
p3.Print(); //输出: “姓名:zhang;性别: 女;生日:1991-1-1;
cout<<"收支为:"<<p3.GetCashFlow()<<"元"<<endl; //输出:0元
Person *p=new Person("li",'m',Date(1990,2,2)); //创建堆对象(调用有参构造)
p->Print();&
上一个:C++中怎样输出a[n]数组
下一个:C++程序设计适合零基础学C++吗?