+ 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
7 Respuestas
+ 3
What is the foundation that supports other fundamentals of object - orientation, such as inheritance and polymorphism?
+ 1
btw I see you CAN write without caps lock.
+ 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
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'.
0
HonFu So do you have an example to make me understand this?
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.
0
What is the foundation that supports other fundamentals of object - orientation, such as inheritance and polymorphism?
abstraction