GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-May-2007, 02:34
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 400
Peter_APIIT is on a distinguished road
Angry

Inheritance Suspicious Problem


Hello 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 http://www.codersource.net/cpp_tutorial_inheritance.html

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:
class A
{
public:
  ~A() // non virtual
  {
  // ...
  }
};

class B: public A /* bad; A has a non virtual dtor*/
{
public:
  ~B()
  {
  // ...
  }
};

int main()
{
 A * p = new B; /*seemingly OK*/
 delete p; /*trouble, B's dtor not called*/
}
from http://www.devx.com/cplus/Article/16328/0/page/5


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 06:06. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 10-May-2007, 21:34
davis
 
Posts: n/a

Re: Inheritance Suspicious Problem


Your code is nearly complete, but try this:

CPP / C++ / C Code:
#include <iostream>

using namespace std;

class A
{
public:
    virtual ~A() { cout << "A's dtor" << endl; }
};

class B : public A
{
public:
    ~B() { cout << "B's dtor" << endl; }
};

int main()
{
    A* p_a = new B();
    delete p_a;

    return 0;
}

Now just remove the keyword virtual to see the difference.


:davis:
  #3  
Old 11-May-2007, 00:05
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 400
Peter_APIIT is on a distinguished road
Thumbs up

Re: Inheritance Suspicious Problem


Well, 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.
__________________
Linux is the best OS in the world.
  #4  
Old 11-May-2007, 02:49
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 889
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: Inheritance Suspicious Problem


Quote:
Originally Posted by Peter_APIIT
Well, 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.

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  
Old 11-May-2007, 03:06
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 400
Peter_APIIT is on a distinguished road
Thumbs up

Re: Inheritance Suspicious Problem


Probably 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:
// A header file for Employee

#ifndef _Employee_
#define _Employee_

#include<string>
using std::string;

class Human
{
protected:
	string firstname;
	string lastname;
	string address;
// The protected member can be accessed 
// from derived class member function
public:
	Human() : firstname(), lastname(), address(){}
	virtual ~Human(){}
	
	void setFirstName();
	string getFirstName();

	void setLastName();
	string getLastName();

	void setAddress();
	string getAddress();
};

class Employee : public Human
{
	int employeeID;
	float salary;

	Employee() : employeeID(0), salary(0)
	{
		setFirstName();
		setLastName();
		setAddress();
	}
	~Employee(){} 

	void setEmployeeID();
	int getEmployeeID();

	void setSalary();
	float getSalary();
};

#endif

This is the implementation file
CPP / C++ / C Code:
/* A program that record employee detail 
   and connect to SQL server to future use
*/

#include<iostream>
#include<string>

using std::cout;
using std::endl;
using std::cin;
using std::string;
using std::getline;

#include "Employee.h"

int main(int argc, char *argv[])
{
	int loop;
	Human *peter = new Employee();

	cout <<"\n\n\t\t  ";
	loop = 0;
	while(loop < 40)
	{
		cout << "-";
		loop++;
	}
	cout << "\n\n\t\t\tWelcome to Employee Detail System\n";
	cout <<"\n\t\t  ";
	loop = 0;
	while(loop < 40)
	{
		cout << "-";
		loop++;
	}
	
	peter->setEmployeeID();

	return 0;
}

void Employee::setEmployeeID()
{
	cout << "Enter Employee ID : ";
	cin >> employeeID;
};

Error Message is as below:

Quote:
Error 1 error C2248: 'Employee::Employee' : cannot access private member declared in class 'Employee' d:\c++\employee\employee\employee.cpp 19

Error 2 error C2039: 'setEmployeeID' : is not a member of 'Human' d:\c++\employee\employee\employee.cpp 37


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.
__________________
Linux is the best OS in the world.
Last edited by LuciWiz : 11-May-2007 at 04:17. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #6  
Old 11-May-2007, 04:11
davis
 
Posts: n/a

Re: Inheritance Suspicious Problem


CPP / C++ / C Code:
class Employee : public Human
{
//private: ?
	int employeeID;
	float salary;
//public: ?
	Employee() : employeeID(0), salary(0)
	{
		setFirstName();
		setLastName();
		setAddress();
	}
	~Employee(){} 

	void setEmployeeID();
	int getEmployeeID();

	void setSalary();
	float getSalary();
};

...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  
Old 11-May-2007, 06:15
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 400
Peter_APIIT is on a distinguished road

Re: Inheritance Suspicious Problem


Thanks doe
__________________
Linux is the best OS in the world.
  #8  
Old 12-May-2007, 22:08
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 400
Peter_APIIT is on a distinguished road
Thumbs up

Re: Inheritance Suspicious Problem


Hello 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:
// A header file for Employee

#ifndef _Employee_
#define _Employee_

#include<string>
using std::string;

class Human
{
protected:
	string firstname;
	string lastname;
	string address;
// The protected member can be accessed 
// from derived class member function
public:
	Human() : firstname(), lastname(), address(){}
	virtual ~Human(){}
	
	void setFirstName();
	string getFirstName();

	void setLastName();
	string getLastName();

