So I’m learning about inheritance in my c++ class now and trying to put all the terminology to light. I understand the difference between protected and private. But when you make a function a friend isn’t that the same thing as making it public?
The examples they are using to teach inheritance is shapes, so:
class shape{
struct circle
struct rectangle
struct line
}
Something like that, what is the difference between saying
class circle: shape
class circle: public shape
class circle: private shape (don't actually know if this one is possible)
And finally, what does the keyword virtual mean/do/used for? Thank you
First and foremost you should pick up a good C++ book and read the concepts:
The Definitive C++ book and guide list
To get you started little brief about your Questions:
Your first example is an example of Composition not Inheritance.
Inheritance is IS A relationship.
Composition is HAS A relationship.
In the example,
CircleIS A type ofShape.is same as:
For a class, Default Access specifier is
Privateby default. This is Private Inheritance. Classcircleprivately derives from classshape.Is Public Inheritance, Class
circlepublically derives from classshape.This C++-Faq should be a good read for understanding the basics:
What are access specifiers? Should I inherit with private, protected or public?
But when you make a function a friend isn’t that the same thing as making it public?
When you make a function as
friendof an class, Access specifiers no longer apply to that function. That function can access,protectedas well asprivatemembers of that class.But this is only limited to that function.what does the keyword
virtualmean/do/used for?The keyword
virtualis used to implement Dynamic/Run-Time Polymorphism.It is an broad term to be explained as such, So it is upmost important that you read and understand the concept from an book. If you still face problems in understanding anything specific about it,Come back here and feel free to ask an Specific Question here.