More about classes.

This is a discussion on More about classes. within the C++ Programming forums, part of the General Programming Boards category; Ok this one is a doosey! I don't know if anyone will understand but I will try my best to ...

  1. #16
    1479
    Join Date
    Aug 2003
    Posts
    253
    Ok this one is a doosey! I don't know if anyone will understand but I will try my best to explain what I want done.

    Here is an example class:
    Code:
    class Car
        {
            public:
                int StartUp(int gasInTank);
                void PutInGear();
                void Accelerate();
                void SlowDown();
                void FloorIT();
                char GetMake(char* carMake);
                char GetModel(char* carModel);
                int GetYearMade();
            private:
                int YearMade;
                char Model;
                char Make;
        };
    Could I make it where if gasInTank<=0 then they could not PutInGear? Not sure if that sounds clear to anyone. Basically I want the function StartUp to determine, based on user input, if there is enough gass in the car to be able to use the PutInGear Function. I think I saw this in another thread, will I have to use bool type arguments? Such as:
    Code:
    Car::StartUp(int gasInTank)
       {
         if (gasInTank<=0)
          { 
            StartUp = fail
            cout <<"You need more gas to start your car" <<endl;
          }
          else
               cout <<"Your car starts-up smoothly" <<endl;
         }
    In other words: If StartUp fails then the user will not be able to PutInGear.

    If this makes anysense at all please give me INFORMATION (i.e. threads, links, google search keywords, to things that I will need to know in order to pull this off. (if its possible)
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #17
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    1)First of all, the most intuitive way to declare an array function parameter is like this:

    void my_func(char my_array[]);

    It is more intuitive because you're declaring the array like this:

    char my_array[30];

    so the syntax is similar, and you don't have to confuse yourself with pointers.

    2)You're the programmer aren't you? You're the one that's going to be calling the functions for your class, right? So, don't call the PutInGear() function if the conditions aren't met. After all the user doesn't enter input like:

    my_car.PutInGear()

    do they? It's not like the user gets to decide what's going to happen.
    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	switch (menu_choice)
    	{
    	case 1:
    		...
    
    	case 2:
    		...
    
    	case 3:
    		if(gas<=0)
    			cout<<"Sorry, you can't put "
    				<<"the car in gear: no gas"<<endl;
    		else
    			my_Car.PutInGear();
    		break;
    
    	default:
    
    	}
    
    	return 0;			 
    }
    3) You don't have any constructors for your class nor any SetMember() functions, so that might be a problem seeing how your data members are private.
    Last edited by 7stud; 08-16-2003 at 03:55 AM.

  3. #18
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by FillYourBrain
    I get the idea you like Stl, Cat.
    I like the STL, boost, etc. Their containers and algorithms, besides being incredible time savers, make it vastly easier to write memory-leak free code, make it easier to declare and use dynamically sized multidimentional arrays, make it easier to write exception-safe code, etc. They make it safe to return a pointer to a newly allocated resource without the possibility of a leak, and in general, they're not only safer, but more elegant and readable as well.

  4. #19
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    class Car
        {
            public:
                int StartUp(int gas);
                void PutInGear();
                void Accelerate();
                void SlowDown();
                void FloorIT();
                char GetMake(char* carMake);
                char GetModel(char* carModel);
                int GetYearMade();
            private:
                int YearMade;
                char Model;
                char Make;
               int gasInTank;
        };
    You know all your member functions can access the private variables...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 11:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM

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