![]() |
|
#1
|
|||
|
|||
Inheritance Suspicious ProblemHello all expert programmer, i have read some of the articles from the internet about C++ inheritance.
Below is my problem: 1. Some of the exceptions to be noted in C++ inheritance are as follows. * The constructor and destructor of a base class are not inherited * the assignment operator is not inherited * the friend functions and friend classes of the base class are also not inherited. from www.codersource.net and 2. Why Inheriting from a Class That Has No Virtual Destructor is Dangerous Classes with a non-virtual destructor aren't meant to serve as base classes (such classes are usually known as "concrete classes"). std::string, std::complex, and std::vector are concrete classes. Why is inheriting from such classes not recommended? When you use public inheritance, you create an is-a relationship between the base class and its derived classes. Consequently, pointers and references to base can actually point to a derived object. Because the destructor isn't virtual, C++ will not call the entire destructor chain when you delete such an object. For example: CPP / C++ / C Code:
I don't know which one is true. The first statement saying the derived class is not inherit the constructors and destructor. The second statement stated that derived class is inherit from base class but you need to define the base constructor as virtual and called it through pointer. I feel confusing now. Thanks for your explanation. Your help is greatly appreciated by me and others. Last edited by LuciWiz : 10-May-2007 at 07:06.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|||
|
#2
|
|||
|
|||
Re: Inheritance Suspicious ProblemYour code is nearly complete, but try this:
CPP / C++ / C Code:
Now just remove the keyword virtual to see the difference. :davis: |
|
#3
|
|||
|
|||
Re: Inheritance Suspicious ProblemWell, i think you have misunderstand my question.
I know when we declare virtual in base class, we can override a function in derived class but two statement from my post 1 stated is totally different. Website 1 stated that constructor and destructor will not inherited by derived class but website 2 mentioned some opposite opinion. You can check my post 1. Thanks for your explanation. I feel glad you are willing to reply. A billion thanks to you and your families. |
|
#4
|
||||
|
||||
Re: Inheritance Suspicious ProblemQuote:
The 2 statements on the website don't contradict each other. The code davis posted demonstrates the use of polymorphism; it doesn't demonstrate "overriding" of virtual functions... If you had compiled the code and followed his directions I think you would have understood the difference between having and not having the base destructor "virtual" when using polymorphism. Best regards, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#5
|
|||
|
|||
Re: Inheritance Suspicious ProblemProbably i understand what u told me. Thanks you. By the way, i have another question which is as below:
Well, my program is not necessary to use inheritance but i would like to practice enough. This is the header file CPP / C++ / C Code:
This is the implementation file CPP / C++ / C Code:
Error Message is as below: Quote:
I using Microsoft VS 2005 professional edition. Thanks for your help. Your help is greatly appreciated by me and others. Hope GOD will blessed you. A billion thanks for any advice. Last edited by LuciWiz : 11-May-2007 at 05:17.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|
#6
|
|||
|
|||
Re: Inheritance Suspicious ProblemCPP / C++ / C Code:
...member access in classes are private by default. You will want to decide which of these need to define the public interface to your class. Also, if you use [c++][/c++] tags, your code will be color coded, which is one of the better features about this forum. :davis: |
|
#7
|
|||
|
|||
Re: Inheritance Suspicious ProblemThanks doe
|
|
#8
|
|||
|
|||
Re: Inheritance Suspicious ProblemHello expert programmer, i feel shameful because always post the stupid question in this forum.
I bag your pardon. My problem is i have followed the instructions given by you all but i still can solved the problem. is there anything wrong or i very stupid ? Below is my program : This is the interface: CPP / C++ / C Code:
This is the implementation : CPP / C++ / C Code:
Error Message is Quote:
My problem is i would to instantiate a object employee and this object has properties of Human. Thanks for your help. Your help is greatly appreciated by me an others. Last edited by LuciWiz : 14-May-2007 at 11:41.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|
#9
|
|||
|
|||
Re: Inheritance Suspicious ProblemI don't understand this statement.
CPP / C++ / C Code:
Thanks. |
|
#10
|
|||
|
|||
Re: Inheritance Suspicious ProblemQuote:
What is not to understand? However, that is the root of your problem, too. You are declaring a pointer to a Human type. Later, when you try to access the member Employee::setEmployeeID() you get an error saying that Human has no such member, which is true. A pointer of type Human doesn't have a setEmployeeID member. You will want to change your above code so that you declare a pointer of type Employee. Also, since you are using "new" you will want a corresponding "delete." You will also want to implement the member operations that you declared in Human, such as: CPP / C++ / C Code:
These will result in an error when you go to link the application, since the Employee constructor uses them. Perhaps this will help you a bit: CPP / C++ / C Code:
Employee.cpp CPP / C++ / C Code:
Output: Code:
Note that we usually use a separate header file for each class definition, therefore, we'd expect to see Human.h and Employee.h as separate files. Also, this implementation is BAD. It isn't too bad for a beginner trying to understand how C++ works, but we usually want to separate the I/O method from the class interface implementation. That is, instead of defining setFirstname as taking no argument, we would expect it to take a const reference to a std::string. That would remove the "cin" and "cout" from the implementation, which strongly couples the class design to the console. If the operation takes a string, the string can come from any source and not just the console as it is now written. Also, we would expect that Human would have an Address class, which is more complex than a single string, particularly if we were going to do something like mailing the employee's tax-related forms to them. However, not all Humans have an "Address." What address is it if you're talking to an indigenous native member of the Kayan tribe of Borneo? In other words, we might expect to have a further specialization of Human whereby we somehow categorize the specialization to have some distinction of "civilization" that includes having an "address." :davis: |
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Graphic problem in Unreal Tournament 2004 | zerox | Computer Software Forum - Games | 10 | 09-Oct-2005 13:31 |
| Runtime Problem involving "printf" in C Program | supamakia | C Programming Language | 2 | 09-Oct-2005 11:09 |
| a significant problem after installing Xp | mohammad | Computer Software Forum - Windows | 10 | 09-Aug-2005 08:03 |
| inheritance problem | ap6118 | C++ Forum | 8 | 11-Apr-2005 12:24 |
| Inheritance problem | CaptnB | C++ Forum | 2 | 27-Jan-2005 09:09 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The