Object properties and Methods | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 13

Object properties and Methods

So if i have an object,is there anyway of getting/outputting all its properties and methods? This is very easy in js,but the solution seems unclear in other languages. Ill appreciate it if solution could be orovided in: java c++ Thanks

27th May 2018, 6:50 PM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
15 Answers
+ 5
Brains in c++ you can make this bcoz you just required class name in that and the object name which you want to prefer....
27th May 2018, 7:08 PM
Yesh Jadav
Yesh Jadav - avatar
+ 5
Manual you are right
27th May 2018, 7:08 PM
Yesh Jadav
Yesh Jadav - avatar
+ 5
Brains In java, you can use java.lang.reflect class to access the methods called: getDeclaredFields() -> to access all public properties. getDeclaredMethods() -> to access all declared methods. Here is an example: https://code.sololearn.com/c1ZsTEPMhher/?ref=app
27th May 2018, 7:29 PM
777
777 - avatar
+ 4
basically in c++ objects properties are held by using methods so..methods(class) is used to express the object within it .
27th May 2018, 6:55 PM
Yesh Jadav
Yesh Jadav - avatar
+ 4
in c++ you can‘t if you don‘t already know the inner workings(aka the declaration) of you class. you can get all the data in the class because its just saved from &my_object to &my_object + sizeof(my_object)(not quite sure if sizeof works at runtime). i dont see an obvious way to also get the type information or differentiate between different members. maybe you could do something like this using compiler specific implementations of rtti, but thats really complex and it doesn‘t save information about the members as far as i know bit here is an article about rtti: http://www.openrce.org/articles/full_view/23
27th May 2018, 7:02 PM
Max
Max - avatar
+ 4
So provided i have no idea which class an object came from,and i need to extract all its properties,its impossible in this languages?wow
27th May 2018, 7:05 PM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
+ 4
Brains I believe that is right, in c++ you need to know exactly where it is came from. If private you need a method to get past encapsulation. java and c# share that feature.
27th May 2018, 7:08 PM
Manual
Manual - avatar
+ 4
Brains you might be able to find out which class it is from using typeid(). but c++ and other compiled languages normaly dont support this stuff since it costs alot of performance. also check out this article http://jackieokay.com/2017/04/13/reflection1.html
27th May 2018, 7:09 PM
Max
Max - avatar
+ 4
The thing is im creating random objects from random classes. if class A had a property called "big" i could test if that object had a big property to find out whether its from class A.good idea,not when you have several classes that share similar properties,thats why im desperate for this
27th May 2018, 7:10 PM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
+ 4
edit: mentioned by Max typeid example example from a website - not sure where https://code.sololearn.com/cGdQSt426qOe/?ref=app
27th May 2018, 7:11 PM
Manual
Manual - avatar
+ 4
Thanks Manual this is way easier
27th May 2018, 7:16 PM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
+ 3
Brains C++ if public you can just call them. cout << obj.var; if private you need to call or return from a method, definded within the class. dataType getter(){ return var; }
27th May 2018, 7:04 PM
Manual
Manual - avatar
+ 3
Manual but that doesn‘t automaticaly iterate through all members like he asked for.
27th May 2018, 7:06 PM
Max
Max - avatar
+ 3
@Brains in that case use typeid or something that description makes your problem simpler than i thought, i thought you didnt even have the declarations if the classes
27th May 2018, 7:14 PM
Max
Max - avatar
+ 1
In python we have read only attribute __dict__ . which contains all details like class name ,variable of object,methods of objects. Any thing else in python which gives object properties?
1st Sep 2018, 10:29 PM
Kalyan
Kalyan - avatar