answersLogoWhite

0


Want this question answered?

Be notified when an answer is posted

Add your answer:

Earn +20 pts
Q: Base class and drive class definition with example in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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


Difference between base class and derived class?

The derived class derives, or inherits, from the base class. The derived class is like a "child", and the base class, the "parent". The child class has the attributes of the parent class - its variables (those defined at the class level) and methods. Changes done to the parent class (the base class) will affect the child class (the derived class).


How you can override base class member in derived classexplain with example?

To override a base class method you simply need to declare the base class method as being virtual. As well as creating a v-table, this also gives a visual hint to other developers that you expect the function to be overridden. The v-table ensures that all calls to the base class method are routed to the derived class method, thus ensuring objects behave polymorphically, according to their actual type, and not what we're actually pointing at. Consider the following example: #include <iostream> class base { public: virtual ~base(); virtual void PrintMe() const { std::cout << "I am a base class!" << std::endl; } }; class derived: public base { public: void PrintMe() const { std::cout << "I am a derived class!" << std::endl; } }; int main() { base b; derived d; base* pb = &d; b.PrintMe(); d.PrintMe(); pb->PrintMe(); return( 0 ); } Output: I am a base class! I am a derived class! I am a derived class! Note that although pb points to the base class instance of d, it still knows that it really is a derived class, as can be seen from the third line of output. Now try removing the virtual keyword from the base class method. The output will change as follows: Output: I am a base class! I am a derived class! I am a base class! Now your derived class thinks it is a base class. This is because the v-table no longer has no entry for that method, and therefore the call cannot be routed to the overridden derived class method. The base class method is called because that's what we're actually pointing at and the object no longer behaves polymorphically according to its actual type. Note also that if any method is declared virtual in a class, the class constructor must also be declared virtual. If you fail to do this, your classes will not be destroyed properly. The virtual keyword ensures that the most-derived class is always destroyed first, before working up the hierarchy of destructors to eventually destroy the least-derived class, the base class itself. Consider the following example without a virtual destructor: #include <iostream> class base { public: base(){ std::cout << "Base class created" << std::endl; } ~base(){ std::cout << "Base class destroyed" << std::endl; } }; class derived: public base { public: derived(){ std::cout << "Derived class created" << std::endl; } ~derived(){ std::cout << "Derived class destroyed" << std::endl; } }; int main() { derived* d = new derived(); base* b = d; delete( b ); return( 0 ); } Output: Base class created Derived class created Base class destroyed As you can see, the derived class was created but was not destroyed. We've created a memory leak: that memory cannot be recovered until the program ends. Now add the virtual keyword to the base class destructor: #include <iostream> class base { public: base(){ std::cout << "Base class created" << std::endl; } virtual ~base(){ std::cout << "Base class destroyed" << std::endl; } }; class derived: public base { public: derived(){ std::cout << "Derived class created" << std::endl; } ~derived(){ std::cout << "Derived class destroyed" << std::endl; } }; int main() { derived* d = new derived(); base* b = d; delete( b ); return( 0 ); } Output: Base class created Derived class created Derived class destroyed Base class destroyed Now we have the expected behaviour and have resolved the memory leak. Remember, if ANY method of a class is declared virtual, the destructor must also be declared virtual. Note that although derived classes need not use the virtual keyword in front of overrides (it is implied by the base class), there is no harm in explicitly declaring them as such, if only to give a visual hint that the methods are expected to be overridden by derivatives of the derivative (multi-level inheritance).


When a class inherits from another class what is being extended?

When a derived class inherits from a base class, the base class functionality is being extended.


What is the base class for all swing components?

The JComponent class of the javax.swing package serves as the base class.

Related questions

What is the definition of base number?

A base number is the value to the power of the exponent. For example, in 2^4, 2 is the base number and 4 is the exponent.


Why is NH3 being one example of a'proton acceptor' considered a base?

the bronstead-lowry definition of a base is a proton acceptor...


Definition of Base as math term?

The base number is the the number that is being repeatedlymultiplied in exponent problems. Example: 32 _ three is the base and two is the exponent


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


How can a base class protect derived classes so that changes to the base class will not affect them explain with example?

