prog9.cpp
来自「C++语言程序设计题典」· C++ 代码 · 共 85 行
CPP
85 行
#include <iostream.h>
#include <string.h>
class object //物品类
{
protected:
char color[10]; //物品类型
char type[10]; //颜色
public:
object() {}
object(char *t,char *c)
{
strcpy(type,t);
strcpy(color,c);
}
};
class shoe : public object //鞋子类
{
private:
friend class person;
public:
shoe(char *t,char *c) : object(t,c) {}
};
class clothes : public object //衣服类
{
private:
friend class person;
public:
clothes(char *t,char *c) : object(t,c) {}
};
class pants : public object //裤子类
{
private:
friend class person;
public:
pants(char *t,char *c):object(t,c) {}
};
class person //人员类
{
protected:
char name[10];
clothes *myclothes; //指向clothes对象的指针
shoe *myshoe; //指向shoe对象的指针
pants *mypants; //指向pants对象的指针
public:
person(char *na) //构造函数
{
strcpy(name,na);
}
void wear(shoe & ashoe) //穿着鞋子成员函数
{
myshoe=&ashoe;
}
void wear(clothes & aclothes) //穿着衣服成员函数
{
myclothes=&aclothes;
}
void wear(pants & apants) //穿着裤子成员函数
{
mypants=&apants;
}
void show() //输出一个人的穿着情况
{
cout << " 姓名:" << name << endl;
cout << " 穿着" << myclothes->color << myclothes->type <<",";
cout << mypants->color << mypants->type << "和";
cout << myshoe->color << myshoe->type << endl;
}
};
void main()
{
person p1("王华"),p2("李明");
clothes clothes1("西装","黑色"),clothes2("运动服","蓝色");
pants pants1("西裤","黑色"),pants2("运动裤","蓝色");
shoe shoe1("皮鞋","棕色"),shoe2("运动鞋","黑色");
p1.wear(clothes1);
p1.wear(pants1);
p1.wear(shoe1);
p2.wear(clothes2);
p2.wear(pants2);
p2.wear(shoe2);
cout << "输出结果:" << endl;
p1.show();
p2.show();
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?