شرکت نرم افزاری مبین ملکان

تولید کننده انواع نرم افزارهای کاربردی، ارائه دهنده مشاوره و مقالات تخصصی در زمینه برنامه نویسی


Warning: error_log(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /home/mobinsof/public_html/libraries/simplepie/simplepie.php on line 7523

Warning: error_log(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /home/mobinsof/public_html/libraries/simplepie/simplepie.php on line 7523

Warning: strtotime(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /home/mobinsof/public_html/libraries/joomla/utilities/date.php on line 56

Warning: strtotime(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /home/mobinsof/public_html/libraries/joomla/utilities/date.php on line 56

Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /home/mobinsof/public_html/libraries/joomla/utilities/date.php on line 198

Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /home/mobinsof/public_html/libraries/joomla/utilities/date.php on line 198

مفهوم وراثت در ++C


مثال زیر سورس یک بازی بسیار ساده است که هدف از آن نشان دادن مفهوم وراثت در ++C است که در درس برنامه سازی پیشرفته برای دانشجویان کامپیوتر تدریس می شود، این بازی را در آزمایشگاه برای همکلاسی هایم نوشتم، برنامه دارای یک کلاس به نام ماشین عمومی است که دارای خصوصیاتی مانند رنگ و شکل هست و متدی دارد به نام حرکت، همچنین یک کلاس به نام ماشین جنگی دارد که فقط متد شلیک را داراست، بنابراین مثلاً موقع ایجاد کلاس هواپیما ضرورتی ندارد که خصوصیاتی نظیر رنگ و شکل و متد حرکت و شلیک را دوباره برایش طراحی کنیم بلکه این کلاس کافی است خصوصیات کلاسهای ماشین عمومی و ماشین جنگی را به ارث ببرد تا کامل شود، امیدوارم کدهای زیر مفید بوده باشد.

برای کامپایل بهتر است از توربو سی 3.0 استفاده کنید.

 

#include<conio.h>
#include<iostream.h>
#include<dos.h>
int currx,curry;
class public_machine
{
 private:
 char m_shape;
 int  m_color;
 public:
 public_machine(char machine_shape, int machine_color)
 {
  m_shape=machine_shape;
  m_color=machine_color;
 }
 ~public_machine()
 {
  textcolor(7); //default text color
 }
 void move(int x,int y)
 {
  if (x<0 || x>80) x=0;
  if (y<0 || y>40) y=0;
  currx=x;
  curry=y;
  clrscr();
  gotoxy(x,y);
  textcolor(m_color);
  cprintf(&m_shape);
 }
};
class war_machine
{
 private:
 char m_shape;
 int  m_color;
 public:
 war_machine(char shape,int color)
 {
  m_shape=shape;
  m_color=color;
 }
 ~war_machine()
 {
  textcolor(7);
 }
 void volley()
 {
  clrscr();
  gotoxy(currx,curry);
  textcolor(m_color);
  cprintf(&m_shape);
  textcolor(4); //red
  for (int i=1;i<=4;i++)
  {
    cprintf(".");
    sound(500);
    nosound();
    delay(100);
  }
 }
};
class airplan:public public_machine,public war_machine
{
 private:
 public:
 airplan(char m_shape,int m_color):public_machine(m_shape,m_color),war_machine(m_shape,m_color){}
 ~airplan(){}
};
class tank:public public_machine,public war_machine
{
 private:
 public:
 tank(char m_shape,int m_color):public_machine(m_shape,m_color),war_machine(m_shape,m_color){}
 ~tank(){}
};
class peykan:public public_machine
{
 private:
 public:
 peykan(char m_shape,int m_color):public_machine(m_shape,m_color){}
 ~peykan(){}
};
void play_airplan()
{
 clrscr();
 cout<<"Wellcom to my simple 'Airplan Game'.\n";
 cout<<"Use u,d,l,r to move, and s to volley(shellik).\n";
 cout<<"And x for return to main menu.\nPress any key when ready...";
 getch();
 char c;
 airplan ap('A',9);
 ap.move(40,20);
 do
 {
  c=getch();
  switch (c)
  {
   case 'd' : ap.move(currx,curry+1); // move down
   break;
   case 'u' : ap.move(currx,curry-1); // move up
   break;
   case 'l' : ap.move(currx-1,curry); // move left
   break;
   case 'r' : ap.move(currx+1,curry); // move right
   break;
   case 's' : {ap.volley();ap.move(currx,curry);} // on 's' volley === shellik
   break;
  }
 }
 while (c!='x');
}
void play_tank()
{
 clrscr();
 cout<<"Wellcom to my simple 'Tank Game'.\n";
 cout<<"Use u,d,l,r to move, and s to volley(shellik).\n";
 cout<<"And x for return to main menu.\nPress any key when ready...";
 getch();
 char c;
 tank tk('T',13);
 tk.move(40,20);
 do
 {
  c=getch();
  switch (c)
  {
   case 'd' : tk.move(currx,curry+1); // move down
   break;
   case 'u' : tk.move(currx,curry-1); // move up
   break;
   case 'l' : tk.move(currx-1,curry); // move left
   break;
   case 'r' : tk.move(currx+1,curry); // move right
   break;
   case 's' : {tk.volley();tk.move(currx,curry);} // on 's' volley 
   break;
  }
 }
 while (c!='x');
}
void play_peykan()
{
 clrscr();
 cout<<"Wellcom to my simple 'Peykan Game'.\n";
 cout<<"Use u,d,l,r to move.\n";
 cout<<"And x for return to main menu.\nPress any key when ready...";
 getch();
 char c;
 peykan pk('P',8);
 pk.move(40,20);
 do
 {
  c=getch();
  switch (c)
  {
   case 'd' : pk.move(currx,curry+1); // move down
   break;
   case 'u' : pk.move(currx,curry-1); // move up
   break;
   case 'l' : pk.move(currx-1,curry); // move left
   break;
   case 'r' : pk.move(currx+1,curry); // move right
   break;
  }
 }
 while (c!='x');
}
int main()
{
 char c;
 do
 {
  c=getch();
  clrscr();
  cout<<"(1) Play with Airplan!\n";
  cout<<"(2) Play with Tank!\n";
  cout<<"(3) Play with Peykan!\n";
  cout<<"(4) Exit Game!\n";
  switch (c)
  {
   case '1':play_airplan();
   break;
   case '2':play_tank();
   break;
   case '3':play_peykan();
   break;
  }
 }
 while (c!='4');
 return 0;
}


نظرات

هنوز هیچ نظری ارسال نشده است ! شما اولین نفر باشید

نظرشما؟

 
 
 
 
 

یک محصول تصادفی


Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /home/mobinsof/public_html/libraries/joomla/utilities/date.php on line 198

Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /home/mobinsof/public_html/libraries/joomla/utilities/date.php on line 198

بخش کاربران

ضمانت پرداخت زرین پال


جستجوی سورس کد

ويژوال بيسيک   جاوا

سي پلاس پلاس اي اس پي

آخرین سورس کدها

کانال ما در آپارات

تقویم شمسی


بخشهای فعال


Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /home/mobinsof/public_html/modules/mod_sections/helper.php on line 33

Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /home/mobinsof/public_html/modules/mod_sections/helper.php on line 33

بیشترین بازدید


Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /home/mobinsof/public_html/libraries/joomla/utilities/date.php on line 198

Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /home/mobinsof/public_html/libraries/joomla/utilities/date.php on line 198