functions

This is a discussion on functions within the C++ Programming forums, part of the General Programming Boards category; say if i had one header file that defined all the function prototypes and global variables for each class.. something ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    91

    functions

    say if i had one header file that defined all the function prototypes and global variables for each class..
    something like
    Code:
    //blah.h
    
    #ifndef _blah_h
    #define _blah_h
    
    class blah1 {
    public:
     int b1();
     int b2();
    private:
     int b;
    };
    
    class blah2 {
    public:
     int b3();
     int b4();
    };
    
    class blah3 : public blah2 {
    public:
     int b5();
    };
    
    #endif
    does this appear correct ?

    if it is correct then in my blah.cpp file where i go to actually implement these functions how would it look?

    somedthing like...
    Code:
    //blah.cpp
    
    #include "blah.h"
    
    int b1() {
     return 1;
    }
    
    int b2() {
     return 2;
    }
    
    int b3() {
     return 3;
    }
    
    int b4() {
     return 4;
    }
    
    int b5() {
     return 5;
    }
    and if that is correct, then what about when im using inheritance but the i would like to re-write a function how would it know if im calling the parents implementation of a function or a subclass'es implemetation of that function?

    thanks

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Well thats not how you impliment them you have to do it like this
    Code:
    int blah1::b1() {
     return 1;
    }
    
    int blah1::b2() {
     return 2;
    }
    
    int blah2:: b3() {
     return 3;
    }
    
    int blah2::b4() {
     return 4;
    }
    
    int blah3:: b5() {
     return 5;
    }
    and if your using inheritance and you want to re-write a function then you don't really need to use inheritance
    Woop?

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    thanks for that, that way makes more sense than what i had.. teehee

    and if your using inheritance and you want to re-write a function then you don't really need to use inheritance
    what do you mean by this?
    so if i had something like
    Code:
    //blah.h without all the ifndef crap..
    class blah2 
    {
       public:
              virtual int b1();
    };
    
    class blah1 : public blah2
    {
       public:
                 int b1();
    };
    and then in the .cpp file have somethign like ??
    Code:
    //blah.cpp
    
    int blah2::b1()
    {
    return 1;
    }
    
    int blah1::b1()
    {
    return 1;
    }
    is that okay ?

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    looks okay, but if b1() is going to do the same thing in blah1 as it does in blah2, then it doesn't need to be redeclared/redefined as it will be inherited and can be used as is. If you want b1() to do something different in blah1 than it did in blah2, then declare b1() virtual in blah2 and redeclare/redefine it in blah2.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM

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