Hello.

In general, there are 3 classes, one is abstract, the others are derivatives. In the abstract, a virtual function is described without implementation, and in the other two it is implemented. How to call the implemented function from each derived class in main ()?

Here is the code:

class components { public: virtual int solve(void) = 0; }; class componentsOne : public components { public: int result, x, y; componentsOne(int x, int y) { printf("Create object First"); printf("%d",solve(x, y)); } int solve(int x, int y) { return result = x+y; } }; class componentsTwo : public components { public: int result, x, y; componentsTwo(int x, int y) { printf("Create object First"); printf("%d",solve(x, y)); } int solve(int x, int y) { return result = x*x+y; } }; 

Thank you very much in advance!

    3 answers 3

     #include <iostream> class components { public: virtual int solve() = 0; }; class componentsOne : public components { private: int m_x, m_y; public: componentsOne(int x, int y) : m_x(x), m_y(y) { std::cout << "Create object One\n"; } int solve() { return (m_x+m_y); } }; class componentsTwo : public components { private: int m_x, m_y; public: componentsTwo(int x, int y) : m_x(x), m_y(y) { std::cout << "Create object Two\n"; } int solve() { return (m_x*m_x+m_y); } }; int main() { components *object; object = new componentsOne(5, 2); std::cout << "ComponentOne->solve() === " << object->solve() << "\n"; delete object; object = new componentsTwo(3, 2); std::cout << "ComponentTwo->solve() === " << object->solve() << "\n"; delete object; return 0; } 

    With 1 pointer to the base class, you can work with all the heirs.

    • c: \ projects \ 1l \ 15 \ 15.cpp (57): error C2259: componentsOne: it is not possible to create an instance of the abstract class 1> because of the following members: 1> int components :: solve (void): abstract 1> c: \ projects \ 1l \ 15 \ 15.cpp (20): see the announcement "components :: solve" 1> c: \ projects \ 1l \ 15 \ 15.cpp (61): error C2259: componentsTwo: it is not possible to create an abstract instance class 1> because of the following members: 1> int components :: solve (void): abstract 1> c: \ projects \ 1l \ 15 \ 15.cpp (20): see the announcement "components :: solve" - andrewshka
    • And so does not want, or I have something in the code is not so ... Or something is wrong with the compiler. - andrewshka
    • Well, I didn't rewrite your code much. So try it. And it is better to open some book on C ++. - Roman Goriachevskiy
    • I have all the rules compiled and run. - Roman Goriachevskiy
    • Thank you so much! - andrewshka
     int main() { componentsOne c1(2,4); int x1 = c1.solve(); componentsTwo c2(2,4); int x2 = c2.solve(); return 0; } 

    somewhere like that.

    upd

    And class definitions need to be fixed like this: class components {public: virtual int solve () = 0; };

     class componentsOne : public components { public: int result, x, y; componentsOne(int x, int y) { printf("Create object First"); printf("%d",solve()); } int solve() { return x+y; } }; class componentsTwo : public components { public: int result, x, y; componentsTwo(int x, int y) { printf("Create object Two"); printf("%d",solve()); // правда зачем оно здесь, да ещё в С стиле } int solve() { return x*x+y; } }; 
    • So not interesting. Interesting - with pointers to the base and derived classes, in different combinations - gecube
    • It's obvious, I would not ask. This is a standard object creation. Immediately, something like shaman pointer is needed :) In general, here are the errors after your example: error C2259: componentsOne: it is impossible to create an instance of an abstract class due to the following members: int components :: solve (void): abstract c: \ projects \ 1l \ 15 \ 15.cpp (20): see the announcement "components :: solve" - andrewshka
    • Visual Studio 2010 - andrewshka
    • @gecube, it's not that not interesting, I would have gotten away. But it is not compiled) - andrewshka
    • Well, the abstract method is crooked - the signatures are different. Therefore, either in the base write `virtual int solve (int x, int y) = 0;` or write correctly in the heirs. I choose the second option. - KoVadim

    In both answers, the main ones are completely uninteresting and, most importantly, not illustrative.

     int main() { components *Comp[2]; Comp[0] = new componentsOne(3,5); Comp[1] = new componentsTwo(2,4); for(int i = 0; i < 2; i++) Comp[i]->solve();//Вот, где самая сладость-то ................. delete [] Comp; return 0; } 
    • and what's the catch in your code? You use 2 pointers for two objects, and I, after 1 pointer, work with two objects. then do such an example. funct (components * comp) {comp-> solve (); } int main () {componentsOne compOne (3, 5); componentsTwo compTwo (2, 4); funct (& compOne); funct (& compTwo); return 0; } - Roman Goriachevskiy
    • and we are already on "you"? I see that you did not understand, and I do not find anything surprising in this. - BuilderC
    • @BuilderC, to be honest, I also do not see anything fundamentally different from the version of the vehicle. - skegg
    • In addition, @BuilderC, you have an error. See, you create a Comp array on the stack, and then you delete[] to it. Probably, you want to free the objects in the heap pointed to by the members of this array? So do it wrong. The result of this code will be deplorable. It is necessary for each member to personally apply delete Delete Comp [0]; delete Comp [1]; - skegg
    • @mikillskegg, you are absolutely right in both of your comments. 1) polymorphism is shown more prominently and 2) that’s my hand did not raise this delete to write. - BuilderC