GIDForums  

Go Back   GIDForums > Computer Programming Forums > 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 24-Jan-2005, 12:25
CaptnB CaptnB is offline
New Member
 
Join Date: Jan 2005
Posts: 16
CaptnB is on a distinguished road

Inheritance problem


Hi, for my program, I've created a class named CMaterial which is a base for different materials. The base class only contains a number ID, a name and a type. From that class, I derived two other classes (CMAT1 and CMATS1) which both have their own member variables.

Now, what I'd like to do, is to affect a material to another class CTube. I added a member variable of type CMaterial to the class CTube thinking that I could store the material of any kind (CMAT1 or CMATS1) but it seems that it only stores the members of CMaterial and not the derived one. I read a few tutorials and every example showed me how a derived class can acces members from the base class. I need to do the inverse, can it be done??

Another problem I have (I think it's the same) is that I'd like to output CMAT1 and CMATS1 data into a text file using operator<< overload from my CMaterial class so I could output either format.

Here's the class definitions:

CPP / C++ / C Code:
// Material.h: interface for the CMaterial class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_MATERIAL_H__2319DB98_61C2_4012_B42B_DBFDE799A23C__INCLUDED_)
#define AFX_MATERIAL_H__2319DB98_61C2_4012_B42B_DBFDE799A23C__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CMaterial : public CObject  
{
public:
	// Member variables
	int m_MID;				// Material ID (Related to Nastran)
	CString m_MatName;		// User defined material name
	CString m_MatType;		// Type of material (MAT1 or MATS1)

public:
	// Default constructor / destructor
	CMaterial();
	virtual ~CMaterial();

	// Copy constructor / operator overload
	CMaterial(const CMaterial& copy);
	CMaterial operator=(CMaterial aff);
	virtual ostream& operator<<(const CMaterial& material);

	// Get / Set functions
	int GetMID();
	CString GetMatName();
	CString GetMatType();

	void SetMID(int MID);
	void SetMatName(CString MatName);
	void SetMatType(CString MatType);

	// Other functions
};

#endif // !defined(AFX_MATERIAL_H__2319DB98_61C2_4012_B42B_DBFDE799A23C__INCLUDED_)


CPP / C++ / C Code:
 
// MAT1.h: interface for the CMAT1 class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_MAT1_H__9B741231_0EEE_45AD_A580_25DDBA1EDC1F__INCLUDED_)
#define AFX_MAT1_H__9B741231_0EEE_45AD_A580_25DDBA1EDC1F__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CMAT1 : public CMaterial  
{
	// Friend functions
	friend ostream& operator<<(ostream &out, const CMAT1& mat1);

private:
	// Member variables
	double m_E;				// Young's modulus
	double m_G;				// Shear modulus
	double m_NU;			// Poisson's ratio
	double m_RHO;			// Mass density
	double m_A;				// Thermal expansion
	double m_TREF;			// Reference temperature
	double m_GE;			// Structural element damping coefficient
	double m_ST;			// Stress limit for tension
	double m_SC;			// Stress limit for compression
	double m_SS;			// Stress limit for shear
	int m_MCSID;			// Material coordinate system ID


public:
	// Default constructor / destructor
	CMAT1();
	virtual ~CMAT1();

	// Copy constructor / operator overload
	CMAT1(const CMAT1& copy);
	CMAT1 operator=(CMAT1 aff);

	// Get / Set functions
	double GetE();
	double GetG();
	double GetNU();
	double GetRHO();
	double GetA();
	double GetTREF();
	double GetGE();
	double GetST();
	double GetSC();
	double GetSS();
	int GetMCSID();

	void SetE(double E);
	void SetG(double G);
	void SetNU(double NU);
	void SetRHO(double RHO);
	void SetA(double A);
	void SetTREF(double TREF);
	void SetGE(double GE);
	void SetST(double ST);
	void SetSC(double SC);
	void SetSS(double SS);
	void SetMCSID(int MCSID);
};

#endif // !defined(AFX_MAT1_H__9B741231_0EEE_45AD_A580_25DDBA1EDC1F__INCLUDED_)


CPP / C++ / C Code:
 
// MATS1.h: interface for the CMATS1 class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_MATS1_H__AFEE434A_24ED_4B6D_B22D_B64B804384AA__INCLUDED_)
#define AFX_MATS1_H__AFEE434A_24ED_4B6D_B22D_B64B804384AA__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "mattable.h"

class CMATS1 : public CMaterial  
{
	// Friend functions
	friend ostream& operator<<(ostream &out, const CMATS1& mats1);

private:
	// Member variables
	CMatTable m_Table;		// Table entry
	CString m_TYPE;			// Type of material nonlinearity
	double m_H;				// Work hardening slope
	int m_YF;				// Yield function criterion
	int m_HR;				// Hardening rule
	double m_LIMIT1;		// Initial yield point
	double m_LIMIT2;		// Internal friction angle (in degrees)

public:
	// Default constructor / destructor
	CMATS1();
	virtual ~CMATS1();

	// Copy constructor / operator overload
	CMATS1(const CMATS1& copy);
	CMATS1 operator=(CMATS1 aff);

	// Get / Set functions
	CMatTable GetTable();
	CString GetTYPE();
	double GetH();
	int GetYF();
	int GetHR();
	double GetLIMIT1();
	double GetLIMIT2();

	void SetTable(CMatTable Table);
	void SetTYPE(CString TYPE);
	void SetH(double H);
	void SetYF(int YF);
	void SetHR(int HR);
	void SetLIMIT1(double LIMIT1);
	void SetLIMIT2(double LIMIT2);
};

#endif // !defined(AFX_MATS1_H__AFEE434A_24ED_4B6D_B22D_B64B804384AA__INCLUDED_)
  #2  
Old 24-Jan-2005, 22:22
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 570
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
I think you are a little misguided, CaptainB. If you have a base-class A, that is inherited by classes B and C, then it is natural for B and C to have access to anything in base-class A. This link exists due to the inheritance of the base-class by B and C. However, if you instantiate class A alone, it will have no access whatsoever to the functions locked inside B and C. There is no way to overcome this. Let's look at this from a genetic perspective.

My father had great metabolism, but couldn't talk his way out of a paper pag when it came to an argument. When I was born, I naturally inherited his great metabolism, but I also inherited my mom's ability to speak confidently in a confrontation. Unfortunately for my father, the parent cannot access the skills that the child is born with unless it was the parent that gave those skills. If my dad couldn't stand up in a debate when I was born, he still couldn't after I was born, even though I was able to take his great metabolism. Class inheritance works the same way.

If a base-class is inherited by a derived class, the derived class has access to any of those inherited functions, but the base-class alone cannot see any part of the class that were derived from it, nor is there a way to tell if any classes at all have been derived from it.

So, if you would like the parent to have a skill in common with the child, then the parent's got to be born with it.

// Upon looking at your code, it is my suggestion that you come up with more meaningful identifiers.
__________________
-Aaron
  #3  
Old 27-Jan-2005, 08:09
CaptnB CaptnB is offline
New Member
 
Join Date: Jan 2005
Posts: 16
CaptnB is on a distinguished road
I finally found out how to do what I wanted but I'm not sure if it's the more efficient way to do it. I've added virtual functions in my base class that, in my derived classes, return material properties. It works fine but if I have 10 different materials type with 10 variables each, I would need to add 100 virtual functions in my base class. Instead of all that, I could have created only one class with the 100 members, I would get the same result.

I'm sure there's a way to do what I want, I mean it's quite simple. A class representing a mechanical part with a member representing the part's material. Material can be of many types.

If you have any suggestions, I'm all ears...

CaptnB
 
 

Recent GIDBlogMatch IP in CIDR by gidnetwork

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
Css design problem JUNK KED Web Design Forum 4 25-Jan-2005 17:45
File Serialize's problem stanely MS Visual C++ / MFC Forum 2 08-Dec-2004 09:54
Tee chart problem Arun C++ Forum 0 01-Sep-2004 23:23
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 07:53
unwanted scrollbar problem kelly001 Web Design Forum 3 24-Oct-2003 10:44

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

All times are GMT -6. The time now is 07:01.


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