answersLogoWhite

0

no, Parent class can not access the members of child class ,but child class can access members of parent class

User Avatar

Wiki User

∙ 16y ago
User Avatar

What else can I help you with?

Continue Learning about Engineering

Need of friend function in c plus plus?

Friend functions are required whenever you have a function that cannot logically be declared a member of the class, but that still requires private access to the class members. In most cases, private access (and therefore protected access) to the class members would be undesirable in an external function; members are declared private or protected for a reason. However, if the class interface is such that the external function cannot fulfil its role and the interface cannot be modified because it would undermine the encapsulation, then the class may declare the function to be a friend.The function itself has no access to the implicit thispointer, thus it must accept an instance of the class by reference (where an instance is guaranteed to exist for every call) or by pointer (if an instance may or may not exist). The function can then indirectly access the private members of the class via the reference or non-NULL pointer.Friends functions are often employed whenever two (or more) classes must work closely together in such a way that an operation upon any one instance of a class must automatically invoke private or protected operations upon one or more of the others. For instance, in a parent/child structure, the parent often contains a private embedded list of its children while each child contains an embedded read-only pointer to its parent. Thus when adding a child to a parent, it would be desirable for the parent::add_child(child&) method to automatically modify the private child::m_parent member of the given child. Therefore the parent::add_child() method should be declared a friend of the child class.


What is function overriding in Java?

Method overriding is similar to method overloading, with a small difference. In overriding, a method in a parent class is overridden in the child class. The method in the child class will have the same signature as that of the parent class. Since the method in the child class has the same signature & name as the method of its parent class, it is termed as overriding. In situations where you may have to explicitly call the parent class method you can use the "super" keyword and for explicitly calling the current objects method you can use the "this" keyword.


What is the use of reusability in inheritance?

Inheritance in Java refers to the feature wherein the code/functionality of one class can be used in another class that extends this class. Example: public class Parent { ... .. } public class Child extends Parent { ... .. . } Here the class Child extends the class Parent and hence the methods of Parent are available for Child to use. This way we are re-using the code in the parent class in the child class instead of re-writing the whole thing again.


How can you access private functions of a class from the Main function in Cpp?

Any member functions and data members declared as 'private' in a class, can only be accessed directly by functions within the class.They cannot be accessed directly by derived objects, nor from anywhere outside an object of the class, such as from the Main function.To access private class members, you must rely on what are called accessor functions. Accessor functions are functions inside the class, either public or protected, which automatically have access to private members.If a function from Main, or elsewhere outside the class hierarchy, needs access, then you need to use publicaccessor functions. For derived class access, you can use protected accessor functions.


Can additional methods of base class be hidden from the derived class in object oriented software?

Yes. During inheritance only the public members of the parent class are visible to the child class. If you declare a method or a variable as private, the child class cannot access it. In other words, the methods and variables are hidden from the derived class.

Related Questions

What functions get executed when you inherit from a class with the same functions name?

In general, the child class's functions will be used in place of the parent.


Need of friend function in c plus plus?

Friend functions are required whenever you have a function that cannot logically be declared a member of the class, but that still requires private access to the class members. In most cases, private access (and therefore protected access) to the class members would be undesirable in an external function; members are declared private or protected for a reason. However, if the class interface is such that the external function cannot fulfil its role and the interface cannot be modified because it would undermine the encapsulation, then the class may declare the function to be a friend.The function itself has no access to the implicit thispointer, thus it must accept an instance of the class by reference (where an instance is guaranteed to exist for every call) or by pointer (if an instance may or may not exist). The function can then indirectly access the private members of the class via the reference or non-NULL pointer.Friends functions are often employed whenever two (or more) classes must work closely together in such a way that an operation upon any one instance of a class must automatically invoke private or protected operations upon one or more of the others. For instance, in a parent/child structure, the parent often contains a private embedded list of its children while each child contains an embedded read-only pointer to its parent. Thus when adding a child to a parent, it would be desirable for the parent::add_child(child&) method to automatically modify the private child::m_parent member of the given child. Therefore the parent::add_child() method should be declared a friend of the child class.


What is function overriding in Java?

Method overriding is similar to method overloading, with a small difference. In overriding, a method in a parent class is overridden in the child class. The method in the child class will have the same signature as that of the parent class. Since the method in the child class has the same signature & name as the method of its parent class, it is termed as overriding. In situations where you may have to explicitly call the parent class method you can use the "super" keyword and for explicitly calling the current objects method you can use the "this" keyword.


Can a base class access members of a derived class?

