1

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

Richard
  • 5,840
  • 36
  • 123
  • 208

3 Answers3

7

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, Circle IS A type of Shape.


class circle: shape 

is same as:

class circle: private shape 

For a class, Default Access specifier is Private by default. This is Private Inheritance. Class circle privately derives from class shape.

In Private Inheritance all the public and protected members of the Base class become Private members of the Derived class.

class circle: public shape 

Is Public Inheritance, Class circle publically derives from class shape.

In Public Inheritance the public members of Base class become Public members of Derived class and protected members of the Base class become Protected members of the Derived class.

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 friend of an class, Access specifiers no longer apply to that function. That function can access, protected as well as private members of that class.But this is only limited to that function.


what does the keyword virtual mean/do/used for?
The keyword virtual is 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.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Now explain squares and rectangles! Both are shapes. – Ed Heal Nov 01 '11 at 03:57
  • 2
    @EdHeal: Let us not pretend,We were never confused when we learnt this first time.I agree the question is broad but definitely not unanswerable.The OP needs to be pointed to the right direction.SO is not only for expert programmers. – Alok Save Nov 01 '11 at 04:02
0

Is the process that allows programmers to create a new class that is a specialization of the existing class, and then, the new class is called the Derived class of the existing class and the existing class is called the base of the new created class.

0

Inheritance is when a new class take over the old existing class and Polymorphism is when you refer a certain class in program as a base of the new introduced class.