class CAnimal
{
public:
CAnimal(){cout<<"Constructor of Animal"<<endl;};
virtual ~CAnimal(){cout<<"Destructor of Animal"<<endl;};
void Run();
void Clone();
private:
int m_age;
double m_weight;
};
class CHair
{
public:
std::string GetColor(){return this->m_color;};
void SetColor(std::string str){this->m_color = str;};
private:
std::string m_color;
};
class Chorse:public CAnimal
{
public:
Chorse(){cout<<"Constructor of horse"<<endl;};
~Chorse(){cout<<"Destructor of horse"<<endl;}
private:
CHair m_hair;//组合
};
void main()
{
Chorse horse;//此时可以看到构造和析构的顺序
horsem_hair;//访问子类自身变量
horseClone();//访问父类继承方法
}
一字一字敲出来得哦,望采纳。
基本思路:
蛇每吃一个食物蛇身子就增加一格,用UP, DOWN, LEFT, RIGHT控制蛇头的运动,而蛇身子跟着蛇头走,每后一格蛇身子下一步走到上一格蛇身子的位置,以此类推。
#include <stdioh>
#include <conioh>
#include <windowsh>
#define BEG_X 2
#define BEG_Y 1
#define WID 20
#define HEI 20
HANDLE hout;
typedef enum {UP, DOWN, LEFT, RIGHT} DIR;
typedef struct Snake_body
{
COORD pos;//蛇身的位置
struct Snake_body next;//下一个蛇身
struct Snake_body prev;//前一个蛇身
}SNAKE, PSNAKE;
PSNAKE head = NULL;//蛇头
PSNAKE tail = NULL;//蛇尾
//画游戏边框的函数
void DrawBorder()
{
int i, j;
COORD pos = {BEG_X, BEG_Y};
for(i = 0; i < HEI; ++i)
{
SetConsoleCursorPosition(hout, pos);
for(j = 0; j < WID; ++j)
{
if(i == 0)//第一行
{
if(j == 0)
printf("┏");
else if(j == WID - 1)
printf("┓");
else
printf("━");
}
else if(i == HEI - 1)//最后一行
{
if(j == 0)
printf("┗");
else if(j == WID - 1)
printf("┛");
else
printf("━");
}
else if(j == 0 || j == WID - 1)//第一列或最后一列
printf("┃");
else
printf(" ");
}
++posY;
}
}
//添加蛇身的函数
void AddBody(COORD pos)
{
PSNAKE pnew = (PSNAKE)calloc(1, sizeof(SNAKE));
pnew->pos = pos;
if(!head)
{
head = tail = pnew;
}
else
{
pnew->next = head;//新创建蛇身的next指向原先的蛇头
head->prev = pnew;//原先的蛇头的prev指向新创建的蛇身
head = pnew;//把新创建的蛇身作为新的蛇头
}
SetConsoleCursorPosition(hout, head->pos);
printf("◎");
}
//蛇身移动的函数
void MoveBody(DIR dir)
{
PSNAKE ptmp;
COORD pos = head->pos;
switch(dir)
{
case UP:
if(head->posY > BEG_Y + 1)
--posY;
else
return;
break;
case DOWN:
if(head->posY < BEG_Y + HEI - 2)
++posY;
else
return;
break;
case LEFT:
if(head->posX > BEG_X + 2)
posX -= 2;
else
return;
break;
case RIGHT:
if(head->posX < BEG_X + (WID - 2) 2)
posX += 2;
else
return;
break;
}
AddBody(pos);//添加了一个新的蛇头
ptmp = tail;//保存当前的蛇尾
tail = tail->prev;
if(tail)
tail->next = NULL;
SetConsoleCursorPosition(hout, ptmp->pos);
printf(" ");
free(ptmp);
}
int main()
{
int ctrl;
DIR dir = RIGHT;//初始蛇的方向是向右的
COORD pos = {BEG_X + 2, BEG_Y + HEI / 2};
system("color 0E");
system("mode con cols=90 lines=30");
hout = GetStdHandle(STD_OUTPUT_HANDLE);
printf(" ------------贪吃蛇的移动------------");
DrawBorder();
//自定义几个蛇的身体
AddBody(pos);
posX += 2;
AddBody(pos);
posX += 2;
AddBody(pos);
posX += 2;
AddBody(pos);
posX += 2;
AddBody(pos);
posX += 2;
AddBody(pos);
posX += 2;
AddBody(pos);
//控制蛇的移动
while(ctrl = getch())
{
switch(ctrl)
{
case 'w':
if(dir == DOWN)
continue;
dir = UP;
break;
case 's':
if(dir == UP)
continue;
dir = DOWN;
break;
case 'a':
if(dir == RIGHT)
continue;
dir = LEFT;
break;
case 'd':
if(dir == LEFT)
continue;
dir = RIGHT;
break;
case 'q':
return 0;
}
MoveBody(dir);
}
return 0;
}
扩展资料:
实现逻辑
1,可以设置光标,就能实现制定位置打印制定符号。
2,涉及一个结构体,包含两个元素坐标元素和一个结构体指针。
3,结构体串联形成链表,遍历获取成员坐标,打印符号得到蛇身。
4,不断的加头,去尾,重新遍历坐标,再打印形成蛇的移动。
5,食物产生的位置判定,不能越界,也不能与蛇身体重合。
6,蛇的转向判定,一条规则,不允许倒退。
7,转向的实现,跟行进方向决定新的关节坐标(当前头的上下左右)
8,死亡检测,是否头节点坐标是否与墙壁重合,是否与身体其他关节重合。
9,加速减速,设置刷新休眠时间实现。
参考资料来源:百度百科-C语言
#include <iostream>
#include <string>
using namespace std;
class animal
{
public:
animal(string n){ name = n; };
string GetName(){ return name; };
virtual float GetWeight()= 0;
private :
string name;
};
class dog :public animal
{
public:
dog(float w, string n) :animal(n){ weight = w; };
float GetWeight() { return weight; };
private:
float weight;
};
class cat : public animal
{
public:
cat(float w, string n) :animal(n){ weight = w; };
float GetWeight() { return weight; };
private:
float weight;
};
void main()
{
dog d(2,"dog1");
cat c(1, "cat1");
cout << dGetName() << ":" << dGetWeight() << "KG" << endl;
cout << cGetName() << ":" << cGetWeight() << "KG" << endl;
}
以上就是关于试设计一个动物类CAnimal ,它包含年龄,体重等属性,并具有跑, 叫, 生长, 克隆等能力;并派生出马类Ch全部的内容,包括:试设计一个动物类CAnimal ,它包含年龄,体重等属性,并具有跑, 叫, 生长, 克隆等能力;并派生出马类Ch、c语言 贪吃蛇 程序、编写C++程序:声明一个动物类animal,再由此派生出狗类Dog和猫类Cat。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)