UNCLE_CRAPER

Abstract Base Class, private inheritance and multiple public

May 19th, 2015
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstring>
  4.  
  5. using std::cout;
  6. using std::endl;
  7. using std::cin;
  8.  
  9. class Card
  10. {
  11.     enum{ red, white, black, green };
  12.     enum{ pika, buba, tref, cherv };
  13.     int suit;
  14.     int back;
  15. public:
  16.     Card() { srand((unsigned)time(NULL)); suit = rand() % 4 + 1; back = rand() % 4 + 1; };
  17.     Card(int s, int b) { suit = s; back = b; };
  18.     Card Draw()const { return *this; };
  19.     void Show()const { cout << "Suit: " << suit << endl << "Back: " << back << endl; }
  20. };
  21. class Person
  22. {
  23.     char * name;
  24.     char * second_name;
  25. protected:
  26.     virtual void Data()const = 0;
  27. public:
  28.     Person();
  29.     Person(char * nm, char * scnd_nm = "none");
  30.     Person(const Person & obj);
  31.     Person & operator=(const Person & obj);
  32.     virtual void Show()const = 0;
  33.     virtual ~Person() = 0;
  34. };
  35. class Gunslinger : virtual /*public*/ Person
  36. {
  37.     double time;
  38.     int notches;
  39. protected:
  40.     void Data()const;
  41. public:
  42.     Gunslinger();
  43.     Gunslinger(double tm, int ntchs, char * nm = "none", char * scnd_nm = "none");
  44.     Gunslinger(double tm , int ntchs, const Person & obj);
  45.     Gunslinger(const Gunslinger & obj);
  46.     Gunslinger & operator=(const Gunslinger & obj);
  47.     Gunslinger & oprtr(const Gunslinger & obj);
  48.     double Draw()const { cout << "Time: "; return time; }
  49.     void Show()const;
  50.     virtual ~Gunslinger(){};
  51. };
  52. class PokerPlayer : virtual /*public*/ Person
  53. {
  54.     Card card;
  55. protected:
  56.     void Data()const;
  57. public:
  58.     PokerPlayer();
  59.     PokerPlayer(char * nm, char * scnd_nm = "none");
  60.     PokerPlayer(const Person & obj);
  61.     PokerPlayer(const PokerPlayer & obj);
  62.     PokerPlayer & operator=(const PokerPlayer & obj);
  63.     PokerPlayer & oprtr(const PokerPlayer & obj);
  64.     Card Draw()const { card.Show(); return card; }
  65.     void Show()const{ Person::Show(); card.Show(); };
  66.     virtual ~PokerPlayer(){};
  67. };
  68. class BadDude : public Gunslinger, public PokerPlayer
  69. {
  70. protected:
  71.     void Data()const{};
  72. public:
  73.     BadDude();
  74.     BadDude(double tm, int ntchs, char * nm = "none", char * scnd_nm = "none");
  75.     BadDude(double tm, int ntchs, const ::Person & obj);
  76.     BadDude(const BadDude & obj);
  77.     BadDude & operator=(const BadDude & obj);
  78.     Card Cdraw()const { return PokerPlayer::Draw(); }
  79.     double Gdraw()const { return Gunslinger::Draw(); }
  80.     void Show()const;
  81.     virtual ~BadDude(){};
  82. };
  83.  
  84. /////////////////////////////////////////////////////////////SOURCE FILE////////////////////////////////////////////////////
  85.  
  86. #include "person.h"
  87.  
  88. void Person::Data()const
  89. {
  90.     cout << "Name: " << name << "\n";
  91.     cout << "Second name: " << second_name << "\n";
  92. }
  93. Person::Person()
  94. {
  95.     name = new char[strlen("none") + 1];
  96.     second_name = new char[strlen("none") + 1];
  97.  
  98.     strcpy_s(name, strlen("none") + 1 * sizeof(char), "none");
  99.     strcpy_s(second_name, strlen("none") + 1 * sizeof(char), "none");
  100. }
  101. Person::Person(char * nm, char * scnd_nm)
  102. {
  103.     name = new char[strlen(nm) + 1];
  104.     second_name = new char[strlen(scnd_nm) + 1];
  105.  
  106.     strcpy_s(name, strlen(nm) + 1 * sizeof(char), nm);
  107.     strcpy_s(second_name, strlen(scnd_nm) + 1 * sizeof(char), scnd_nm);
  108.    
  109. }
  110. Person::Person(const Person & obj)
  111. {
  112.     name = new char[strlen(obj.name) + 1];
  113.     second_name = new char[strlen(obj.second_name) + 1];
  114.  
  115.     strcpy_s(name, strlen(obj.name) + 1 * sizeof(char), obj.name);
  116.     strcpy_s(second_name, strlen(obj.second_name) + 1 * sizeof(char), obj.second_name);
  117. }
  118. Person & Person::operator=(const Person & obj)
  119. {
  120.     if (this == & obj)
  121.         return *this;
  122.  
  123.     delete[]name;
  124.     delete[]second_name;
  125.  
  126.     name = new char[strlen(obj.name) + 1];
  127.     second_name = new char[strlen(obj.second_name) + 1];
  128.  
  129.     strcpy_s(name, strlen(obj.name) + 1 * sizeof(char), obj.name);
  130.     strcpy_s(second_name, strlen(obj.second_name) + 1 * sizeof(char), obj.second_name);
  131.  
  132.     return *this;
  133. }
  134. void Person::Show()const
  135. {
  136.     cout << "Name: " << name << "\n";
  137.     cout << "Second name: " << second_name << "\n";
  138. }
  139. Person::~Person()
  140. {
  141.     delete[]name;
  142.     delete[]second_name;
  143. }
  144. ////////////////////////////////////////////////////////////////////////////////////////////////
  145. void Gunslinger::Data()const
  146. {
  147.     cout << "Time: " << time << "\n";
  148.     cout << "Notches: " << notches << "\n";
  149. }
  150. Gunslinger::Gunslinger() : Person()
  151. {
  152.     time = 0;
  153.     notches = 0;
  154. }
  155. Gunslinger::Gunslinger(double tm, int ntchs, char * nm, char * scnd_nm) : Person(nm, scnd_nm)
  156. {
  157.     time = tm;
  158.     notches = ntchs;
  159. }
  160. Gunslinger::Gunslinger(double tm, int ntchs, const Person & obj) : Person(obj)
  161. {
  162.     time = tm;
  163.     notches = ntchs;
  164. }
  165. Gunslinger::Gunslinger(const Gunslinger & obj) : Person(obj)
  166. {
  167.     time = obj.time;
  168.     notches = obj.notches;
  169. }
  170. Gunslinger & Gunslinger::operator=(const Gunslinger & obj)
  171. {
  172.     if (this == &obj)
  173.         return *this;
  174.     Person::operator=(obj);
  175.  
  176.     time = obj.time;
  177.     notches = obj.notches;
  178.  
  179.     return *this;
  180. }
  181. Gunslinger & Gunslinger::oprtr(const Gunslinger & obj)
  182. {
  183.     if (this == &obj)
  184.         return *this;
  185.  
  186.     time = obj.time;
  187.     notches = obj.notches;
  188.  
  189.     return *this;
  190. }
  191. void Gunslinger::Show()const
  192. {
  193.     Person::Data();
  194.     cout << "Time: " << time << "\n";
  195.     cout << "Notches: " << notches << "\n";
  196. }
  197. /////////////////////////////////////////////////////////////////////////////////////////////////
  198. void PokerPlayer::Data()const
  199. {
  200.     card.Show();
  201. }
  202. PokerPlayer::PokerPlayer() : Person(), card(){}
  203. PokerPlayer::PokerPlayer(char * nm, char * scnd_nm) : Person(nm, scnd_nm) {}
  204. PokerPlayer::PokerPlayer(const Person & obj) : Person(obj), card() {}
  205. PokerPlayer::PokerPlayer(const PokerPlayer & obj) : Person(obj)
  206. {
  207.     card = obj.card;
  208. }
  209. PokerPlayer & PokerPlayer::operator=(const PokerPlayer & obj)
  210. {
  211.     if (this == &obj)
  212.         return *this;
  213.     Person::operator=(obj);
  214.  
  215.     card = obj.card;
  216.  
  217.     return *this;
  218. }
  219. PokerPlayer & PokerPlayer::oprtr(const PokerPlayer & obj)
  220. {
  221.     if (this == &obj)
  222.         return *this;
  223.  
  224.     card = obj.card;
  225.  
  226.     return *this;
  227. }
  228. ///////////////////////////////////////////////////////////////////////////////////////////
  229. BadDude::BadDude() : Person::Person(), Gunslinger(), PokerPlayer() {}
  230. BadDude::BadDude(double tm, int ntchs, char * nm, char * scnd_nm)
  231.     : Person::Person(nm, scnd_nm), Gunslinger(tm, ntchs, nm, scnd_nm), PokerPlayer(nm, scnd_nm) {}
  232. BadDude::BadDude(double tm, int ntchs, const ::Person & obj)
  233.     : Person::Person(obj), Gunslinger(tm, ntchs, obj), PokerPlayer(obj) {}
  234. BadDude::BadDude(const BadDude & obj) : Person::Person(obj), PokerPlayer(obj), Gunslinger(obj) {}
  235.  
  236. BadDude & BadDude::operator=(const BadDude & obj)
  237. {
  238.     if (this == &obj)
  239.         return *this;
  240.  
  241.     Person::operator=(obj);
  242.     PokerPlayer::oprtr(obj);
  243.     Gunslinger::oprtr(obj);
  244.    
  245.     return *this;
  246. }
  247. void BadDude::Show()const
  248. {
  249.     Person::Data();
  250.     Gunslinger::Data();
  251.     PokerPlayer::Data();
  252. }
  253.  
  254.  
  255. /////////////////////////////////////////////////////////main file////////////////////////////////////////////////////////
  256.  
  257. #include "person.h"
  258. #include "vld.h"
  259.  
  260. void main()
  261. {
  262.     const int size=3;
  263.     Person *data[size];
  264.     Gunslinger gun;
  265.     BadDude obj2(11.2, 23, "Ricky", "Rud");
  266.     BadDude obj1 = obj2;
  267.  
  268.     for (int i = 0; i < size;)
  269.     {
  270.         int choose = 0;
  271.         cout << "Enter 1: gun, 2: poker, 3:baddude => ";
  272.         cin >> choose;
  273.  
  274.         switch (choose)
  275.         {
  276.         case 1:
  277.             data[i] = new Gunslinger(25.5, 15, "Jonh", "Travolta");
  278.             i++;
  279.             break;
  280.         case 2:
  281.             data[i] = new PokerPlayer("Jimmi", "Bgood");
  282.             i++;
  283.             break;
  284.         case 3:
  285.             data[i] = new BadDude(6.6, 2, "Billy", "Bobby");
  286.             i++;
  287.             break;
  288.         }
  289.     }
  290.    
  291.     //gun = (Gunslinger)*data[0];
  292.     gun.Show();
  293.     cout << "!!!!!!!!!!!!!!!!!!!!!!!\n";
  294.  
  295.     /*obj1 = obj2;
  296.     obj2.Show();
  297.     obj1.Show();*/
  298.  
  299.     for (int i = 0; i < size; i++)
  300.     {
  301.         data[i]->Show();
  302.         cout << "!!!!!!!!!!!!!!!!!!!!!!!\n";
  303.     }
  304.  
  305.     for (int i = 0; i < size; i++)
  306.         delete data[i];
  307.  
  308.     cout << "Bye\n";
  309. }
Add Comment
Please, Sign In to add comment