answersLogoWhite

0


Best Answer

The best design strategy would be to provide a protected accessor (getter) that returns the variable either by value or by constant reference. If the derived class needs to mutate the variable, then provide a protected mutator (setter). Only derived classes have protected access to their base classes. However, derivatives can also lower that access to private with respect to their own derivatives, if desired.

Even if the variable does not represent a class invariant, it's still best to provide an interface rather than expose an implementation detail outside of the class, whether that exposure is public or protected. In this way the implementation detail may be changed at a future time without affecting any of the consumers of your class (including derivatives), since they will all be using the public or protected interface. So long as that interface remains unchanged, the scope of your internal changes is limited to the class itself, thus you won't break any code that uses your class.

If your class already provides a public interface to the variable, then your derived class can obviously make use of that as well. You only need a protected interface when no public interface is provided.

User Avatar

Wiki User

โˆ™ 8y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

โˆ™ 8y ago

Derived classes can access member variables of their base classes by declaring those members protected or public.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How Can a derived class access an integer variable of base class in qt?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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.


Is an abstract class cannot have any member variable in C plus plus?

An abstract base class may have member variables. Whether or not it actually needs member variables depends on the nature of the base class itself. If the variable is common to all derived classes, then it makes sense to place the variable in the base class. If some derived classes have no need of the variable, then it is better to derive an intermediate class with the variable, and to derive those classes that require that variable from the intermediate class, rather than directly from the abstract class.


Can you access in drive class from base class private data?

Base class should no knowledge about derived classes. The "private" modifier on a data member means private to the class which defined it. Base class cannot directly reference/access the private data member of the derived class, and the derived classes cannot access the private data member defined in the base class. Either way the accessing the private data member should be done via properties or getters


Which keyword is used to declare a class?

There is no keyword for it. You can use a variable of 1 class in another only id the other class is derived from the 1st class.Although you can use the variable of an object of a class in another class using object.variable


Java Which access modifier is preferred for instance variables?

Usually Private is the preferred access modifier for instance variables. Benefits: 1. No other class can access this variable directly. They can do only through the getter/setter methods 2. Only the methods in that particular class can use this variable

Related questions

What is difference between struct variable and class object?

structure variable can access the variable but class object can access the function also


What is private in vbnet?

The 'private' keyword in vb.net is commonly used in variable declaration or sometimes in creating functions. This means that when a variable or function is created privately (eg private variable1 as integer), the class that contains that variable/function will be the only class that will have access to it. Here's an example: class form1 Private variable1 As Integer=5 end class _______________________ class form2 end class form2 will not be able to access form1's variable1 because it is declared as Private. To make form1's variable1 usable to form2 it should be declared as Public variable1 As Integer=5. I hope i've given you a clear explanation. Good luck on your programming mate!


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.


Which is public to all access class or struct or function or variable?

The default access specifier for a class is private. The default access specifier for a struct is public. It does not matter if it is a function or a variable.


Is an abstract class cannot have any member variable in C plus plus?

An abstract base class may have member variables. Whether or not it actually needs member variables depends on the nature of the base class itself. If the variable is common to all derived classes, then it makes sense to place the variable in the base class. If some derived classes have no need of the variable, then it is better to derive an intermediate class with the variable, and to derive those classes that require that variable from the intermediate class, rather than directly from the abstract class.


Can you access in drive class from base class private data?

Base class should no knowledge about derived classes. The "private" modifier on a data member means private to the class which defined it. Base class cannot directly reference/access the private data member of the derived class, and the derived classes cannot access the private data member defined in the base class. Either way the accessing the private data member should be done via properties or getters


Different access specifies in C plus plus?

Public members and methods of a class are universally accessible. Protected members and methods of a class are accessible to methods of instances of that class and its derived classes. Private members and methods of a class are accessible only to methods of instances of that class.Class A has three members: public_member, protected_member, and private_member, which have access corresponding to their names. Class A has access to all three. Class B, derived from class A, has access to public_member and protected_member, but not private_member. Unrelated class C has access only to public_member.


Which keyword is used to declare a class?

There is no keyword for it. You can use a variable of 1 class in another only id the other class is derived from the 1st class.Although you can use the variable of an object of a class in another class using object.variable


Java Which access modifier is preferred for instance variables?

Usually Private is the preferred access modifier for instance variables. Benefits: 1. No other class can access this variable directly. They can do only through the getter/setter methods 2. Only the methods in that particular class can use this variable


How do you access private variable in flex?

In Flex, you access a private variable by calling it from within that class. Remember, the "private" modifier means it cannot be directly accessed outside of the class you declare it in (note I say "directly accessed" you can indirectly access it via public functions which I will show below). Example: declare your variable at the top of your class like this: private var myVariable:String; Then, inside one of your functions of that class, you can access the variable and/or assign it this way: public function changeMyVariable(value:String):void { // sets the private variable to a custom string myVariable="test String"; trace("myVariable is set to "+myVariable); // sets the private variable to the argument passed in myVariable = value; trace("myVariable is now set to" +myVariable); }


How can the base class data be accessed from database directly using derived class?

In order to access a base class member the base class member must be declared protected or public. Private data is only accessible to members of the same class and to friends of the class. I'm not entirely sure what you mean by accessing from a database. A derived class does not require a database in order to access its base class members, it only requires protected access at the very least. Note that although you can declare derived classes to be friends of their base classes and thus allow private access, it would be an unusual design since base classes should never know anything about any of their derivatives.


What is refrence variable?

Reference variables in java is used to refer to an object. Its a way to access another variable or memory address with a variable and change the data inside the memory address. It gives direct access to the memory access. example:- Box b=new Box(); b is the reference variable of type Box. It can hold reference to any instance of class Box. new Box() creates an instance of class Box. So b is now pointing to an object of class Box. shreya..