what am i doing wrong : object orientated programming

This is a discussion on what am i doing wrong : object orientated programming within the C++ Programming forums, part of the General Programming Boards category; i basicly want to create a function wich returns a value if value is correct then say welcome to the ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    56

    what am i doing wrong : object orientated programming

    i basicly want to create a function wich returns a value if value is correct then say welcome to the lines ..

    this is my coder can any1 help me im a bit confused

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <fstream>
    
    // encode
    class test{
    public:
    int test::acces();
    int test::menu();
    int acces_response;
    };
    
    int test::acces(){
    cout <<"acces granted 0.0 must be your lucky day";
    int acces_response = 123  ;
    
    }
    
    int test::menu(){
    cout <<"1. encode\n2. decode\n3. calculator\n4.credits";
    
    }
    
    // decode
    
    // acces
    
    // main function
    int main(){
    test test   ;
    int x;
    ofstream a_file ( "LOG.txt" );
    a_file<<"starting program at \n";  // still need to add date !!!
    cout <<"(c) 2004 \n";
    a_file<<"log : function open acces() \n";
    int acces = test.acces();
    if (test::acces_response == 123){
    cout <<"test succesfull... \n welcome to the lines soldier";
    cin>>x;
    }
    }
    if x == y , y == x

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    First of all, you're mixing old-style C++ and modern C++. This is how it's done now:

    Code:
     #include <iostream>
     #include <stdlib>
     #include <fstream>
     
     using namespace std;
    Most compilers are friendly and will accept confusion like this, but it's a good habit to know the difference and start using it.

    If you were including some C libraries, you also drop the .h from the header file and precede the name with a C, for example "#include <cstdlib>".

    You specify int as the return type for all your functions, but you never actually return anything. You use the return statement to return a value. You can use
    Code:
    return 0;
    And just specify a constant. You would normally do things like this inside a conditional (if-statement), and in main functions this is just used to end the program and confirm program success with the operating system (0 is used for this, other values can be defined by you for different types of errors). You can also calculate the value you want to return, for example, in a mathematical function, and return that variable.

    In your class defintion, you don't need the test:: before each function name. You should probably also know that you're spelling acess wrong - it has 2 s's. From a coding standpoint, spelling doesn't matter as long as you're consistent - but it's good to know when you're dealing with communications with the user.
    Last edited by sean; 11-20-2004 at 01:29 PM. Reason: typos

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    56
    thx

    so all i needed to do was
    Code:
    return acces_response;
    Code:
    using namespace std;
    never used it :/ im using dev-cpp 4.0 and it has no problems with it
    Last edited by gamer; 11-20-2004 at 01:33 PM. Reason: fixed code tags
    if x == y , y == x

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Code:
    class test{
    public:
    int test::acces();
    int test::menu();
    int acces_response;
    };
    Should be:
    Code:
    class test {
       public:
          int access();
          int menu();
          int access_response;
    };
    Code:
    test test   ;
    Give your object unambiguous names:
    Code:
    test obj;
    Code:
    test::acces_response
    Should be
    Code:
    obj.access_response
    For future reference, it helps to
    a) indent your code
    b) let us know what errors/problems you are having with the code
    c) what it should do (if it isn't obvious from context)
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 11:59 PM
  2. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 08:49 AM
  3. synchronization object choosing
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 05:33 AM
  4. object orientated program
    By wart101 in forum C++ Programming
    Replies: 9
    Last Post: 01-08-2006, 02:42 AM
  5. Linked List Templates and Object Types
    By ventolin in forum C++ Programming
    Replies: 10
    Last Post: 06-16-2004, 01:05 PM

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