Using inheritance.

This is a discussion on Using inheritance. within the C++ Programming forums, part of the General Programming Boards category; Using inheritance, this program will allow the creation of different types of soldiers based on the Soldier base class. Each ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    19

    Using inheritance.

    Using inheritance, this program will allow the creation of different types of soldiers based on the Soldier base class. Each type of soldier should handle marching and fireing differently.
    For example:
    A scout would have less ammo, and would use up less stamina to march
    A machine gunner would have more ammo, but go through it quicker
    A flamethrower would use up more stamina by marching


    I'm having trouble trying to call the different classes... I want to be able to say if the user selects scout then less ammo would show up and less stamina would be taken while marching.. instead of the base soldiers ammo and marching.. how and where should i call this


    Code:
    #include<iostream>
    #include<string>
    #include<cstdlib>
    #include<ctime>
    
    using namespace std;
    
    void Seed()
    {
    	srand(time(0));
    }
    
    int GetRandNum(int max)
    {
     return rand() % max + 1;
    }
    
    class Soldier
    {
    public:
    	Soldier(string input);
    	int March();
    	int Fire();
    	void Rest();
    	void LoseStamina(int distance);
    	void Reload();
    	void LoseAmmo(int shots);
    	void DisplayStats();
    
    
    protected: 
    	string m_name;
    	int m_ammo;
    	int m_maxAmmo;
    	int m_stamina;
    	int m_maxStamina;
    };
    
    
    
    Soldier::Soldier(string input)
    {
    	m_name = input;
    	m_stamina = 50;
    	m_maxStamina = m_stamina;
    	m_ammo = 20;
    	m_maxAmmo = m_ammo;
    }
    
    int Soldier::March()
    {
    	return GetRandNum(10);
    }
    
    int Soldier::Fire()
    {
    	return GetRandNum(4);
    }
    
    void Soldier::Rest()
    {
    	m_stamina = m_maxStamina;
    }
    
    void Soldier::Reload()
    {
    	m_ammo = m_maxAmmo;
    }
    
    void Soldier::LoseStamina(int distance)
    {
    	m_stamina -= distance;
    
    	cout << m_name << " you marched " << distance <<  " miles, you lose " << distance << " stamina " << endl;
    
    	if(m_stamina <= 0)
    		cout << m_name << " can't move " << endl;
    	
    	
    }
    
    void Soldier::LoseAmmo(int shots)
    {
      m_ammo -= shots;
    
      cout << m_name << " you shot " << shots << " bullets " << endl;
    
      if (m_ammo <=0)
    	  cout << m_name << " is out of ammo " << endl;
    }
    
    void Soldier::DisplayStats()
    {
    	cout << "Name: " << m_name << endl;
    	cout << "Stamina: " << m_stamina << "/" << m_maxStamina << endl;
    	cout << "Ammo: " << m_ammo << "/" << m_maxAmmo << endl;
    		
    }
    
    class Scout :  public Soldier
    {
          public:
                 Scout(string input);
                 void LoseStamina(int distance);
          private:
                  int m_stamina;
    	          int m_maxStamina;
                  int m_ammo;
                  int m_maxAmmo;
          };
    Scout::Scout(string input) : Soldier(input)
          {
          m_name = input;              
          m_stamina = 100;
          m_maxStamina = m_stamina;              
          m_ammo = 5;
         m_maxAmmo = m_ammo;
      }
    void Scout::LoseStamina(int distance)
          {
          	m_stamina -= distance/2;
    
    	cout << m_name << ", you lost " << distance << " stamina " << endl;
    
    	if(m_stamina <= 0)
    		cout << m_name << " can't move " << endl;
               
          }
    
    class Machiner : public Soldier
    {
          public:
                 Machiner(string input);
                 void LoseStamina(int distance);
                 void LoseAmmo(int shots);
          private:
                  int m_stamina;
                  int m_maxStamina;
    	          int m_ammo;
    	          int m_maxAmmo;
               };
    Machiner::Machiner(string input) : Soldier(input)
          {
             m_stamina = 25;
             m_maxStamina = m_stamina;
             m_ammo = 100;
             m_maxAmmo = m_ammo;
             }
    void Machiner::LoseStamina(int distance)
                {
          	m_stamina -= distance * 2;
    
    	cout << m_name << ", you lost " << distance << " stamina " << endl;
    
    	if(m_stamina <= 0)
    		cout << m_name << " can't move " << endl;
           }
           
    void Machiner::LoseAmmo(int shots)
    {
      m_ammo -= shots * 4;
    
      cout << m_name << " you shot " << shots << " bullets " << endl;
    
      if (m_ammo <=0)
    	  cout << m_name << " is out of ammo " << endl;
    }
    
    
    class Sniper : public Soldier
    {
          public:
                 Sniper(string input);
                 void LoseAmmo(int shots);
          private:
         
         };
          Sniper::Sniper(string input) : Soldier(input)
          {
          };
          
    void Sniper::LoseAmmo(int shots)
      {
      m_ammo - 1;
    
      cout << m_name << " you shot " << shots << " bullets " << endl;
    
      if (m_ammo <=0)
    	  cout << m_name << " is out of ammo " << endl;
    }    
    
    
    
    int main()
    {
    	string name, type;
    	int input, stamina, ammo;
    	bool play = true;
    
    	Seed();
    
    	cout << "Welcome to training soldier" << endl;
    	cout << "Please enter your name" << endl;
    	cin >> name;
    
    	Soldier player(name);
     
    	
    	
    		system("CLS");
    		cout << "Greetings Private " << name << "!" << endl;
    		cout << "Are you a Scout, Machiner, or Sniper Soldier? " << endl;
    		
            cin >> type;
    		{
      if (type == "Scout")
      {
       cout << "You are a scout!" << endl;
       Scout player(name);
       if (type == "Machiner")
       {
         cout << "You are a machiner!" << endl;
         if (type == "Sniper")
         {
                  cout << "You are a sniper!" << endl;        
                       }
                      }
               }
    }
        while(play)	
    	{	
    		
    		
    		
    		cout << "What would you like to do?" << endl;
    		cout << "Press 1 to do marching drills" <<endl;
    		cout << "Press 2 to shoot your weapon" << endl;
    		cout << "Press 3 to rest a bit." << endl;
    		cout << "Press 4 to reload your weapon." << endl;
    		cout << "Press 5 to view your stats." << endl;
    		cin >> input;
    
    	system("CLS");
    	switch(input)
    	{
    	case 1:
             {
                     if(type == "Scout")
                     {
                     Scout.LoseStamina(player.March());
            
               }
          player.LoseStamina(player.March());
          }
    			system("PAUSE");
    		break;
    
    	case 2:
             {
    		player.LoseAmmo(player.Fire());
        }	
    			system("PAUSE");
    		break;
    	case 3:
             {
    		player.Rest();
    		cout << "Your Stamina has been regained" << endl;
    		cout << "Get back to marching soldier!" << endl;
         }
    		system("PAUSE");
    		break;
    	case 4:
             {
    		player.Reload();
    		cout << "Your gun is now reloaded" << endl;
    		cout << "Try hitting the target this time!" << endl;
           }
            system("PAUSE");
    		break;
    	case 5:
             {
    		 player.DisplayStats();
          }
    		 system("PAUSE");
    		break;
    	}
    	}
    
    system("PAUSE");
    return 0;
    
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    You could, for example, put a new constructor into Soldier which is protected, ensuring that only derived classes can access it. Say that you leave it empty. Now your derived classes (ie Scout) can set its own parameters inside its own constructor.

    And because the scout loses less stamina, you could simply override the lose stamina function in Scout. And so on.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I would also consider not using subclasses for that - it appears that all the classes do things pretty much the same way. Instead you could use a couple of parameters (data members that you could initialize differently for different types of soldiers).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    That, I think, is a matter of style.
    Having subclasses for all of them makes it more clear that they're different kinds of soldiers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    I would say the soldiers are not different enought to warrant multiple classes and a complicated inheritance hierarchy. Not to mention that if you want to add soldiers you are now forcing yourself to create new classes which isn't very 'moddable'.

    I would go with anon's suggestion and create a data structure that has the qualities that all soldiers have and then use 1 class to contain that data. In this way you could easily serialize the data and you could add new soldiers by simply passing in new values for the data.

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    19
    The way I have done it essentially works however

    Code:
    #include<iostream>
    #include<string>
    #include<cstdlib>
    #include<ctime>
    
    using namespace std;
    
    void Seed()
    {
    	srand(time(0));
    }
    
    int GetRandNum(int max)
    {
     return rand() % max + 1;
    }
    
    class Soldier
    {
    public:
    	Soldier(string input);
    	int March();
    	int Fire();
    	void Rest();
    	void LoseStamina(int distance);
    	void Reload();
    	void LoseAmmo(int shots);
    	void DisplayStats();
    
    
    protected: 
    	string m_name;
    	int m_ammo;
    	int m_maxAmmo;
    	int m_stamina;
    	int m_maxStamina;
    };
    
    
    
    Soldier::Soldier(string input)
    {
    	m_name = input;
    	m_stamina = 50;
    	m_maxStamina = m_stamina;
    	m_ammo = 20;
    	m_maxAmmo = m_ammo;
    }
    
    int Soldier::March()
    {
    	return GetRandNum(10);
    }
    
    int Soldier::Fire()
    {
    	return GetRandNum(4);
    }
    
    void Soldier::Rest()
    {
    	m_stamina = m_maxStamina;
    }
    
    void Soldier::Reload()
    {
    	m_ammo = m_maxAmmo;
    }
    
    void Soldier::LoseStamina(int distance)
    {
    	m_stamina -= distance;
    
    	cout << m_name << " you marched " << distance <<  " miles, you lose " << distance << " stamina " << endl;
    
    	if(m_stamina <= 0)
    		cout << m_name << " can't move " << endl;
    	
    	
    }
    
    void Soldier::LoseAmmo(int shots)
    {
      m_ammo -= shots;
    
      cout << m_name << " you shot " << shots << " bullets " << endl;
    
      if (m_ammo <=0)
    	  cout << m_name << " is out of ammo " << endl;
    }
    
    void Soldier::DisplayStats()
    {
    	cout << "Name: " << m_name << endl;
    	cout << "Stamina: " << m_stamina << "/" << m_maxStamina << endl;
    	cout << "Ammo: " << m_ammo << "/" << m_maxAmmo << endl;
    		
    }
    
    class Scout :  public Soldier
    {
          public:
                 Scout(string input);
                 void LoseStamina(int distance);
                 void DisplayStats();
          private:
                  int m_stamina;
    	          int m_maxStamina;
                  int m_ammo;
                  int m_maxAmmo;
          };
    Scout::Scout(string input) : Soldier(input)
          {
          m_name = input;              
          m_stamina = 100;
          m_maxStamina = m_stamina;              
          m_ammo = 5;
         m_maxAmmo = m_ammo;
      }
    
    void Scout::LoseStamina(int distance)
          {
          	m_stamina -= distance/10;
    
    	cout << m_name << ", you lost " << distance << " stamina " << endl;
    
    	if(m_stamina <= 0)
    		cout << m_name << " can't move " << endl;
    		}
    void Scout::DisplayStats()
    {
    	cout << "Name: " << m_name << endl;
    	cout << "Stamina: " << m_stamina << "/" << m_maxStamina << endl;
    	cout << "Ammo: " << m_ammo << "/" << m_maxAmmo << endl;
    }
    class Machiner : public Soldier
    {
          public:
                 Machiner(string input);
                 void LoseStamina(int distance);
                 void LoseAmmo(int shots);
                 void DisplayStats();
          private:
                  int m_stamina;
                  int m_maxStamina;
    	          int m_ammo;
    	          int m_maxAmmo;
               };
               
    Machiner::Machiner(string input) : Soldier(input)
          {
             m_stamina = 25;
             m_maxStamina = m_stamina;
             m_ammo = 100;
             m_maxAmmo = m_ammo;
             }
    void Machiner::LoseStamina(int distance)
                {
          	m_stamina -= distance * 2;
    
    	cout << m_name << ", you lost " << distance << " stamina " << endl;
    
    	if(m_stamina <= 0)
    		cout << m_name << " can't move " << endl;
           }
           
    void Machiner::LoseAmmo(int shots)
    {
      m_ammo -= shots * 4;
    
      cout << m_name << " you shot " << shots << " bullets " << endl;
    
      if (m_ammo <=0)
    	  cout << m_name << " is out of ammo " << endl;
    	  
    }
    void Machiner::DisplayStats()
    {
    	cout << "Name: " << m_name << endl;
    	cout << "Stamina: " << m_stamina << "/" << m_maxStamina << endl;
    	cout << "Ammo: " << m_ammo << "/" << m_maxAmmo << endl;
    
    }
    class Sniper : public Soldier
    {
          public:
                 Sniper(string input);
                 void LoseAmmo(int shots);
                 void DisplayStats();
          private:
         
         };
          Sniper::Sniper(string input) : Soldier(input)
          {
          };
          
    void Sniper::LoseAmmo(int shots)
      {
      m_ammo - 1;
    
      cout << m_name << " you shot " << shots << " bullets " << endl;
    
      if (m_ammo <=0)
    	  cout << m_name << " is out of ammo " << endl;
    }    
    void Sniper::DisplayStats()
    {
    	cout << "Name: " << m_name << endl;
    	cout << "Stamina: " << m_stamina << "/" << m_maxStamina << endl;
    	cout << "Ammo: " << m_ammo << "/" << m_maxAmmo << endl;
    }
    
    int main()
    {
    	string name, type = "";
    	int input, stamina, ammo;
    	bool play = true;
    
    	Seed();
    
    	cout << "Welcome to training soldier" << endl;
    	cout << "Please enter your name" << endl;
    	cin >> name;
    	
        cout << "Are you a Scout, Machiner, or Sniper Soldier? " << endl;
    	cin >> type;
    	{
    Soldier Player(name);  right here is where my problem is. when i switch each of these out for the other the program will run as if the player is a soldier scout or machiner i've tried to make an if statement making it possible to activate the one that is typed in but i can't get it to work.. so if i type in Scout i want the Scout Player(name) to run.. but it keeps giving me errors in my if statements.. any ideas.
    // Scout Player(name);
    // Machiner Player(name);
    // Sniper Player(name);
    
      if (type == "Scout")
      {
      cout << "You are a scout!" << endl;
       
       if (type == "Machiner")
       {
       cout << "You are a machiner!" << endl;
         if (type == "Sniper")
         {
          cout << "You are a sniper!" << endl;        
                   	}
    	}
      } 
     	system("CLS");  
        cout << "Greetings Private " << name << " you are a " << type << endl;  
           
           
        while(play)	
    	{	
    		
    		cout << "What would you like to do?" << endl;
    		cout << "Press 1 to do marching drills" <<endl;
    		cout << "Press 2 to shoot your weapon" << endl;
    		cout << "Press 3 to rest a bit." << endl;
    		cout << "Press 4 to reload your weapon." << endl;
    		cout << "Press 5 to view your stats." << endl;
    		cin >> input;
    
    	system("CLS");
    	switch(input)
    	{
    	case 1:
          {
                       
          player.LoseStamina(player.March());
          }
    			system("PAUSE");
    		break;
    
    	case 2:
             {
    		player.LoseAmmo(player.Fire());
        }	
    			system("PAUSE");
    		break;
    	case 3:
             {
    		player.Rest();
    		cout << "Your Stamina has been regained" << endl;
    		cout << "Get back to marching soldier!" << endl;
         }
    		system("PAUSE");
    		break;
    	case 4:
             {
    		player.Reload();
    		cout << "Your gun is now reloaded" << endl;
    		cout << "Try hitting the target this time!" << endl;
           }
            system("PAUSE");
    		break;
    	case 5:
             {
    		 player.DisplayStats();
          }
    		 system("PAUSE");
    		break;
    
                      }
                      }
    system("PAUSE");
    return 0;
    }

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    If 'works' is all your after then any solution will do. After all, it's just code, and you can make it do what you want how you want. Not a great approach but it gets the job done.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. conditional inheritance?
    By arcaine01 in forum C++ Programming
    Replies: 17
    Last Post: 09-04-2009, 05:12 PM
  2. Replies: 16
    Last Post: 06-08-2009, 04:03 PM
  3. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  4. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 01:46 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21