Substract | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

Substract

How a class function can access another member of a class Example: class otherthing{ }; class somthing{ Public: Int waka=1000; }; class another:public otherthing{ Public: Int chapa=100; Void patanga() { waka=waka-chapa; } }; Int main () { another a; otherthing *p1=&a; p1->patanga(); } What's the problem?

6th Nov 2019, 2:23 PM
Mohamad Soltani
Mohamad Soltani - avatar
3 Antworten
0
What do you want to do? And where have you declared a class otherthing?
6th Nov 2019, 2:27 PM
Avinesh
Avinesh - avatar
0
Substract those two
6th Nov 2019, 2:37 PM
Mohamad Soltani
Mohamad Soltani - avatar
0
#include <iostream> using namespace std; class somthing{ public: int waka=1000; }; class another:public somthing { public: int chapa=100; int patanga() { waka=waka-chapa; return waka; } }; int main() { another a; another* p1=&a; cout << p1->patanga(); return 0; } This works. You created class otherthing for no reason and you tried to inherit from it. Above all that you tried to create a pointer of that. Please try to understand everything you write. I am saying that because your variable waka was in class somthing and that is the reason you couldn't subtract things.
6th Nov 2019, 2:54 PM
Avinesh
Avinesh - avatar