subclass object on superclass

I am wondering if this is possible.

I want to make an object of a subclass to the superclass.

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
class Superclass
{
    public:
       virtual void doSomething(int x, int y, int z);
}

void Superclass::doSomething(int x, int y,  int z)
{
    //in this method I want to make an object for subclass
    //subClass1 objSubClass1;
    //subClass2 objSubClass2;
    //I want to declare this function but it is not valid since
    //the declaration of subclass is in the lower part 
    //upcasting or downcasting?
    switch(z){
        case 1:
            objSubClass1.doSomething(int x, int y);
        case 2:
            objSubClass2.doSomething(int x, int y);
    }
}

class subClass1 : public Superclass
{
    public:
         virtual void doSomething(int x1, int x2);
}

void subClass1::doSomething(int x1, int x2)
{
    cout<<"subclass1 "<<e1<<e2;      
}

class subClass2 : public Superclass
{
    public:
       //...
}
void subClass2::doSomething(int e1, e2)
{
    cout<<"subclass2 "<<e1<<e2;
}


Thanks.
You can move the definition of Superclass::doSomething after the declaration of subClass2 and subClass1 or forward declare the two derived classes
This won't work becouse void doSomething(int x, int y, int z); and void doSomething(int x1, int x2); c++ understands as two different functions. The return type, number of arguments and their types must be the same if you wand to redefine a virtual function.

Other than that, your code should work if you change its order.
1.declare Superclass
2.declare subClass1 and subClass2
3.define all functions
Can you overload virtual functions? Must virtual functions be redefined? Do you need a pointer to a base class to perform runtime polymorphism (or a reference the object?)

Bazzy: Your solution works because the objects are declared as objects of the function? Would you be able to declare objects of subClass as members of Superclass in that way?
@hamsterman
Sorry for that. it should be void doSomething(int x1, int x2, int z1).

@Bazzy
I thought that defining a function of superclass below the declaration of the subclass is isn't working and is ugly.

By the way, Thanks Bazzy it works! and to all who replied in my post!
syneris wrote:
Your solution works because the objects are declared as objects of the function? Would you be able to declare objects of subClass as members of Superclass in that way?
My solution works because if doSomething is defined before the declarations of the two other classes, it doesn't know that they exist.
You can't have objects of derived classes on the base class:
The derived classes need a complete declaration of the class members as they will use them inside themselves,
this won't work:
1
2
3
4
5
6
class Base; // forward declaration only

class Derived: public Base // Error: Base isn't a complete type
{
    //...
};
To have a member object of a derived class, the base class should see the full declaration of the derived class,
again, this won't work:
1
2
3
4
5
6
class Derived; // forward declaration only

class Base 
{
    Derived der; // Error: Derived isn't a complete type
};
And if you think about it: if Base has an object of Derived, Derived has the members of Base, then Derived has an object of Derived which has an object of Derived ...
But you can have pointers:
1
2
3
4
5
6
class Derived; // forward declaration only

class Base
{
    Derived *d; // OK, you will construct a full object whenever you want 
};



olredixsis wrote:
I thought that defining a function of superclass below the declaration of the subclass is isn't working and is ugly.
The real thing you should have is having all the declarations of the classes in a header file and the function definitions on a source file, in this way you won't have these problems


Yeah, the objects are declared as the object of the function of the superclass

So what's wrong with this code?

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
#include <iostream>

using namespace std;

class Superclass
{
    public:
       virtual void doSomething(int x, int y, int z);
};

class SubClass1 : public Superclass
{
    public:
         virtual void doSomething(int x1, int x2, int x3);
};

void SubClass1::doSomething(int x1, int x2, int x3)
{
    cout<<"subclass1 "<<x1<<x2<<endl;      
}

class SubClass2 : public Superclass
{
    public:
       virtual void doSomething(int e1, int e2, int e3);
};
void SubClass2::doSomething(int e1, int e2, int e3)
{
    cout<<"subclass2 "<<e1<<e2<<endl;
}

void Superclass::doSomething(int x, int y, int z)
{
	SubClass1 objSubClass1;
	SubClass2 objSubClass2;
    switch(z){
        case 1:
            objSubClass1.doSomething(x, y, z);
			break;
        case 2:
            objSubClass2.doSomething(x, y, z);
			break;
    }
}

int main(){

	int xs = 1, ys = 2, zs = 1;

	Superclass sc1;
	SubClass1 sub1;
	SubClass2 sub2;

	sc1.doSomething(xs,ys,zs);
	cin.get();
	return 0;
}


The code works! but is it ugly or lacks professionalism?
Thanks. Try the code if you want.

More professional code would look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Class.h

class Superclass
{
    public:
       virtual void doSomething(int x, int y, int z);
};

class SubClass1 : public Superclass
{
    public:
         virtual void doSomething(int x1, int x2, int x3);
};

class SubClass2 : public Superclass
{
    public:
       virtual void doSomething(int e1, int e2, int e3);
};


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
// Class.cpp

#include <iostream>
#include "Class.h"

using namespace std;

void SubClass1::doSomething(int x1, int x2, int x3)
{
    cout<<"subclass1 "<<x1<<x2<<endl;      
}

void SubClass2::doSomething(int e1, int e2, int e3)
{
    cout<<"subclass2 "<<e1<<e2<<endl;
}

void Superclass::doSomething(int x, int y, int z)
{
	SubClass1 objSubClass1;
	SubClass2 objSubClass2;
    switch(z){
        case 1:
            objSubClass1.doSomething(x, y, z);
			break;
        case 2:
            objSubClass2.doSomething(x, y, z);
			break;
    }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// main.cpp

#include <iostream>
#include "Class.h"

using namespace std;

int main(){

	int xs = 1, ys = 2, zs = 1;

	Superclass sc1;
	SubClass1 sub1;
	SubClass2 sub2;

	sc1.doSomething(xs,ys,zs);
	cin.get();
	return 0;
}


That's it! Thanks Bazzy, your a big help!
Topic archived. No new replies allowed.