Base class can access members functions of derived class if they were declared as 'virtual' in base class and have same prototype. For this type of access, a pointer to base class is required.class Parent{//data members and constructor herevirtual void Func1(){cout


What are demerits of friend function?

The only demerit of a friend function is when it is used inappropriately. The first thing to remember with friends is that they are privileged -- they have unrestricted access to the private members of a class. Therefore it would be unwise to allow such access to a function that you have absolutely no control over. However, it is equally unwise to permit a function to have unrestricted access when it has no need for that access. If the public interface of a class is sufficient, then the function has no business being a friend. Many people will tell you that friends undermine the fundamental concepts of encapsulation and data-hiding. Quite frankly this is errant nonsense and shows a complete misunderstanding of what friendship means. A friend no more undermines data-hiding than does the class itself. Class member methods are highly-privileged in that they have unrestricted access to the private members of ANY instance of the class, not just the current instance. If friends are said to undermine data-hiding and encapsulation then surely the same must be said of the class, to a much higher degree. Far from undermining data-hiding and encapsulation, they actively enforce it. Unfortunately, the majority of examples that are said to back this up actually do the exact opposite. Remember, if a public interface exists and is sufficient, then use it, otherwise you really are undermining data-hiding and encapsulation. But consider this: if a function cannot be implemented within the class itself (as a member method) and the function cannot work with the current public interface of the class, then you will be faced with just two options: either you grant the function friend access, or you alter the public interface to suit the function. If you alter the public interface, not only does the function have access to that interface, but you also expose that interface to ALL external functions. That alone will do far more to undermine any concept of data-hiding and encapsulation than if you just gave the function friend access. At least then the exposure is limited only to that one friend, and not to all and sundry. So what would be a good example of a friend function? Imagine you have a parent class that can contain one or more instances of a child class and the child class contains a private member pointer to its parent. To add a child to a parent you might call a Parent::InsertData() method, which creates a new child for the given data and sets the child's parent to itself via a Child::SetParent() method. But if the parent can set a child's parent pointer, then what's to stop some errant code from altering that pointer? That alone would undermine the encapsulation we hold so dear. So the SetParent() method must be made private, which means the parent can no longer update the child's parent pointer. The only remaining solution is to declare the Parent::InsertData() method as a friend of the child class. This then allows that one function (and only that function) to alter the parent pointer of a child from outside of the child class itself. And since the parent and child classes are inextricably bound together anyway, all you've really done is extend the child's interface without exposing the interface to all and sundry. Encapsulation and data-hiding are not compromised in any way as the Parent::InsertData() method is now encapsulated as part of the child class interface; it has the same access rights as the child class itself. And if the child class can act responsibly with regards to its own instance data (as well as the data of other instances of the child class) then surely the same can be said of the Parent::InsertData() method. And even if that method passed the child to another function for further processing, that function would not have friend access to the child.


What is the use of reusability in inheritance?

Inheritance in Java refers to the feature wherein the code/functionality of one class can be used in another class that extends this class. Example: public class Parent { ... .. } public class Child extends Parent { ... .. . } Here the class Child extends the class Parent and hence the methods of Parent are available for Child to use. This way we are re-using the code in the parent class in the child class instead of re-writing the whole thing again.


How can you access private functions of a class from the Main function in Cpp?

Any member functions and data members declared as 'private' in a class, can only be accessed directly by functions within the class.They cannot be accessed directly by derived objects, nor from anywhere outside an object of the class, such as from the Main function.To access private class members, you must rely on what are called accessor functions. Accessor functions are functions inside the class, either public or protected, which automatically have access to private members.If a function from Main, or elsewhere outside the class hierarchy, needs access, then you need to use publicaccessor functions. For derived class access, you can use protected accessor functions.


Can additional methods of base class be hidden from the derived class in object oriented software?

Yes. During inheritance only the public members of the parent class are visible to the child class. If you declare a method or a variable as private, the child class cannot access it. In other words, the methods and variables are hidden from the derived class.


What are the key differences between a parent class and a grandparent class in object-oriented programming?

In object-oriented programming, a parent class is a class that is extended by another class, known as a child class. A grandparent class, on the other hand, is a class that is higher up in the inheritance hierarchy and is not directly extended by the child class. The key difference is that a parent class is directly related to the child class, while a grandparent class is further up the inheritance chain.


What will a parent of a child in your class say about you?

it depends on who the child is and if you have hurt that child teased that child and things like that


Difference between inheriting a method and overriding?

Inheriting a method means - a class is able to use a method that is declared in its parent class. Because of inheritance we need not re-declare the method in the child class again but still use it as it is. Overriding means re-declaring a method that is already available in the parent class in the child class to alter its features as per the requirement in the child class.


What is inheritance in oops?

Inheritance is a process of inheriting members and member functions of a particular class. This means one class can use the data members and functions of another similar class. This reduces efforts as similar data members or functions do not need to be declared again and again.