1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

object-oriented C++ sorting program

Discussion in 'C++' started by Shayaan_Mustafa, Dec 25, 2010.

  1. Shayaan_Mustafa

    Shayaan_Mustafa New Member

    Joined:
    Dec 25, 2010
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Still a student.
    Location:
    Karachi
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<iostream.h>
    //Calculate average and then sort the given numbers
    /*********** base class *************/
    class average
    {
    public:
    void avg(void);
    };
    /*********** member of base class ************/
    void average::avg(void)
    {
    clrscr();
    int num[100],sum;
    int n,limit;
    cout<<"Enter limit: ";
    cin>>limit;
    for(n=0;n<limit;n++)
    {
    cout<<"Enter number: ";
    cin>>num[n];
    }
    sum=0;
    for(n=0;n<limit;n++)
    sum+=num[n];
    printf("Av %d",sum/limit);
    }
    /*********** descendant class ************/
    class average2:public average
    {
    public:
    void sort(int,int);
    };
    /*********** member of descendant class *************/
    void average2::sort(int num[],int n)
    {
    int out,in,temp;
    for(out=0;out<n-1;out++)
    for(in=out+1;in<n;in++)
    if(num[out]>num[in])
    {
    temp=num[in];
    num[in]=num[out];
    num[out]=temp;
    }
    }
    /********** main function ***********/
    average s;
    average2 s1;
    void main(void)
    {
    int sum,num[100];
    clrscr();
    s.avg();
    s1.sort(num[],n);
    getch();
    }
    

    This is my created code which first calculate average of given numbers and then sort them in an order may be ascending or descending(don't know), I have two errors in this code. I much tried to get rid of them but I couldn't be successful. Errors are:
    #1)(in line 36) 'average2::sort(int *,int)' is not a member of 'average2'
    #2)(in line 55) expression syntax
    What is wrong with my code and why? Thanks.
     
    Last edited by a moderator: Dec 25, 2010
  2. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    You don't have a member function named sort() in your class definition.

    Jim
     
  3. Shayaan_Mustafa

    Shayaan_Mustafa New Member

    Joined:
    Dec 25, 2010
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Still a student.
    Location:
    Karachi
    But I have member function in descendant class, you take a look at here,
    Code:
    /*********** descendant class ************/
    class average2:public average
    {
    public:
    void sort(int,int);
    };
     void sort(int,int) =>Is this not a member function for which you are talking about???
     
    Last edited by a moderator: Dec 26, 2010
  4. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    The function signatures do not match. In your class definition you have two integer parameters and your function has one parameter that is an array of integers and the other is an integer. They must match.

    Code:
    class average2:public average
    {
       public:
          void sort(int,int);   // This must be the same as the function implementation.
    };
    
    void average2::sort(int num[],int n) //This must be the same as the member declaration.
    {
    
    Please use code tags.

    Jim
     
    Shayaan_Mustafa likes this.
  5. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    this should do it check the changes i made.

    Code:
    
    [COLOR=Red]#include<iostream>
    using namespace std;[/COLOR]
    
    //Calculate average and then sort the given numbers
    /*********** base class *************/
    class average{
        public:
            [COLOR=Red]int num[100],n;[/COLOR]  [COLOR=Red]//in order to use them ,they must be defined as public![/COLOR]
            void avg(void);
    };
    /*********** member of base class ************/
    void average::avg(void){
        int sum;
        int n[COLOR=Red]1[/COLOR],limit;[COLOR=Red]//n now is public property of the class so you need another name[/COLOR]
        cout<<"Enter limit: ";[COLOR=Red]//what will happen if i enter 110 for example?you should check it![/COLOR]
        cin>>limit;
        [COLOR=Red]n=limit;//we set n to be equal with the limit[/COLOR]
        for(n[COLOR=Red]1[/COLOR]=0;n[COLOR=Red]1[/COLOR]<limit;n[COLOR=Red]1[/COLOR]++){
            cout<<"Enter number: ";
            cin>>num[n[COLOR=Red]1[/COLOR]];[COLOR=Red]getchar();[/COLOR]
        }
        sum=0;
        for(n[COLOR=Red]1[/COLOR]=0;n[COLOR=Red]1[/COLOR]<limit;n[COLOR=Red]1[/COLOR]++)
            sum+=num[n[COLOR=Red]1[/COLOR]];
        printf("Av %d",sum/limit);[COLOR=Red]//average as an integer?maybe you should reconsider this!(..float maybe?)[/COLOR]
    }
    /*********** descendant class ************/
    class average2:public average{
        public:
            void sort();//you need an array here not an integer
    };
    /*********** member of descendant class *************/
    void average2[COLOR=Red]::sort()[/COLOR]{
        int out,in,temp;
        for(out=0;out<n-1;out++)
            for(in=out+1;in<n;in++)
                if(num[out]>num[in]){
                    temp=num[in];
                    num[in]=num[out];
                    num[out]=temp;
                }
            }
    /********** main function ***********/
    average s;
    average2 s1;
    [COLOR=Red]int [/COLOR]main(void){[COLOR=Red]//always int main!!![/COLOR]
        //int sum,num[100];[COLOR=Red]<---why do you use it?[/COLOR]
        s.avg();
        s1[COLOR=Red].sort();//no need for parameters since you get the values from the super class[/COLOR]
        getchar();
    }
    
    
     
  6. Shayaan_Mustafa

    Shayaan_Mustafa New Member

    Joined:
    Dec 25, 2010
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Still a student.
    Location:
    Karachi
    To virxen


    I am using Turbo C++ IDE, so when I compile your code by pressing F8 key for step by step compilation so at
     
  7. Shayaan_Mustafa

    Shayaan_Mustafa New Member

    Joined:
    Dec 25, 2010
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Still a student.
    Location:
    Karachi
    As you said I did & 1 error has been removed but in line#55 I am getting an expression syntax error. What's this? How do I remove this? Here
    /********** main function ***********/
    average s;
    average2 s1;
    void main(void)
    {
    int sum,num[100];
    clrscr();
    s.avg();
    s1.sort(num[],n);//In this line I am getting that expression syntax error
    getch()
     
  8. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    when you say turbo c++ what version do you mean?

    try this then
    Code:
    #include<iostream.h>
    
    
    //Calculate average and then sort the given numbers
    /*********** base class *************/
    class average{
        public:
            int num[100],n;  
            void avg(void);
    };
    /*********** member of base class ************/
    void average::avg(void){
        int sum;
        int n1,limit;
        cout<<"Enter limit: ";//what will happen if i enter 110 for example?you should check it!
        cin>>limit;
        n=limit;
        for(n1=0;n1<limit;n1++){
            cout<<"Enter number: ";
        cin>>num[n1];
        }
        sum=0;
        for(n1=0;n1<limit;n1++)
            sum+=num[n1];
        cout<<"Av "<<sum/limit;//average as an integer?maybe you should reconsider this!(..float maybe?)
    }
    /*********** descendant class ************/
    class average2:public average{
        public:
            void sort();//you need an array here not an integer
    };
    /*********** member of descendant class *************/
    void average2::sort(){
        int out,in,temp;
        for(out=0;out<n-1;out++)
            for(in=out+1;in<n;in++)
                if(num[out]>num[in]){
                    temp=num[in];
                    num[in]=num[out];
                    num[out]=temp;
                }
            }
    /********** main function ***********/
    average s;
    average2 s1;
    int main(void){
        s.avg();
        s1.sort();
        return 0;
    }
    
    
     
  9. Shayaan_Mustafa

    Shayaan_Mustafa New Member

    Joined:
    Dec 25, 2010
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Still a student.
    Location:
    Karachi
    To virxen.


    Yes this code is right. But still a problem, i.e. it is only calculating average but not sorting the given(or entered) numbers. Means this code is running just 1 function i.e. s.avg() but not s1.sort(). Why it is so?
    Kindly help me.
     
  10. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    Code:
    #include<iostream.h>
    
    
    //Calculate average and then sort the given numbers
    /*********** base class *************/
    class average{
        public:
        int num[100],n;
        void avg(void);
    };
    /*********** member of base class ************/
    void average::avg(void){
        int sum;
        int n1,limit;
        cout<<"Enter limit: ";//what will happen if i enter 110 for example?you should check it!
        cin>>limit;
        n=limit;
        for(n1=0;n1<limit;n1++){
        cout<<"Enter number: ";
        cin>>num[n1];
        }
        sum=0;
        for(n1=0;n1<limit;n1++)
        sum+=num[n1];
        cout<<"Av "<<sum/limit<<endl;//average as an integer?maybe you should reconsider this!(..float maybe?)
    }
    /*********** descendant class ************/
    class average2:public average{
        public:
        void sort();
    };
    /*********** member of descendant class *************/
    void average2::sort(){
        int out,in,temp;
        for(out=0;out<n-1;out++)
        for(in=out+1;in<n;in++)
            if(num[out]>num[in]){
            temp=num[in];
            num[in]=num[out];
            num[out]=temp;
            }
    for (out=0;out<n;out++)//this 2 lines print the results
        cout<<num[out]<<endl;
    }
    /********** main function ***********/
    average2 s1;
    int main(void){
        s1.avg();
        s1.sort();
        return 0;
    }
    
    
    you define 2 objects separated.(s,s1)

    It must be only one object in order to share data between methods.
     
    Shayaan_Mustafa and shabbir like this.
  11. Shayaan_Mustafa

    Shayaan_Mustafa New Member

    Joined:
    Dec 25, 2010
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Still a student.
    Location:
    Karachi
    Your code is perfect. Thank you very very much. I am great full to you for this ever. Thanks a lot.
     

Share This Page