Inheritance: Calling Base Constructor

Do I define Mode() in the Statistics class and how would that look like? I don't know much about inheritance.
Last edited on
In mode.h you declare two constructors:
1
2
    Mode();
    Mode(int *,int);//Constructor inputing the initial 

But in mode.cpp I can only see a definition for the second one:
1
2
3
4
5
6
7
8
9
10
11
Mode::Mode(int *a,int s){
    //Copy the array
    this->bSize=s;
    this->b = new int[s];
        for(int i=0;i<s;++i){
            this->b[i]=a[i];
        }
    //Sort then find the mode
    this->b=this->mark_Sort(this->b,s);
    this->nMode();
}

If you declare two constructors you need to define a body for each of them.
I added the extra Mode(); to fix one of my previous errors:
In file included from main.cpp:37:0:
Mode.h:61:23: error: no matching function for call to ‘Mode::Mode()’
Mode.h:23:6: note: candidates are: Mode::Mode(int*, int)
Mode.h:10:12: note: Mode::Mode(const Mode&)
Last edited on
Why did you post another thread about this same topic...?

And it's as I said, you need to define the default constructor. If you don't know how to define a function, you should go look it up in your textbook or a tutorial. Though if your professor is giving this much code, I'd expect you to know at least the basics of C++.
So the default constructor allows me access to the Mode class? If so, then what would I need to define in mode(); to allow me access to the Mode class?
You don't need to define anything in it if you don't want to. It's simply that Statistics automatically calls the default constructor of Mode to construct the members it inherits. If you want to avoid that, you could use an intializer list.
If your compiler supports C++11 you can just instruct it to generate a default constructor for your class:
 
MyClass() = default;
What would you put in the initializer list?
I am looking at an example online and I was wondering if this is what you are talking about?
Last edited on
Well, that's exactly how you do it. Just replace Base(nValue) with the name of your base class and the required constructor arguments.

PS: Initialization lists are part of the method definition. Put the definition in Mode.cpp

PPS: You need to change the class declaration from
class Statistics : Mode
to
class Statistics : public Mode

or else you won't be able to use polymorphy.
I have no arguments to pass, so I added this
Last edited on
¿return? ¿what is the value of bSize?
I think you should get back to the basics. You shouldn't be playing around with classes if you do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int Statistics::set_Median(){ /*why does this "setter" not take any arguments, but return a value?
If you want this function to return the median, call it "getMedian" or something like that*/ 
           
       middle=bSize/2; 
    if(middle%2==0)//Calculate if size of array is even or odd
    {
     median=middle;//((b[middle]) + (b[middle-1]))/2;
    }
    else
    {
        median=middle;//b[middle];
    }  
       int set_Median(median); //this is a function declaration
       
} 
@hanst99:
If I were to guess, I would say, he is setting the Median for that object, from other given data.

Like I used a function, SetStr(); in the Class for handling Complex Numbers, which would make it in the form:
<Real_Part> + i<Imaginary_Part>

The function took no arguments, butwasn't even called directly, but included in some other class methods, like those that would set the Real r Imaginary value...
Last edited on
I inherited bSize from the Mode class which is the size of the array.
¿which array? (I guess that int *b; in Mode. posting the headers could help)
¿where do you put anything in that array? ¿where do you set the value of bSize?

hanst99 wrote:
int set_Median(median); //this is a function declaration
Nope, I made the same mistake. That is creating a variable (an integer) and set it to the value of median. It is using the same name as the function, thought (scope resolution).
So @OP, this ain't Pascal. To return a value from a function you use return value;
Last edited on
Yes, it will be interpreted by the compiler as a local variable. But it really looked to me as if the OP intended for it to be a function declaration.
Main.cpp fills an array with random numbers which then I pass to nMode() to get the array sorted out and store the sorted array to *b in the Mode class.
Last edited on
1
2
Mode *mode = new Mode(one_Dim, array_size); //sigh, one 'object'
Statistics cat; //another object 
You are setting the 'b array' of the mode object. You are not touching the 'b array' of the cat object.
I am trying inherit the sorted array b and the size of the array bSize into the Statistics class, so that I can calculate the median of the sorted array.
Last edited on
After analyzing this code I have one question, Why do you use two different variable accesses from a constructor, which isn't a copy constructor, than all other functions?

You are coping an array into a new object, not coping the object. The code would look very different and would use the "This" pointer to manage a object to object copy, which is not what you are trying to do.

Simply put the constructor should look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Mode::Mode(int *a,int s)
{
    //Copy the array
    bSize=s;
    b = new int[s];
    for(int i=0;i<s;++i)
    {
          b[i]=a[i];
    }
    //Sort then find the mode
    mark_Sort(b, s);
    nMode();
}


My opinion you are trying to access two different objects at the same time through your code. In other words the virtual table is screwed up for the objects. You would end up calling two different functions from any inheritances. I didn't understand the need for the complexity. You are not dealing with a virtual constructor or a purely virtual object.
Topic archived. No new replies allowed.