Thread: is it possible to pass to a function a whole class?

  1. #1
    Registered User
    Join Date
    Oct 2013
    Location
    greece
    Posts
    87

    is it possible to pass to a function a whole class?

    i have a class that has N objects and i wanna pass all of them to a function that will binary search one number.. is it possible?
    for example :
    Code:
    class book{
    private:
    int num;
    };
    int main()
    {
    book *b;
    cin <<N;
    b=new book[N];
    
    }
    void binary search(//i wanna pass the whole class here with all //objects in order to binary search them);

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sure. Easiest way is to a vector or array:

    std::vector<book> v(N); // If N is only known at runtime
    binary_search(v);
    void binary_search(const std::vector<book>& input);

    std::array<book, N> v; // If N is known at compile time
    binary_search(v);
    void binary_search(const std::array<book, N>& input);

    In either case, you can use .begin() and .end() to iterate over the array/vector. .size() gives you the size.
    There is no need to use new.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2013
    Location
    greece
    Posts
    87
    so i cant find the solution yet so i will upload a part of my code if sb can help me more .... when i put vector it says that does not name a type in namespace std...
    Code:
    class book{
    private:
        string name;
        string author;
        int year;
        int price;
    public:
        //book(string,string,int,int);
        void setall(string,string,int,int);
        void display();
        int price1(){return price;}
    
    };
    void book::setall(string str1,string str2,int p1,int p2)
    {
        name=str1;
        author=str2;
        year=p1;
        price=p2;
    }
    void book::display()
    {
        cout << "name : "  << name << "   author :  " << author << "   year :  "  << year << " price: "<<price<<endl;
         }
    
    void binary_search(const std::vector<book>& S,int *,int,int);
    
    int main()
    {
    int result;
    int N;
    string str1,str2;
      int p1,p2;
      int i=0;
      book *b=new book[N];
      ifstream myfile ("book.txt");
      if (myfile.is_open())
      {
        while ( myfile.good() )
        {
        if (i==0)
            myfile >> N;
        else {
          myfile >> str1 >> str2 >> p1 >> p2;
          //cout << "str1 : "  << str1 << "   str2 :  " << str2 << "   p1 :  "  << p1 << " p2: "<<p2<<endl;
    
    
        b[i-1].setall(str1,str2,p1,p2);}
        i++;
        }
        myfile.close();
      }
      int x;
    std::vector<book> v(N);
    
    binary_search(v,&result,N, x);
    
    
    cout <<result<<endl;
    
    if (result!=0)
        cout<<"It has been found at the position : "<<result;
    else cout<<"there is no such element";
    
    system("pause");
    }
    
    void binary_search(const std::vector<book>& S,int *result,int N,int keynum){
    int mid;
    int high=N-1;
    int low=0;
    
    
    do
    {
    mid= (low + high) / 2;
    if ( keynum < S[mid].price1() )
    high = mid - 1;
    else if ( keynum > S[mid].price1())
    low = mid + 1;
    } while( keynum!=S[mid].price1() && low <= high);
    
    if( keynum == S[mid].price1() )
    {
    //cout<<"It has been found at the position : "<<mid+1;
    *result= mid+1;
    }
    else
    {
    *result=0;
    }
    system("Pause");
    
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by wesdom
    when i put vector it says that does not name a type in namespace std...
    You should #include <vector> to use std::vector.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by wesdom View Post
    when i put vector it says that does not name a type in namespace std...
    That typically means "you forgot to include the header for this type."
    What is the appropriate header, then, you ask? For that, you refer to the documentation. cppreference.com is a good place to look.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2013
    Location
    greece
    Posts
    87
    yea i saw all of them i include vector but i still cant use all of my objects like this
    Code:
    void binary_search(const std::vector<book>& S,int *result,int N,int keynum){
    int mid;
    int high=N-1;
    int low=0;
    for(int i =0; i<N; i++ )
        S[i].display();
    
    /*do
    {
    mid= (low + high) / 2;
    if ( keynum < S[mid].price1() )
    high = mid - 1;
    else if ( keynum > S[mid].price1())
    low = mid + 1;
    } while( keynum!=S[mid].price1() && low <= high);
    
    if( keynum == S[mid].price1() )
    {
    //cout<<"It has been found at the position : "<<mid+1;
    *result= mid+1;
    }
    else
    {
    *result=0;
    }
    system("Pause");*/
    
    }

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, you can. And again, you don't need to pass the size (and should not), because you can query the vector for it.
    If it doesn't work, then show us the errors and the faulting code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Oct 2013
    Location
    greece
    Posts
    87
    im working on it i will let you know soon thenx a lot

  9. #9
    Registered User
    Join Date
    Oct 2013
    Location
    greece
    Posts
    87
    when i do somthing like this in my binary search S.display();
    it says
    |error: 'const class std::vector<book>' has no member named 'display'|
    thats why i said i still cannt use all of my data because
    a) i have this function in my class
    and b) when i type S[i].display(); it prints this error:
    error: passing 'const book' as 'this' argument of 'void book::display()' discards qualifiers [-fpermissive]|
    so .... if you have any idea...

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    A member function which doesn't modify an instance of the class should be marked `const'.

    The concept is called "const correctness" which is crucial in good C++ code.

    I'd post a quick example, but you need more details than I have time. I'd suggest reading some tutorials.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  11. #11
    Registered User
    Join Date
    Oct 2013
    Location
    greece
    Posts
    87
    yea that's what i am up to wright now

  12. #12
    Registered User
    Join Date
    Oct 2013
    Location
    greece
    Posts
    87
    i saw some tutorials about const correctness and ... i realize that may i have not made clear whats my goal. i dont need to pass to a function a const object ,i ned to pass to my function a table of objects....
    in order to explain this a litle bit here is an example
    my class will look like this
    Code:
    class book{
    private:
        string name;
        string author;
        int year;
        int price;
    }
    and i want to do a binary search for N objects inside my function for the price till i fount much through different objects price
    i hope i have make a litle bit more understandable...
    i dont think i need to const my values since i want to be able to change them

  13. #13
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by phantomotap View Post
    The concept is called "const correctness" which is crucial in good C++ code.
    And C too!

    Doesn't it also allow the compiler to further optimize code? Or is that just an urban legend?

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by wesdom View Post
    i hope i have make a litle bit more understandable...
    Nope. You're not making any sense.

    i dont think i need to const my values since i want to be able to change them
    binary_search does not need to--and should not--modify its arguments. That's why we pass it by const reference. That means you need to make member functions that do not change the object's state const.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Oct 2013
    Location
    greece
    Posts
    87
    yea but even if i put const my values i still wont be able to do the search over all my objects ... it sure has to be a way to process all my class objects through my search function this is what is my final goal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pass/Save Variable contents from class to class !
    By k@$f in forum C Programming
    Replies: 4
    Last Post: 12-30-2013, 11:23 AM
  2. trying to pass a array of a class
    By morngrym in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2010, 01:43 PM
  3. Is it possible to pass a class to a function?
    By 6tr6tr in forum C++ Programming
    Replies: 7
    Last Post: 05-01-2008, 05:46 PM
  4. Replies: 3
    Last Post: 11-22-2007, 12:58 AM
  5. syntax to pass a member function pointer to another class?
    By reanimated in forum C++ Programming
    Replies: 4
    Last Post: 11-27-2003, 05:24 PM