Can we add structures under class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can we add structures under class?

4th Mar 2017, 6:34 PM
mudang
mudang - avatar
6 Answers
+ 1
what do you mean by structures? and in which language?
4th Mar 2017, 6:35 PM
Meharban Singh
Meharban Singh - avatar
+ 1
in c++
4th Mar 2017, 6:35 PM
mudang
mudang - avatar
+ 1
I mean data structure .. can we use struct inside class as we do like in nested if?
4th Mar 2017, 6:37 PM
mudang
mudang - avatar
+ 1
class Car { private: int nWheels; int curbWeight; public: int GetNWheels() { return nWheels; } struct Engine { int cylinders; int horsepower; } Engine engine; } This is perfectly valid in c++. Car myCar; cout << "This car's horsepower is " << myCar.engine.horsepower << endl; It would be a good idea to define everything within the constructor function. Is this what you were looking for? EDIT: Here is my own Playground example. https://code.sololearn.com/crRfGrL8rmRd
5th Mar 2017, 12:48 AM
Zeke Williams
Zeke Williams - avatar
+ 1
Well, what is your question then? To take your words literally, my example was a "structure under a class", so I'm not sure what you are asking.
14th Mar 2017, 5:33 PM
Zeke Williams
Zeke Williams - avatar
0
tnx but this was not my question.... although this solution was cool
5th Mar 2017, 11:17 AM
mudang
mudang - avatar