Classes | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Classes

I CAN’T TOGGLE OF ALL CAPS. IT IS ALSO INCREDIBLE TO ME HOW I STILL CAN’T UNDERSTAND CLASSES. I AM LOOKING FOR A GOLD-HEARTED GUY WHO CAN EXPLAIN THIS TO ME. WHY IF I CREATE AN OBJECT BY PASSING THE CONSTRUCTOR 3 VARIABLES, THEN PASS THAT SAME OBJECT TO A SECOND CLASS THIS SECOND CLASS DOESN’T KNOW ABOUT THE EXISTENCE OF THOSE 3 VARIABLE? ISN’T THIS THE WAY ‘COMPOSITION’ SHOUDL WORK? https://code.sololearn.com/cews9NCLSPjs/?ref=app

19th Dec 2019, 9:30 PM
Francesco Derme
Francesco Derme - avatar
7 Answers
+ 3
What is the foundation that supports other fundamentals of object - orientation, such as inheritance and polymorphism?
27th Apr 2021, 4:35 AM
Kuchakov Sevinchbek
Kuchakov Sevinchbek - avatar
+ 1
btw I see you CAN write without caps lock.
19th Dec 2019, 9:54 PM
HonFu
HonFu - avatar
+ 1
HonFu Yes apparently i can oldy write eithoit capslock in comments, i don’t know what kind of strange bug that is, btw thanks you for your explanatiin
20th Dec 2019, 6:13 AM
Francesco Derme
Francesco Derme - avatar
0
If you don't specify the accessibility of members, they will be private by default, which means they can only be read by other instances of that type. If you want them to be accessible for other classes, you'll have to lable them 'public'.
19th Dec 2019, 9:37 PM
HonFu
HonFu - avatar
0
HonFu So do you have an example to make me understand this?
19th Dec 2019, 9:40 PM
Francesco Derme
Francesco Derme - avatar
0
Best to make it an easy one. Every instance stores an int, and that was it. The 'box' for the int, n, is private. I've commented it out, because it's default anyway. The constructor is public. class C { //private: int n; public: C (int n) { this->n = n; } }; int main() { C c = C{5}; cout << c.n; return 0; } If you try to access n, it will fail because it's private. Write public: over that int, then suddenly it works and you can access it from outside.
19th Dec 2019, 9:54 PM
HonFu
HonFu - avatar
0
What is the foundation that supports other fundamentals of object - orientation, such as inheritance and polymorphism? abstraction
30th Mar 2022, 8:54 AM
علي أحمد سالمي