Thread: Help with Classes Exercise

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    8

    Help with Classes Exercise

    I'm confused.. here's the homework;

    Note: all of this has to be done in classes.

    Part I
    Define the class Lock. The data members should be private so that the codes may only be altered via the interface functions.

    Part II
    Write a program called locktest.cpp which declares a lock-type variable, initializes its secret code to some value, then enters a loop in which it asks the user for a code to dial and states whether the lock is open or not.

    This was provided by the teacher:
    //
    Member Functions (public)
    Void setCode(int newCode) //Sets the secret code to newcode
    void dialcode(int combo) //Dials combo on the lock
    bool isopen() //Returns true if lock is open and false if not.

    data(private)
    int code //secret code that opens lock
    int setting //The code that has been dialed on lock. If setting matches code, the lock is considered open. Otherwise, lock is closed.
    //

    Can anyone give me hints on where to get started?
    thanks in advance.

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    I would start out here:

    http://www.cprogramming.com/tutorial/lesson12.html

    Teacher you about classes. Is there anything more specific you're confused about?
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Can anyone give me hints on where to get started?

    ummm...a compiler?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Attempt to write the program then post it- we will then help. Remember we will not write the code for you.

    Mr. C.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    8
    Ugh so confused.. here's my attempt.

    Code:
       class Lock
       {
       public:
          void setCode(int newCode);
          void dialCode(int combo);
          bool isOpen();
       
       private:
          int code;
          int setting;
       };
    
       void Lock::setCode(int newCode)
       {
          code=newCode;
       
       
       }
    
       void Lock::dialCode(int combo)
       {
          if(combo=code)
          {
             isOpen();
          }
       
       }
    
       bool Lock::isOpen()
       {
          return true;
       }
    
    
    
    
    
    
       int main()
       {
          Lock loke;
          int a;
          int b;
          cout<<"Enter the secret lock combo"<<endl;
          cin>>a;
          cout<<"Guess the password"<<endl;
          cin>>b;
          loke.setCode(a);
          loke.dialCode(b);
          return(0);
       }
    [Edit by mod - in future, use code tags. You will get a friendlier response]

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    8
    I have a more specific question... How am I suppose to use the bool isOpen() to determine if the combo is correct or not if bool isOpen() does not accept any input?

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    8
    I got this far..


    Code:
    #include<iostream.h>
    
    
               
       class Lock
               
       {
       public:
          void setCode(int newCode);
          void dialCode(int combo);
          bool isOpen();
       
       private:
          int code;
          int setting;
       };
    
               
       void Lock::setCode(int newCode)
               
       {
          code=newCode;
       
       }
    
               
       void Lock::dialCode(int combo)
               
       {
          setting=combo;
       }
    
               
       bool Lock::isOpen()
               
       {
          if(code!=setting)
             return false;
       }
    
    
               
       int main()
               
       {
          Lock lock;
          int a;
          int b;
          cout<<"Enter the password"<<endl;
          cin>>a;
          cout<<"Guess the password"<<endl;
          cin>>b;
          lock.setCode(a);
          lock.dialCode(b);
          bool d=lock.isOpen();
          if(d=true)
          {
             cout<<"Correct!"<<endl;
          }
          else
          {
             cout<<"Incorrect"<<endl;
          }
          return(0);
       }
    But it still doesn't work correct.. if the setting and code are different, its suppose to say "Incorrect". However, it says correct all the time.. any help would be appreciated.

    Oh and I'm using Borland C++ Compiler.
    Last edited by Excalibur XP; 09-26-2002 at 07:10 PM.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>if(d=true)
    ... is wrong, you meant to use this:
    >if(d==true)

    and this:
    Code:
    bool Lock::isOpen()
    {
        if(code!=setting)
           return false;
        else return true;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    8
    Thanks Hammer, that really helps a lot.

    Me and my stupid errors.

    Well, now I can finally get some well needed Zzz..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Line of data input method
    By larry_2k4 in forum C Programming
    Replies: 2
    Last Post: 04-28-2009, 11:34 PM
  2. Help with Classes Exercise
    By mscbuck in forum C++ Programming
    Replies: 13
    Last Post: 12-20-2007, 10:50 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. Classes Exercise (again)
    By Excalibur XP in forum C++ Programming
    Replies: 5
    Last Post: 09-29-2002, 06:11 PM