Need help. Some OOP issues. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Need help. Some OOP issues.

Hi. I'm trying to get better understanding of OOP. in this code, I used classes, inheritance and classmethod to calculate area and volume of some 3D objects. Simply I think it could be way better than this. I have some questions too: 1- is it ok to manipulate cls in classmethod to reach the goal as I did in area and volume of sphere? 2- is there any way to calculate area and volume of sphere, using only 1 object (like cube1) instead of 2 (sp1 and sp2) 3- how can I recall class attributes in print, so there will be no need to type it twice? (cuboid(2,3,5)) Thanks for helping. https://code.sololearn.com/cR8DREPtf1Oq/?ref=app

10th May 2022, 5:54 AM
Ashkan Shakibi
Ashkan Shakibi - avatar
2 Answers
+ 2
1. I don't see the problem, does it work? Yeah? Then it's good. That's all. 2. Just create an object "sp" (sp = Cylinder()) and call "sp.sphere_area()" and "sp.sphere_volume()". 3. Just create a string with your content (e.g. str = f'Cuboid({cubo1.x}, {cubo1.y}, {cubo1.z})') and use it twice. You could also define the __str__ method to return the object in your string format and just do print(cube1).
10th May 2022, 6:43 AM
OrHy3
OrHy3 - avatar
0
OrHy3 thanks a lot. I managed to recall class attributes in print utilizing both methods, thanks to you. Also I corrected the code as you suggested to use only 1 object: sp. however, there's a problem using this: sphere needs 2 arguments as a class of Cylinder. Meaning that we need radius and height to calculate area and volume of sphere, which height is obviously irrelevant here. https://code.sololearn.com/caZxae8xCIQA/?ref=app
10th May 2022, 8:28 AM
Ashkan Shakibi
Ashkan Shakibi - avatar