In order for a base class to protect derived classes, so that changes to the base class do not affect the derived classes, you must make sure that the public interface exposed by the base class does not change when the implementation of those public methods do change. You can also prevent inadvertant access to the base class attributes from the derived class, by making them private. class base { public: base(...) {...}; /* constructor */ base~(...) {...}; /* destructor */ method(...) {...}; /* other public methods */ private: ... etc. } class child : base { public: ... private: ... } So long as the calling sequence and functionality (interface) of the base class public methods do not change, the implementation of those public methods can change, and the private methods and attributes can change, without impacting any child class.


What is an inheritance.Explain different types of inheritance?

In Computer Programming, inheritance essentially refers to the re-use of code. For example, in C++, a base class will contain functions and members which can be attributed to other classed. This base class can then be inherited by another class, assuming all of the base class functions and member, but also keeping its own.


What is the definition of expanded notations?

as many times the base number is multiplied by itself for example 5*5=5squared


What is the definition of bronsted lowry base?

A simple definition is: a base accept a proton.


Difference between base class and derived class?

The derived class derives, or inherits, from the base class. The derived class is like a "child", and the base class, the "parent". The child class has the attributes of the parent class - its variables (those defined at the class level) and methods. Changes done to the parent class (the base class) will affect the child class (the derived class).


How you can override base class member in derived classexplain with example?

To override a base class method you simply need to declare the base class method as being virtual. As well as creating a v-table, this also gives a visual hint to other developers that you expect the function to be overridden. The v-table ensures that all calls to the base class method are routed to the derived class method, thus ensuring objects behave polymorphically, according to their actual type, and not what we're actually pointing at. Consider the following example: #include <iostream> class base { public: virtual ~base(); virtual void PrintMe() const { std::cout << "I am a base class!" << std::endl; } }; class derived: public base { public: void PrintMe() const { std::cout << "I am a derived class!" << std::endl; } }; int main() { base b; derived d; base* pb = &d; b.PrintMe(); d.PrintMe(); pb->PrintMe(); return( 0 ); } Output: I am a base class! I am a derived class! I am a derived class! Note that although pb points to the base class instance of d, it still knows that it really is a derived class, as can be seen from the third line of output. Now try removing the virtual keyword from the base class method. The output will change as follows: Output: I am a base class! I am a derived class! I am a base class! Now your derived class thinks it is a base class. This is because the v-table no longer has no entry for that method, and therefore the call cannot be routed to the overridden derived class method. The base class method is called because that's what we're actually pointing at and the object no longer behaves polymorphically according to its actual type. Note also that if any method is declared virtual in a class, the class constructor must also be declared virtual. If you fail to do this, your classes will not be destroyed properly. The virtual keyword ensures that the most-derived class is always destroyed first, before working up the hierarchy of destructors to eventually destroy the least-derived class, the base class itself. Consider the following example without a virtual destructor: #include <iostream> class base { public: base(){ std::cout << "Base class created" << std::endl; } ~base(){ std::cout << "Base class destroyed" << std::endl; } }; class derived: public base { public: derived(){ std::cout << "Derived class created" << std::endl; } ~derived(){ std::cout << "Derived class destroyed" << std::endl; } }; int main() { derived* d = new derived(); base* b = d; delete( b ); return( 0 ); } Output: Base class created Derived class created Base class destroyed As you can see, the derived class was created but was not destroyed. We've created a memory leak: that memory cannot be recovered until the program ends. Now add the virtual keyword to the base class destructor: #include <iostream> class base { public: base(){ std::cout << "Base class created" << std::endl; } virtual ~base(){ std::cout << "Base class destroyed" << std::endl; } }; class derived: public base { public: derived(){ std::cout << "Derived class created" << std::endl; } ~derived(){ std::cout << "Derived class destroyed" << std::endl; } }; int main() { derived* d = new derived(); base* b = d; delete( b ); return( 0 ); } Output: Base class created Derived class created Derived class destroyed Base class destroyed Now we have the expected behaviour and have resolved the memory leak. Remember, if ANY method of a class is declared virtual, the destructor must also be declared virtual. Note that although derived classes need not use the virtual keyword in front of overrides (it is implied by the base class), there is no harm in explicitly declaring them as such, if only to give a visual hint that the methods are expected to be overridden by derivatives of the derivative (multi-level inheritance).


When a class inherits from another class what is being extended?

When a derived class inherits from a base class, the base class functionality is being extended.


What is the base class for all swing components?

The JComponent class of the javax.swing package serves as the base class.