自考新教材-p222

Posted duanqibo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自考新教材-p222相关的知识,希望对你有一定的参考价值。

源程序:

//封闭类的构造函数
#include<iostream>
#include<string>
using namespace std;

class myDate
{
public:
myDate();
myDate(int);
myDate(int, int);
myDate(int, int, int);
myDate(const myDate &d);
~myDate();
void setDate(int, int, int);
void setDate(myDate);
myDate getDate();
void setYear(int);
int getMonth();
void printDate()const;
private:
int year, month, day;
};

myDate::myDate()
{
year = 1970;
month = 1;
day = 1;
cout << "myDate默认构造函数" << endl;
}

myDate::myDate(int y)
{
year = y;
month = 1;
day = 1;
cout << "myDate带1个构造函数" << endl;
}

myDate::myDate(int y, int m)
{
year = y;
month = m;
day = 1;
cout << "myDate带2个构造函数" << endl;
}

myDate::myDate(int y, int m, int d)
{
year = y;
month = m;
day = d;
cout << "myDate带3个构造函数" << endl;
}

myDate::myDate(const myDate &d)
{
year = d.year;
month = d.month;
day = d.day;
cout << "myDate_COPY构造函数" << endl;
}

myDate::~myDate()
{
cout << "myDate析构函数" << endl;
}

void myDate::setDate(int y, int m, int d)
{
year = y;
month = m;
day = d;
return;
}

void myDate::setDate(myDate oneD)
{
year = oneD.year;
month = oneD.month;
day = oneD.day;
return;
}

myDate myDate::getDate()
{
myDate d;
d.year = year;
d.month = month;
d.day = day;
return d;
}

void myDate::setYear(int y)
{
year = y;
return;
}

int myDate::getMonth()
{
return month;
}

void myDate::printDate()const
{
cout << year << "/" << month << "/" << day;
return;
}

class CStudent
{
public:
CStudent();
CStudent(string, myDate);
~CStudent();
void setStudent(string, myDate);
void setName(string);
string getName();
void setBirthday(myDate);
myDate getBirthday();
void printInfo()const;
private:
string name;
myDate birthday;
};

CStudent::CStudent() :name("Noname"), birthday(myDate())
{
cout << "CStudent默认构造函数" << endl;
}

CStudent::CStudent(string str, myDate d) : name(str), birthday(d)
{
cout << "CStudent有参构造函数" << endl;
}
CStudent::~CStudent()
{
cout << "CStudent析构函数" << endl;
}

void CStudent::setStudent(string s, myDate d)
{
name = s;
birthday.setDate(d);
return;
}

void CStudent::setName(string n)
{
name = n;
return;
}

string CStudent::getName()
{
return name;
}

void CStudent::setBirthday(myDate d)
{
birthday.setDate(d);
return;
}

myDate CStudent::getBirthday()
{
return birthday;
}

void CStudent::printInfo()const
{
cout << "姓名:" << name << " 生日:";
birthday.printDate();
cout << endl;
}

class CUndergraduateStudent :public CStudent
{
private:
string department;
public:
CUndergraduateStudent();
CUndergraduateStudent(string, myDate);
~CUndergraduateStudent();
void setDep(string);
void printInfo()const;
};

CUndergraduateStudent::CUndergraduateStudent()
{
cout << "CUndergraduateStudent默认构造函数" << endl;
}

CUndergraduateStudent::CUndergraduateStudent(string str, myDate d) :CStudent(str, d)
{
cout << "CUndergraduateStudent有参构造函数" << endl;
}
CUndergraduateStudent::~CUndergraduateStudent()
{
cout << "CUndergraduateStudent析构函数" << endl;
}

void CUndergraduateStudent::setDep(string dep)
{
department = dep;
}

void CUndergraduateStudent::printInfo()const
{
CStudent::printInfo();
cout << "院系: " << department << endl << endl;
}

int main()
{
CUndergraduateStudent s2("小张", myDate());
system("pause");
return 0;
}

运行结果:

真实输出结果:

myDate默认构造函数

myDate_COPY构造函数

myDate_COPY构造函数

CStudent有参数构造函数

myDate析构函数

CUndergraduateStudent有参数构造函数

myDate析构函数

CUndergraduateStudent析构函数

CStudent析构函数

myDate析构函数

技术图片

 

以上是关于自考新教材-p222的主要内容,如果未能解决你的问题,请参考以下文章

自考新教材-p181

自考新教材-p156

自考新教材-p161

自考新教材-p159

自考新教材-p286

自考新教材-p285