answersLogoWhite

0


Best Answer

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

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can a parent class access the functions of child class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Need of friend function in c plus plus?

Declaring a function to be a friend of a class allows the friend function to gain private (and protected) access to that class, just as it would if it were declared a static member function of the class. Most of the time you will want to use static functions rather than friend functions, but sometimes it is necessary for a member method of another class or an external function to be permitted private access. Although some will tell you this undermines the encapsulation of the class, it does not. If anything, it reinforces that encapsulation, by only allowing the specified function to gain private access. Although the friend function is not physically a member of the class, it automatically becomes tightly bound to the class. That is not necessarily a bad thing when you're only dealing with a limited number of friends, but a class that has many friends can often be an indication of poor class design. Thus it is important to limit friendship and to only use it when it is absolutely required, whenever possible. By way of an example, suppose you have designed a parent and child class. Even without friend functions, the two are implicitly bound to each other: the parent object is a container for child objects, while child objects refer to their parents. All work is delegated and coordinated between the two classes accordingly; they work together as one. Most of the work can be encapsulated without the need to explicitly bind the classes together, however child objects typically refer to their parent object (via a member pointer) and might also be permitted to change their parent, but we wouldn't want to expose that pointer outside of the class, nor would we want to expose any mutators such as child::set_parent() outside of the class. But since the two classes are imlicitly bound to each other, we can declare specific methods of the parent class to be friends of the child class, such as parent::add_child(). In this way we can be sure that whenever we add a child to a parent, the parent not only attaches the child to itself, but also detaches the child from any existing parent beforehand. Far from undermining the encapsulation, this friendship reinforces it, assuring us that both classes are tightly bound.


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.


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.


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.


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?

Declaring a function to be a friend of a class allows the friend function to gain private (and protected) access to that class, just as it would if it were declared a static member function of the class. Most of the time you will want to use static functions rather than friend functions, but sometimes it is necessary for a member method of another class or an external function to be permitted private access. Although some will tell you this undermines the encapsulation of the class, it does not. If anything, it reinforces that encapsulation, by only allowing the specified function to gain private access. Although the friend function is not physically a member of the class, it automatically becomes tightly bound to the class. That is not necessarily a bad thing when you're only dealing with a limited number of friends, but a class that has many friends can often be an indication of poor class design. Thus it is important to limit friendship and to only use it when it is absolutely required, whenever possible. By way of an example, suppose you have designed a parent and child class. Even without friend functions, the two are implicitly bound to each other: the parent object is a container for child objects, while child objects refer to their parents. All work is delegated and coordinated between the two classes accordingly; they work together as one. Most of the work can be encapsulated without the need to explicitly bind the classes together, however child objects typically refer to their parent object (via a member pointer) and might also be permitted to change their parent, but we wouldn't want to expose that pointer outside of the class, nor would we want to expose any mutators such as child::set_parent() outside of the class. But since the two classes are imlicitly bound to each other, we can declare specific methods of the parent class to be friends of the child class, such as parent::add_child(). In this way we can be sure that whenever we add a child to a parent, the parent not only attaches the child to itself, but also detaches the child from any existing parent beforehand. Far from undermining the encapsulation, this friendship reinforces it, assuring us that both classes are tightly bound.


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


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.


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.


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 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


What is mean by class access?

Class access is the ability for any given class to access the functions of another class. Private access limits access to data and code just to the class that contains the private access modifier. The so-called "default" access grants private access, as well as access to any class in the same package. Protected access grants the same as "default" access, and also allows subclasses to access the code and data. Public access allows any class in any package to access the code and data.


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.


What is mean by accessibility?

Class access is the ability for any given class to access the functions of another class. Private access limits access to data and code just to the class that contains the private access modifier. The so-called "default" access grants private access, as well as access to any class in the same package. Protected access grants the same as "default" access, and also allows subclasses to access the code and data. Public access allows any class in any package to access the code and data.