	void setAddress();
	string getAddress();
};

class Employee : public Human
{
	int employeeID;
	float salary;
public:
	Employee() : employeeID(0), salary(0)
	{
		setFirstName();
		setLastName();
		setAddress();
	}
	~Employee(){} 

	void setEmployeeID();
	int getEmployeeID();

	void setSalary();
	float getSalary();
};

#endif


This is the implementation :
CPP / C++ / C Code:
/* A program that record employee detail 
   and connect to SQL server to future use
*/

#include<iostream>
#include<string>

using std::cout;
using std::endl;
using std::cin;
using std::string;
using std::getline;

#include "Employee.h"

int main(int argc, char *argv[])
{
	int loop;
	Human *peter = new Employee();

	cout <<"\n\n\t\t  ";
	loop = 0;
	while(loop < 40)
	{
		cout << "-";
		loop++;
	}
	cout << "\n\n\t\t\tWelcome to Employee Detail System\n";
	cout <<"\n\t\t  ";
	loop = 0;
	while(loop < 40)
	{
		cout << "-";
		loop++;
	}
	
	peter->setEmployeeID();

	return 0;
}

void Employee::setEmployeeID()
{
	cout << "Enter Employee ID : ";
	cin >> employeeID;
};

Error Message is
Quote:
Error 1 error C2039: 'setEmployeeID' : is not a member of 'Human' d:\c++\employee\employee\employee.cpp 37

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.
__________________
Linux is the best OS in the world.
Last edited by LuciWiz : 14-May-2007 at 10:41. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #9  
Old 12-May-2007, 22:09
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 400
Peter_APIIT is on a distinguished road

Re: Inheritance Suspicious Problem


I don't understand this statement.
CPP / C++ / C Code:
Human *peter = new Employee();

Thanks.
__________________
Linux is the best OS in the world.
  #10  
Old 13-May-2007, 07:29
davis
 
Posts: n/a

Re: Inheritance Suspicious Problem


Quote:
Originally Posted by Peter_APIIT
I don't understand this statement.
CPP / C++ / C Code:
Human *peter = new Employee();

Thanks.

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:
    void setFirstName();
    string getFirstName();

    void setLastName();
    string getLastName();

    void setAddress();
    string getAddress();

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:
// A header file for Employee

#ifndef _Employee_
#define _Employee_

#include <string>

class Human
{
protected:
    std::string firstname;
    std::string lastname;
    std::string address;
// The protected member can be accessed 
// from derived class member function
public:
    Human() : firstname(), lastname(), address(){}
    virtual ~Human(){}
    
    void setFirstName();
    std::string getFirstName();
    void setLastName();
    std::string getLastName();
    void setAddress();
    std::string getAddress();
};

class Employee : public Human
{
protected:
    int employeeID;
    float salary;
public:
    Employee() : Human(), employeeID(0), salary(0)
    {
	setFirstName();
	setLastName();
	setAddress();
    }
    ~Employee(){} 

    void setEmployeeID();
    int getEmployeeID();

    void setSalary();
    float getSalary();
};

#endif


Employee.cpp
CPP / C++ / C Code:
/* A program that record employee detail 
   and connect to SQL server to future use
*/

#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::cin;
using std::string;
using std::getline;

#include "Employee.h"

void print_dashed_line()
{
    int count = 40;
    do
    {
	cout << "-";
    } while( --count > 0 );
}

int main()
{
    Employee *peter = new Employee();

    cout <<"\n\n\t\t  ";

    print_dashed_line();

    cout << "\n\n\t\t\tWelcome to Employee Detail System\n";
    cout <<"\n\t\t  ";

    print_dashed_line();
    cout << endl;
    
    peter->setEmployeeID();
    delete peter;

    return 0;
}

void Employee::setEmployeeID()
{
    cout << "Enter Employee ID : ";
    cin >> employeeID;
}

void Human::setFirstName()
{
    cout << "Enter first name: ";
    cin >> firstname;
}
string Human::getFirstName()
{
    return firstname;
}
void Human::setLastName()
{
    cout << "Enter last name: ";
    cin >> lastname;
}
string Human::getLastName()
{
    return lastname;
}
void Human::setAddress()
{
    char buffer[256] = {0};
    cin.getline( buffer, 256 );
    address = buffer;
}

string Human::getAddress()
{
    return address;
}


Output:

Code:
Enter first name: Bilbo Enter last name: Baggins ---------------------------------------- Welcome to Employee Detail System ---------------------------------------- Enter Employee ID : 100


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 GIDBlogPrepping for deployment by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Graphic problem in Unreal Tournament 2004 zerox Computer Software Forum - Games 10 09-Oct-2005 12:31
Runtime Problem involving "printf" in C Program supamakia C Programming Language 2 09-Oct-2005 10:09
a significant problem after installing Xp mohammad Computer Software Forum - Windows 10 09-Aug-2005 07:03
inheritance problem ap6118 CPP / C++ Forum 8 11-Apr-2005 11:24
Inheritance problem CaptnB CPP / C++ Forum 2 27-Jan-2005 08:09

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:25.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.