Recreating a Java program and Trying to pass a derived class as the base class.


here is the code. in question
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
//  main.cpp
//  question
//
//  Created by admini on 6/9/15.
//  Copyright (c) 2015 admini. All rights reserved.
//

#include <iostream>

using namespace std;

class a {
    int a_data;

public:
    a()
    {
        a_data = 1;
    }

    void set_a_data(int input)
    {
        a_data = input;

    }
};

class b : public a {
    int b_data;

public:

    void set_b_data(int input)
    {

        b_data = input;

    }

};

class helperClass {

public:
    a* add_bClass_data_return_aClass(int bInput )
    {
        b* bInstance;
        bInstance->set_a_data(bInput);
        return bInstance;

    }

};

static helperClass *A_helperClass;


int main(int argc, const char * argv[]) {

    const a *a_aClass;

    a_aClass = A_helperClass->add_bClass_data_return_aClass(5);


    return 0;
}



please excuse the format of main. I cannot modify main for this assignment. Is it possible to do what I am thinking? OR do I need to just explicitly pass bClass as a bClass and let inheritance do its job? so to speak. Let me know if you need more info or if something does not make sense.

Last edited on
Hello,

I think helperClass was something like a nested public static ? if so, forget about that, there are dirty things in java ; loved by java devs only ; nested static is one of the thing, and has no place in c++.

I don't understand what you try to achieve ; in c++ we don't use global static ;
in c++ you can deference pointers, if you wish to, if you are using a virtual you can cast a "b" to an "a" and call set on it, you'll see what happens.
Last edited on
I am not sure. I just need to make it work. The assignment has a main class with a global static class. Now it is my job to write the implementation and NOT edit the main file.

In other words... (minus the global static issue - as that might work out to be trivial as I get further into the assignment )

Say class shape
then we have a circle class that is derived from shape
then we have a box class that is derived from shape
then we have a diamond class that is derived from shape

Now my helper class has a function that is

then method is
shape addCircle( circle and shape params )
{
// create new circle

// return circle as a shape?
// or just return a circle?
}


so that I can call the method as

Shape aShape;

aShape = helperFunction->addCircle( circle and shape params ) ;


So that is what I am trying to figure out.


Last edited on
I'm not sure what you are asking. But I noticed this.

In helperClass, on line 48, bInstance is uninitialized. To solve this you may want to do:
b* bInstance = new b;.
Remember to delete bInstance; after you have finished using it.

Topic archived. No new replies allowed.