Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   object-oriented C++ sorting program (http://www.go4expert.com/forums/object-oriented-cpp-sorting-program-t24261/)

Shayaan_Mustafa 25Dec2010 18:44

object-oriented C++ sorting program
 
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.

jimblumberg 26Dec2010 01:03

Re: object-oriented C++ sorting program
 
Quote:

#1)(in line 36) 'average2::sort(int *,int)' is not a member of 'average2'
You don't have a member function named sort() in your class definition.

Jim

Shayaan_Mustafa 26Dec2010 01:26

Re: object-oriented C++ sorting program
 
Quote:

Originally Posted by jimblumberg (Post 76447)
You don't have a member function named sort() in your class definition.

Jim

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???


jimblumberg 26Dec2010 04:51

Re: object-oriented C++ sorting program
 
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

virxen 26Dec2010 21:38

Re: object-oriented C++ sorting program
 
this should do it check the changes i made.

Code:


#include<iostream>
using namespace std;


//Calculate average and then sort the given numbers
/*********** base class *************/
class average{
    public:
        int num[100],n;  //in order to use them ,they must be defined as public!
        void avg(void);
};
/*********** member of base class ************/
void average::avg(void){
    int sum;
    int n1,limit;//n now is public property of the class so you need another name
    cout<<"Enter limit: ";//what will happen if i enter 110 for example?you should check it!
    cin>>limit;
    n=limit;//we set n to be equal with the limit
    for(n1=0;n1<limit;n1++){
        cout<<"Enter number: ";
        cin>>num[n1];getchar();
    }
    sum=0;
    for(n1=0;n1<limit;n1++)
        sum+=num[n1];
    printf("Av %d",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){//always int main!!!
    //int sum,num[100];<---why do you use it?
    s.avg();
    s1.sort();//no need for parameters since you get the values from the super class
    getchar();
}


Shayaan_Mustafa 27Dec2010 02:53

Re: object-oriented C++ sorting program
 
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
[QUOTE] s1.sort();//no need for parameters since you get the values from the super class[\QUOTE]
I receives a message "is up to date" during making, what is this message?
What should I do?
Its not OO sorry its simple C++. Therefore I use header file extension(.h).
& I really thankful to you for helping me.

Shayaan_Mustafa 27Dec2010 03:13

Re: object-oriented C++ sorting program
 
Quote:

Originally Posted by jimblumberg (Post 76452)
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

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()

virxen 27Dec2010 04:23

Re: object-oriented C++ sorting program
 
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;
}


Shayaan_Mustafa 27Dec2010 04:46

Re: object-oriented C++ sorting program
 
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.

virxen 27Dec2010 05:36

Re: object-oriented C++ sorting program
 
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.


All times are GMT +5.5. The time now is 02:45.