A. 【程序填空】人民币运算(输入输出重载)
#include <iostream>
using namespace std;
class RMB;
ostream& operator <<(ostream&, RMB&);
istream& operator >>(istream&, RMB&);
class RMB {
protected:
int yuan, jiao, fen;
public:
RMB(double);
RMB(int, int, int);
bool operator > (RMB&);
friend ostream& operator <<(ostream&, RMB&); //一行输出,无换行
friend istream& operator >>(istream&, RMB&);
friend RMB operator +(RMB&, RMB&);
};
//完成以下类定义的填空
bool RMB::operator >(RMB& R) {
int i = this->yuan * 100 + this->jiao * 10 + this->fen;
int j = R.yuan * 100 + jiao * 10 + fen;
int flag = i - j;
return flag > 0;
}
RMB operator+(RMB& R1, RMB& R2) {
int yuan = R1.yuan + R2.yuan;
int jiao = R1.jiao + R2.jiao;
int fen = R1.fen + R2.fen;
yuan = yuan + (jiao + fen / 10) / 10;
jiao = (jiao + fen / 10) % 10;
fen %= 10;
RMB R3(yuan, jiao, fen);
return R3;
}
ostream& operator<<(ostream& o, RMB& R) {
o << R.yuan << "元" << R.jiao << "角" << R.fen << "分";
return o;
}
istream& operator>>(istream& i, RMB& R) {
i >> R.yuan >> R.jiao >> R.fen;
return i;
}
RMB::RMB(double d=0) {
d *= 100;
yuan = (int)d / 100;
jiao =(int) d / 10 % 10;
fen = (int)d % 10;
}
RMB::RMB(int a, int b, int c) {
yuan = a;
jiao = b;
fen = c;
}
//主函数
int main()
{
int t;
double val_yuan;
cin >> t;
while (t--)
{
cin >> val_yuan; //输入一个浮点数,例如1.23
RMB r1(val_yuan); //例如上一行输入1.23,则生成对象r1是1元2角3分
RMB r2;
cin >> r2; //输入一行三个整数参数,按元、角、分输入
if (r1 > r2) cout << r1 << " > " << r2 << endl;
else cout << r1 << " <= " << r2 << endl;
RMB r3 = r1 + r2;
cout << r1 << " + " << r2 << " = " << r3 << endl;
}
return 0;
}
B. 【程序填空】三维点坐标平移(增量运算符重载)
#include <iostream>
using namespace std;
class Point;
Point operator -- (Point&);
Point operator -- (Point&, int);
class Point {
private:
int x, y, z;
public:
Point(int tx = 0, int ty = 0, int tz = 0)
{
x = tx, y = ty, z = tz;
}
Point operator ++ ();
Point operator ++ (int);
friend Point operator -- (Point&);
friend Point operator -- (Point&, int);
void print();
};
//完成以下填空
Point Point::operator++() {
x += 1;
y += 1;
z += 1;
return *this;
}
Point Point:: operator++(int) {
Point p1(x, y, z);
x += 1;
y += 1;
z += 1;
return p1;
}
void Point::print(){
cout << "x=" << x << " y=" << y << " z=" << z << endl;
}
Point operator--(Point& p) {
p.x -= 1;
p.y -= 1;
p.z -= 1;
return p;
}
Point operator--(Point& p, int) {
Point p1(p.x,p.y,p.z);
p.x -= 1;
p.y -= 1;
p.z -= 1;
return p1;
}
int main()
{
int tx, ty, tz;
cin >> tx >> ty >> tz;
Point p0(tx, ty, tz); //原值保存在p0
Point p1, p2; //临时赋值进行增量运算
//第1行输出
p1 = p0;
p1++;;
p1.print();
//第2行输出
p1 = p0;
p2 = p1++;
p2.print();
//第3、4行输出,前置++
p1 = p0;
(++p1).print();
p1.print();
//第5、6行输出,后置--
p1 = p0;
p1--;
p1.print();
p0.print();
//第7、8行输出,前置--
p1 = p0;
(--p1).print();
p1.print();
return 0;
}
C. 【程序填空】宠物管理
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
class Pet { //基类,也是抽象类
protected:
string kind; //宠物类型
int ID; //宠物编号,固定长度为5位正整数
public:
Pet() : kind("unset"), ID(-1) { }
virtual void set(string tk, int ti) = 0; //宠物必须设置类型和编号
virtual void print() { cout << "NONE" << endl; }
};
//完成类Cat和类Dog的填空
class Cat :public Pet {
string name;
string food;
public:
Cat() {}
Cat(string ts, string tf) {
name = ts;
food = tf;
}
void set(string tk, int ti) {
kind = tk;
ID = ti;
}
void print() {
cout << kind << "'s ID=1";
printf("%04d\n", ID);
cout << name << " likes " << food << endl;
}
};
class Dog :public Pet {
string name;
int size;
public:
Dog() {}
Dog(string ts, int s) {
name = ts;
size = s;
}
void set(string tk, int ti) {
kind = tk;
ID = ti;
}
void print() {
cout << kind << "'s ID=2";
printf("%04d\n", ID);
string s;
if (size == 1) s = "small";
else if (size == 2) s = "medium";
else s = "big";
cout << name << " is " << s << endl;
}
};
//主函数和输出的全局函数如下:
void print_pet(Pet& pr)
{
pr.print();
}
int main()
{
string tk, ts, tf;
int t, ti, tt;
char ptype;
cin >> t;
while (t--)
{
cin >> ptype;
if (ptype == 'C')
{
cin >> tk >> ti >> ts >> tf; //类型、编号、姓名、食物
Cat cc(ts, tf);
cc.set(tk, ti);
print_pet(cc);
}
if (ptype == 'D')
{
cin >> tk >> ti >> ts >> tt; //类型、编号、姓名、犬大小
Dog dd(ts, tt);
dd.set(tk, ti);
print_pet(dd);
}
}
return 0;
}
D. 【程序填空】点距离计算(单继承)
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
//假设点坐标均为整数
class Point {
protected:
int x;
public:
Point(int);
void distance(); //计算当前点到原点的距离,并输出结果信息
};
class Point_2D : public Point {
protected:
int y;
public:
Point_2D(int, int); //有参构造,设置二维平面的x\y坐标
void distance(); //计算当前点到原点的距离,并输出结果信息
};
class Point_3D : public Point_2D {
protected:
int z;
public:
Point_3D(int, int, int); //有参构造,设置三维平面的x\y\z坐标
void distance();//计算当前点到原点的距离,并输出结果信息
//三维点到原点距离,等于x\y\z坐标平方的总和的开方
};
Point_3D::Point_3D(int x,int y,int z):z(z),Point_2D(x,y){}
void Point_3D::distance() {
double dis = sqrt(x * x + y * y + z * z);
cout << "Distance between [" << x << ", " << y << ", " << z << "] and [0, 0, 0] = " <<fixed <<setprecision(2) << dis << endl;
}
Point_2D::Point_2D(int x, int y) :y(y) ,Point(x) {}
void Point_2D::distance() {
double dis = sqrt(x * x + y * y);
cout << "Distance between [" << x << ", " << y << "] and [0, 0] = " << fixed<<setprecision(2) << dis << endl;
}
Point::Point(int x ):x(x) {}
void Point::distance() {
cout << "Distance between [" << x << "] and [0] = " << fixed<<setprecision(2) << fabs(x) << endl;
}
//主函数如下
int main()
{
int num, tx, ty, tz;
cin >> num;
while (num)
{
switch (num) {
case 1:
{ cin >> tx;
Point p1(tx);
p1.distance();
break;
}
case 2:
{ cin >> tx >> ty;
Point_2D p2(tx, ty);
p2.distance();
break;
}
case 3:
{ cin >> tx >> ty >> tz;
Point_3D p3(tx, ty, tz);
p3.distance();
break;
}
}
cin >> num;
}
return 0;
}
E. 【程序填空】学生毕业(拷贝构造之深拷贝)
#include <iostream>
using namespace std;
class MyDate {
private:
int year, month, day;
public:
void set(int ty = 0, int tm = 0, int td = 0)
{
year = ty, month = tm, day = td;
}
void print()
{
cout << year << "-" << month << "-" << day << endl;
}
};
MyDate today; //全局变量,用于毕业日期设置
class STU {
private:
long id; //学号
int gra_state; //未毕业为0,毕业为1
MyDate* gra_date; //毕业日期,初始为空
public:
STU(int ti); //有参构造,输出相关信息
STU(STU& rs); //拷贝构造,设置毕业状态和毕业日期,输出相关信息
~STU()
{
if (gra_state == 1) //判断是否毕业
{
cout << "毕业生" << id << "已析构" << endl;
cout << "毕业日期";
gra_date->print();
delete gra_date;
}
else
cout << "学生" << id << "已析构" << endl;
}
};
//实现STU类的有参构造和拷贝构造
STU::STU(int ti) {
id = (int)ti;
gra_state = 0;
cout << "学生" << id << "已构造" << endl;
}
STU::STU(STU& rs) {
id = (int)rs.id;
gra_state = 1;
gra_date = new MyDate;
*gra_date = today;
cout << "毕业生" << id << "已构造"<<endl;
}
//主函数如下
int main()
{
int y, m, d, ti;
cin >> y >> m >> d;
today.set(y, m, d);
cin >> ti;
STU s1(ti); //通过构造函数生成一个在校学生
STU s2 = s1; //通过拷贝构造生成毕业生
return 0;
}
F. 【程序填空】Tutor类(拷贝构造)
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
//填空实现类STU的定义
class STU
{
string name;
public:
STU(){}
STU(string s):name(s){
cout << "Construct student " << name<<endl;
}
STU(STU& S) {
name = S.name+"_copy";
cout << "Construct student " << name << endl;
}
~STU() {
cout << "Destruct student " << name << endl;
}
};
//其他代码如下
int IDs; //全局变量,用于输出结果提示
class Tutor {
private:
STU stu;
public:
Tutor(STU& s) : stu(s)
{
cout << "Construct tutor " << IDs << endl;
}
~Tutor()
{
cout << "Destruct tutor " << IDs << endl;
}
};
void fuc(Tutor x)
{
cout << "In function fuc()" << endl;
}
int main()
{
cin >> IDs;
STU s1("Tom"); //输入学生姓名Tom
Tutor t1(s1);
IDs++;
cout << "Calling fuc()" << endl;
fuc(t1);
return 0;
}
G. 【程序填空】日期比较(构造与析构)
//头文件和类声明
#include <iostream>
using namespace std;
class MyDate {
private:
int year;
int month;
int day;
public:
MyDate(); //无参构造,默认日期2022-4-1,输出相关构造信息
MyDate(int ty, int tm, int td); //有参构造,根据参数初始化,输出相关构造信息
~MyDate();
bool Before(MyDate& rd);
void print()
{
cout << year << "-" << month << "-" << day;
}
};
//下面填写类实现和主函数
MyDate::MyDate(){
year = 2022;
month = 4;
day = 1;
cout << "Date ";
print();
cout << " default constructed" << endl;
}
MyDate::MyDate(int ty, int tm, int td) {
year = ty;
month = tm;
day = td;
cout << "Date ";
print();
cout << " constructed" << endl;
}
MyDate::~MyDate() {
cout << "Date ";
print();
cout << " destructed" << endl;
}
bool MyDate::Before(MyDate& rd) {
bool MyDate::Before(MyDate &rd){
if(year!=rd.year) return year<rd.year;
if(month!=rd.month) return month<rd.month;
return day<rd.day;
}
}
//main end
int main() {
int t,year,month,day;
cin >> t;
MyDate d0;
while (t--) {
cin >> year >> month >> day;
MyDate d1(year, month, day);
if (d1.Before(d0)) {
d1.print();
cout << " before ";
d0.print();
cout << endl;
}
else {
d0.print();
cout << " before ";
d1.print();
cout << endl;
}
}
}
H. 【程序填空】日期类外定义(类和对象)
//头文件和类声明
#include <iostream>
using namespace std;
class MyDate {
private:
int year;
int month;
int day;
public:
MyDate(); //无参构造,默认日期2022-4-1,输出相关构造信息
MyDate(int ty, int tm, int td); //有参构造,根据参数初始化,输出相关构造信息
~MyDate();
bool Before(MyDate& rd);
void print()
{
cout << year << "-" << month << "-" << day;
}
};
//下面填写类实现和主函数
MyDate::MyDate(){
year = 2022;
month = 4;
day = 1;
cout << "Date ";
print();
cout << " default constructed" << endl;
}
MyDate::MyDate(int ty, int tm, int td) {
year = ty;
month = tm;
day = td;
cout << "Date ";
print();
cout << " constructed" << endl;
}
MyDate::~MyDate() {
cout << "Date ";
print();
cout << " destructed" << endl;
}
bool MyDate::Before(MyDate& rd) {
bool MyDate::Before(MyDate &rd){
if(year!=rd.year) return year<rd.year;
if(month!=rd.month) return month<rd.month;
return day<rd.day;
}
}
//main end
int main() {
int t,year,month,day;
cin >> t;
MyDate d0;
while (t--) {
cin >> year >> month >> day;
MyDate d1(year, month, day);
if (d1.Before(d0)) {
d1.print();
cout << " before ";
d0.print();
cout << endl;
}
else {
d0.print();
cout << " before ";
d1.print();
cout << endl;
}
}
}
I. 【2个空示范】矩形类定义
#include <iostream>
using namespace std;
//填空区域1:类Rect
class Rect {
int x;
int y;
public:
Rect() {}
Rect(int x1, int y1) :x(x1), y(y1) {}
int getx() { return x; }
int gety() { return y; }
};
int Compare(Rect* p1, Rect* p2)
{ //函数Compare内部代码在这里开始
int s1 = p1->getx()*p1->gety();
int s2 = p2->getx()*p2->gety();
if (s1 > s2) return 1;
else if (s1 == s2) return 0;
else return -1;
} //函数Compare在这里结束
int main()
{
int x, y, t;
cin >> t;
while (t--)
{
cin >> x >> y;
Rect r1(x, y);
cin >> x >> y;
Rect r2(x, y);
int rs = Compare(&r1, &r2);
if (rs == 1) cout << "Rect1 > Rect2" << endl;
if (rs == -1) cout << "Rect1 < Rect2" << endl;
if (rs == 0) cout << "Rect1 == Rect2" << endl;
}
return 0;
}
J. 【1个空示范】类ADD定义
//头文件区域
#include <iostream>
using namespace std;
//填空区域1:类ADD的定义
class ADD {
int a;
int b;
public:
ADD() { a = 0; b = 0; }
void input() {
cin >> a >> b;
}
void output() {
cout << a + b << endl;
}
};
//主函数区域
int main()
{
ADD obj;
obj.input(); //input a and b
obj.output(); //ouput a+b
return 0;
}
K. OOP 日期递增(拷贝构造)
//头文件区域
#include <iostream>
using namespace std;
//填空区域1:类CDate的定义
class CDate {
int year;
int month;
int day;
public:
CDate() { year = 0; month = 0; day = 0; }
CDate(int d, int m, int y) {
day = d;
month = m;
year = y;
}
CDate(CDate& d) {
int two;
if ((year % 100) != 0 && (year % 400) == 0 || (year % 4) == 0) two = 29;
else two = 28;
year = d.year;
month = d.month;
day = d.day;
int days[13] = { 0,31,two,31,30,31,30,31,31,30,31,30,31 };
day = (d.day + 1);
if (this->day > days[d.month]) day = 1;
int flag = 0;
if (day != d.day + 1) flag = 1;
month = (d.month + flag);
if (month > 12) month = 1;
flag = 0;
if (month == 1) flag = 1;
year = d.year + flag;
}
void print() {
cout << year << "/" << month << "/" << day << endl;
}
};
//主函数区域
int main()
{
int a, b, c;
cin >> a >> b >> c;
CDate d1(a, b, c);
CDate d2 = d1;
CDate d3(d2);
d1.print();
d2.print();
d3.print();
return 0;
}
//答案
#include <iostream>
using namespace std;
class RMB;
ostream & operator <<(ostream &, RMB &);
istream & operator >>(istream &, RMB &);
class RMB{
protected:
int yuan, jiao, fen;
public:
RMB(double);
RMB (int, int, int);
bool operator > (RMB &);
friend ostream & operator <<(ostream &, RMB&); //一行输出,无换行
friend istream & operator >>(istream &, RMB&);
friend RMB operator +(RMB&, RMB&);
};
//完成以下类定义的填空
RMB::RMB(double r=0){
r+=0.005;
yuan=int(r);
jiao=int(r*10)%10;
fen=int(r*100)%10;
}
RMB::RMB (int y, int j, int f){
yuan=y;
jiao=j;
fen=f;
}
bool RMB::operator > (RMB & r){
if(yuan!=r.yuan)return yuan>r.yuan;
if(jiao!=r.jiao)return jiao>r.jiao;
return fen>r.fen;
}
ostream & operator <<(ostream & o, RMB& r){
o<<r.yuan<<"元"<<r.jiao<<"角"<<r.fen<<"分";
return o;
} //一行输出,无换行
istream & operator >>(istream & i, RMB& r){
i>>r.yuan>>r.jiao>>r.fen;
return i;
}
RMB operator +(RMB& a, RMB& b){
RMB r(a);
r.yuan+=b.yuan;
r.jiao+=b.jiao;
r.fen+=b.fen;
r.jiao+=r.fen/10;
r.fen=r.fen%10;
r.yuan+=r.jiao/10;
r.jiao=r.jiao%10;
return r;
}
//主函数
int main()
{ int t;
double val_yuan;
cin>>t;
while (t--)
{ cin>>val_yuan; //输入一个浮点数,例如1.23
RMB r1(val_yuan); //例如上一行输入1.23,则生成对象r1是1元2角3分
RMB r2;
cin>>r2; //输入一行三个整数参数,按元、角、分输入
if (r1>r2) cout<<r1<<" > "<<r2<<endl;
else cout<<r1<<" <= "<<r2<<endl;
RMB r3 =r1+r2;
cout<<r1<<" + "<<r2<<" = "<<r3<<endl;
}
return 0;
}
=====================================================================
#include <iostream>
using namespace std;
class Point;
Point operator -- (Point & );
Point operator -- (Point &, int);
class Point {
private:
int x, y, z;
public:
Point(int tx=0, int ty=0, int tz=0 )
{ x = tx, y = ty, z = tz; }
Point operator ++ ();
Point operator ++ (int);
friend Point operator -- (Point & );
friend Point operator -- (Point &, int);
void print();
};
//完成以下填空
Point Point::operator ++ (){
x++;
y++;
z++;
return *this;
}
Point Point::operator ++ (int){
Point t(*this);
x++;
y++;
z++;
return t;
}
Point operator -- (Point & p){
p.x--;
p.y--;
p.z--;
return p;
}
Point operator -- (Point & p, int){
Point t(p);
p.x--;
p.y--;
p.z--;
return t;
}
void Point::print(){
cout<<"x="<<x<<" y="<<y<<" z="<<z<<endl;
}
//
int main()
{ int tx, ty, tz;
cin>>tx>>ty>>tz;
Point p0(tx, ty, tz); //原值保存在p0
Point p1, p2; //临时赋值进行增量运算
//第1行输出
p1 = p0;
p1++;;
p1.print();
//第2行输出
p1 = p0;
p2 = p1++;
p2.print();
//第3、4行输出,前置++
p1 = p0;
(++p1).print();
p1.print();
//第5、6行输出,后置--
p1 = p0;
p1--;
p1.print();
p0.print();
//第7、8行输出,前置--
p1 = p0;
(--p1).print();
p1.print();
return 0;
}
=====================================================================
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
class Pet { //基类,也是抽象类
protected:
string kind; //宠物类型
int ID; //宠物编号,固定长度为5位正整数
public:
Pet(): kind("unset"), ID(-1) { }
virtual void set(string tk, int ti)=0; //宠物必须设置类型和编号
virtual void print() { cout<<"NONE"<<endl; }
};
//完成类Cat和类Dog的填空
class Cat:public Pet{
string name;
string food;
public:
Cat(string name,string food):name(name),food(food){}
void set(string k,int i){
kind=k;
ID=10000+i;
}
void print(){
cout<<kind<<"'s ID="<<ID<<endl;
cout<<name<<" likes "<<food<<endl;
}
};
class Dog:public Pet{
string name;
int big;
public:
Dog(string name,int big):name(name),big(big){}
void set(string k,int i){
kind=k;
ID=20000+i;
}
void print(){
string t[4]={"","small","medium","big"};
cout<<kind<<"'s ID="<<ID<<endl;
cout<<name<<" is "<<t[big]<<endl;
}
};
//主函数和输出的全局函数如下:
void print_pet(Pet &pr)
{ pr.print(); }
int main()
{ string tk, ts, tf;
int t, ti, tt;
char ptype;
cin>>t;
while (t--)
{ cin>>ptype;
if (ptype=='C')
{ cin>>tk>>ti>>ts>>tf; //类型、编号、姓名、食物
Cat cc(ts, tf);
cc.set(tk, ti);
print_pet(cc);
}
if (ptype=='D')
{ cin>>tk>>ti>>ts>>tt; //类型、编号、姓名、犬大小
Dog dd(ts, tt);
dd.set(tk, ti);
print_pet(dd);
}
}
return 0;
}
=====================================================================
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
//假设点坐标均为整数
class Point {
protected:
int x;
public:
Point(int);
void distance(); //计算当前点到原点的距离,并输出结果信息
};
class Point_2D: public Point {
protected:
int y;
public:
Point_2D(int, int); //有参构造,设置二维平面的x\y坐标
void distance(); //计算当前点到原点的距离,并输出结果信息
};
class Point_3D: public Point_2D {
protected:
int z;
public:
Point_3D(int, int, int); //有参构造,设置三维平面的x\y\z坐标
void distance();//计算当前点到原点的距离,并输出结果信息
//三维点到原点距离,等于x\y\z坐标平方的总和的开方
};
//完成三个类实现的填空
Point::Point(int x):x(x){}
void Point::distance(){
double d=sqrt(x*x);
cout<<"Distance between ["<<x<<"] and [0] = "<<fixed<<setprecision(2)<<d<<endl;
} //计算当前点到原点的距离,并输出结果信息
Point_2D::Point_2D(int x, int y):Point(x),y(y){
} //有参构造,设置二维平面的x\y坐标
void Point_2D::distance(){
double d=sqrt(x*x+y*y);
cout<<"Distance between ["<<x<<", "<<y<<"] and [0, 0] = "<<fixed<<setprecision(2)<<d<<endl;
} //计算当前点到原点的距离,并输出结果信息
Point_3D::Point_3D(int x, int y, int z):Point_2D(x,y),z(z){} //有参构造,设置三维平面的x\y\z坐标
void Point_3D::distance(){
double d=sqrt(x*x+y*y+z*z);
cout<<"Distance between ["<<x<<", "<<y<<", "<<z<<"] and [0, 0, 0] = "<<fixed<<setprecision(2)<<d<<endl;
}
//计算当前点到原点的距离,并输出结果信息
//三维点到原点距离,等于x\y\z坐标平方的总和的开方
//主函数如下
int main()
{ int num,tx, ty, tz;
cin>>num;
while (num)
{ switch (num) {
case 1:
{ cin>>tx;
Point p1(tx);
p1.distance();
break;
}
case 2:
{ cin>>tx>>ty;
Point_2D p2(tx, ty);
p2.distance();
break;
}
case 3:
{ cin>>tx>>ty>>tz;
Point_3D p3(tx, ty, tz);
p3.distance();
break;
}
}
cin>>num;
}
return 0;
}
=====================================================================
#include <iostream>
using namespace std;
class MyDate {
private:
int year, month, day;
public:
void set(int ty=0, int tm=0, int td=0)
{ year = ty, month = tm, day = td; }
void print()
{ cout<<year<<"-"<<month<<"-"<<day<<endl; }
};
MyDate today; //全局变量,用于毕业日期设置
class STU {
private:
long id; //学号
int gra_state; //未毕业为0,毕业为1
MyDate *gra_date; //毕业日期,初始为空
public:
STU(int ti); //有参构造,输出相关信息
STU(STU & rs); //拷贝构造,设置毕业状态和毕业日期,输出相关信息
~STU()
{ if (gra_state==1) //判断是否毕业
{ cout<<"毕业生"<<id<<"已析构"<<endl;
cout<<"毕业日期";
gra_date->print();
delete gra_date;
}
else
cout<<"学生"<<id<<"已析构"<<endl;
}
};
//实现STU类的有参构造和拷贝构造
STU::STU(int ti){
id=ti;
gra_state=0;
cout<<"学生"<<id<<"已构造"<<endl;
} //有参构造,输出相关信息
STU::STU(STU & rs){
gra_date=new MyDate;
*gra_date=today;
id=rs.id;
gra_state=1;
cout<<"毕业生"<<id<<"已构造"<<endl;
} //拷贝构造,设置毕业状态和毕业日期,输出相关信息
//主函数如下
int main()
{ int y, m, d, ti;
cin>>y>>m>>d;
today.set(y,m,d);
cin>>ti;
STU s1(ti); //通过构造函数生成一个在校学生
STU s2=s1; //通过拷贝构造生成毕业生
return 0;
}
=====================================================================
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
//填空实现类STU的定义
class STU{
string name;
int id;
public:
STU(string name):name(name){
cout<<"Construct student "<<name<<endl;
id=1;
}
STU(STU& s){
name=s.name+"_copy";
cout<<"Construct student "<<name<<endl;
id=2;
}
~STU(){
if(id==1)
cout<<"Destruct student "<<name<<endl;
else
cout<<"Destruct student "<<name<<endl;
}
};
//其他代码如下
int IDs; //全局变量,用于输出结果提示
class Tutor {
private:
STU stu;
public:
Tutor(STU & s): stu(s)
{ cout<<"Construct tutor "<<IDs<<endl; }
~Tutor()
{ cout<<"Destruct tutor "<<IDs<<endl; }
};
void fuc(Tutor x)
{ cout<<"In function fuc()"<<endl; }
int main()
{ cin>>IDs;
STU s1("Tom"); //输入学生姓名Tom
Tutor t1(s1);
IDs++;
cout<<"Calling fuc()"<<endl;
fuc(t1);
return 0;
}
=====================================================================
//头文件和类声明
#include <iostream>
using namespace std;
class MyDate {
private:
int year;
int month;
int day;
public:
MyDate(); //无参构造,默认日期2022-4-1,输出相关构造信息
MyDate(int ty, int tm, int td); //有参构造,根据参数初始化,输出相关构造信息
~MyDate();
bool Before(MyDate &rd);
void print()
{ cout<<year<<"-"<<month<<"-"<<day; }
};
//下面填写类实现和主函数
MyDate::MyDate(){
year=2022;
month=4;
day=1;
cout<<"Date 2022-4-1 default constructed"<<endl;
} //无参构造,默认日期2022-4-1,输出相关构造信息
MyDate::MyDate(int ty, int tm, int td):year(ty),month(tm),day(td){
cout<<"Date ";
print();
cout<<" constructed"<<endl;
} //有参构造,根据参数初始化,输出相关构造信息
MyDate::~MyDate(){
cout<<"Date ";
print();
cout<<" destructed"<<endl;
}
bool MyDate::Before(MyDate &rd){
if(year!=rd.year) return year<rd.year;
if(month!=rd.month) return month<rd.month;
return day<rd.day;
}
//main end
int main(){
int t,year,month,day;
cin>>t;
MyDate d;
while(t--){
cin>>year>>month>>day;
MyDate date(year,month,day);
if(date.Before(d)){
date.print();
cout<<" before ";
d.print();
cout<<endl;
}
else{
d.print();
cout<<" before ";
date.print();
cout<<endl;
}
}
}
=====================================================================
//头文件和日期类声明
#include<iostream>
#include<iomanip>
using namespace std;
class TDate {
private:
int year,month,day;
public:
int getYear(){return year;}
int getMonth(){return month;}
int getDay(){return day;}
void set(int y,int m,int d);
void print();
void addOneDay();
};
//----在以下区域完成部分成员函数的类外定义----
void TDate::set(int y,int m,int d){
year=y;
month=m;
day=d;
}
void TDate::print(){
cout<<year<<setfill('0')<<setw(2)<<month<<setw(2)<<day;
}
void TDate::addOneDay(){
int d[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(year%400==0 || (year%4==0 && year%100!=0))
d[2]=29;
day++;
if(day>d[month]){
day=1;
month++;
}
if(month==13){
month=1;
year++;
}
}
//主函数如下
int main()
{ int t, y,m,d;
cin>>t;
while(t--)
{ TDate d1;
cin>>y>>m>>d;
d1.set(y, m, d);
cout<<"Today-";
d1.print();
d1.addOneDay();
cout<<" Tomorrow-";
d1.print();
cout<<endl;
}
return 0;
}
=====================================================================
#include <iostream>
using namespace std;
//填空区域1:类Rect
class Rect{
int length,width;
public:
Rect(int l,int w):length(l),width(w){}
int getarea(){return length*width;}
};
//填空区域2:全局函数Compare
int Compare(Rect *p1, Rect *p2)
//注意函数声明已经写好,只需要写{}内部分
{ //函数Compare内部代码在这里开始
if(p1->getarea()>p2->getarea())
return 1;
else if(p1->getarea()<p2->getarea())
return -1;
else
return 0;
} //函数Compare在这里结束
int main()
{ int x, y, t;
cin>>t;
while (t--)
{ cin>>x>>y;
Rect r1(x, y);
cin>>x>>y;
Rect r2(x, y);
int rs= Compare(&r1, &r2);
if (rs==1) cout<<"Rect1 > Rect2"<<endl;
if (rs==-1) cout<<"Rect1 < Rect2"<<endl;
if (rs==0) cout<<"Rect1 == Rect2"<<endl;
}
return 0;
}
=====================================================================
//头文件区域
#include <iostream>
using namespace std;
//填空区域1:类ADD的定义
\********** Write your code here! **********\
class ADD{
int a,b;
public:
void input(){
cin>>a>>b;
}
void output(){
cout<<a+b<<endl;
}
};
\*******************************************\
//主函数区域
int main()
{ ADD obj;
obj.input(); //input a and b
obj.output(); //ouput a+b
return 0;
}
=====================================================================
//头文件区域
#include <iostream>
using namespace std;
//填空区域1:类CDate的定义
class CDate{
int year,month,day;
public:
CDate(int d,int m,int y):year(y),month(m),day(d){}
CDate(CDate& date):year(date.year),month(date.month),day(date.day){
int d[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(year%400==0 || (year%4==0 && year%100!=0))
d[2]=29;
day++;
if(day>d[month]){
day=1;
month++;
}
if(month==13){
month=1;
year++;
}
}
void print(){
cout<<year<<"/"<<month<<"/"<<day<<endl;
}
};
//主函数区域
int main()
{ int a,b,c;
cin>>a>>b>>c;
CDate d1(a,b,c);
CDate d2=d1;
CDate d3(d2);
d1.print();
d2.print();
d3.print();
return 0;
}