Having problems with objects and classes
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Having problems with objects and classes

  1. #1
    Join Date
    Nov 2009
    Posts
    24

    Having problems with objects and classes

    hi, I'm having problems with the code below. Can someone help me? When I run the program, I am unable to get the output of GetMissile, GetLaser,.. Also, instead of displaying the correct 'prices' for, for example You.MissilePrice(), I get numbers that don't make sense, such as -858993460

    Code:
    #include <iostream>
    using namespace std;
    
    
    
    class Aircraft
    {
    public:
            Aircraft(){cout<<"aircraft constructor"<<endl;}
            virtual ~Aircraft(){cout<<"aircraft destructor"<<endl;}
    
    protected:
    	string Missile;
    	string Laser;
    	string Bomb;
    	string Propulsion;
    	int Price;
    };
    
    class Jet : public Aircraft
    {
    public:
    	 string GetMissile() {return Missile;}
    	 int MissilePrice() {return Price;}
    
    	 string GetLaser() {return Laser;}
    	 int LaserPrice() {return Price;}
    
    	 string GetBomb() {return Bomb;}
    	 int BombPrice() {return Price;}
    
    	 string GetPropulsion() {return Bomb;}
    	 int PropulsionPrice() {return Price;}
    
    	 void SetMissile(int MissileChoice)
    	 {
    		 switch(MissileChoice)
    		 {
    		 case '0':
    			 Missile = "AA"; 
    			 Price = 150;
    		 case '1':
    			 Missile = "AS"; 
    			 Price = 170;
    		 case '2':
    			 Missile = "ASM"; 
    			 Price = 170;
    		 case '3':
    			 Missile = "ATM"; 
    			 Price = 150;
    		 }
    	 }
    	 void SetLaser(int LaserChoice)
    	 {
    		 switch(LaserChoice)
    		 {
    		 case '0':
    			 Laser = "HF"; 
    			 Price = 50;
    		 case '1':
    			 Laser = "DF"; 
    			 Price = 70;
    		 case '2':
    			 Laser = "CO"; 
    			 Price = 70;
    		 case '3':
    			 Laser = "AGI"; 
    			 Price = 50;
    		 }
    	 }
    	void SetBomb(int BombChoice)
    	 {
    		 switch(BombChoice)
    		 {
    		 case '0':
    			 Bomb = "CB"; 
    			 Price = 300;
    		 case '1':
    			 Bomb = "COB"; 
    			 Price = 400;
    		 case '2':
    			 Bomb = "IC"; 
    			 Price = 400;
    		 case '3':
    			 Bomb = "GP"; 
    			 Price = 350;
    		 }
    	 }
    	void SetPropulsion(int PropulsionChoice)
    	 {
    		 //Jet.Propulsion
    		 switch(PropulsionChoice)
    		 {
    		 case '0':
    			 Propulsion = "SP"; 
    			 Price = 375;
    		 case '1':
    			 Propulsion = "LRP"; 
    			 Price = 400;
    		 case '2':
    			 Propulsion = "EP"; 
    			 Price = 200;
    		 case '3':
    			 Propulsion = "NP"; 
    			 Price = 300;
    		 }
    	 }
    //protected:
    };
    
    
    int main()
    {
    	Jet You;
    	int MissileChoice, LaserChoice, BombChoice, PropulsionChoice;
    	cout<<"choose the missile, laser, bomb, propulsion of the jet"<<endl;
    	cin>>MissileChoice;
    	cin>>LaserChoice;
    	cin>>BombChoice;
    	cin>>PropulsionChoice;
    	You.SetMissile(MissileChoice);
    	You.SetLaser(LaserChoice);
    	You.SetBomb(BombChoice);
    	You.SetPropulsion(PropulsionChoice);
    	cout<<"you chose ";
    	You.GetMissile();
    	You.GetLaser();
    	You.GetBomb();
    	You.GetPropulsion();
    	cout<<"total price is "<<You.MissilePrice()<<" + "<<You.LaserPrice()<<" + ";
    	cout<<You.BombPrice()<<" + "<<You.PropulsionPrice()<<" = ";
    	cout<<You.MissilePrice()+You.LaserPrice()+You.BombPrice()+You.PropulsionPrice();
    
    	cin.ignore();
    	cin.ignore();
    
    
    return 0;
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,424

    Re: Having problems with objects and classes

    Quote Originally Posted by larry burns View Post
    hi, I'm having problems with the code below. Can someone help me? When I run the program, I am unable to get the output of GetMissile, GetLaser,.. Also, instead of displaying the correct 'prices' for, for example You.MissilePrice(), I get numbers that don't make sense, such as -858993460
    Did you run the program using your debugger? If you did, then you would see the obvious mistake if you single-stepped through the code:
    Code:
     {
     case '0':
    	 Missile = "AA"; 
      	 Price = 150;
     case '1':
    	 Missile = "AS"; 
    	 Price = 170;
     case '2':
    	 Missile = "ASM"; 
    	 Price = 170;
     case '3':
    	 Missile = "ATM"; 
    	 Price = 150;
    You make the same mistake in all of the other switch() statements. Do you see the problem? How does switch/case work in C++? Isn't there a "break" statement that goes along with all of this? Again, if you did single step through the program, you would see that the case statements just flow right into each other, which is not correct.

    Second, the Price member is uninitialized when you create your objects. This means that it can have any random value on object creation. What if none of those case statements are executed? What happens to the Price member? It never gets set to anything. Initialize your member variables:
    Code:
    class Aircraft
    {
    public:
            Aircraft() : Price(0)
            {cout<<"aircraft constructor"<<endl;}
    Now Price is set to 0 when an Aircraft is created instead of Price being any garbage value.

    But the bottom line is that the debugger would reveal all of this to you. That's why it is imperative that you know how to use the debugger for your compiler suite, so that you don't need to ask questions and wait (either patiently or impatiently) for answers on a programming board.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 14th, 2012 at 07:13 PM.

  3. #3
    Join Date
    Sep 2007
    Location
    Calcutta, India
    Posts
    95

    Re: Having problems with objects and classes

    Dear Larry,

    If Paul's reply has worked for you, it is kind of mandatory in this forum to mark your thread as [RESOLVED] from your control panel.

    This is also a good practice. So do it right away!!!!

    Indrajit

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center