Thursday, July 9, 2009

Can Anyone explain in detail the inheritance(classes) in C++?

Also the access modifiers(public, private, protected) ?

Can Anyone explain in detail the inheritance(classes) in C++?
When a derived class inherits from a base class, the derived class has access to method signatures, implemenations and attributes of the base class. Even if the base class does not allow access, the derived classes still contains an instance of the "private" portions of the base class in the base class instance portion of the derived class.





The public, private and protected access modifiers describe what level of access the base class provides to its parts to others. When an attribute or method is public, then all classes can access it. When it is private, then no other class can access it. When protected, then derived classes may access those methods and attributes, but no others.





A derived class may also change the implementation of the base class method. In some cases, the base class doesn't even provide an implemenation for a method; it only provides the signature. The derived class must provide the implemenation. These base classes are abstract, and they cannot be instantiated.





There is one way for an outside class to gain access to private attributes. If a class declares another class as a friend, then it allows that other class to access its private methods and attributes.
Reply:public classes can b accessed by any classes whereas private n protected cant b accessed outside te main class.however a public class shud be prefixed by PUBLIC.


inheriteance in C++ are of various types like derived,inherited etc..
Reply:Inheritance is the same in any object oriented language (ok, so almost).





Basically every class IS an object. Say you have a class called circle. A circle IS an object. In fact it inherits from an object (the compiler writes this for you, however). Now shape has a method called Area(). Area returns a double with the area of the shape. Now you have a class called Circle, and you make it inherit from shape. Circle now has a method called Area. This becomes useful if you override the method, and return a value (say pi*r^2) that represents this. Now any object which is a shape can get an area.





C++ allows something called multiple inheritance, which is generally considered to be ugly and rather shaky. It does what it's name implies, but there are usually better, more elegant solutions there. I'm not a c++ programmer (I write C# mostly), so I can't go into details about what they are...
Reply:yes inheritence is to use the properties of one parent class in any other child class, public is wht we can use in all other classes, private which can be used in only the same class, and protected is which an use asprivate, but only can use as public in next class, ok


Regards,


Vikas Kumar,


hi_lovegirls@yahoo.com
Reply:I used to be able to. I think inheritance has to do with...I forget. This is why I switched from CS to philosophy.

online survey

No comments:

Post a Comment