프로그램의 목적은 라우드 스피커 인클로저의 시뮬레이션 스크립트를 생성하는 것이 었습니다.상위 데이터 멤버 값을 하위 클래스로 전달
따라서 나는 스피커를 정의하는 클래스 스피커를 가지고 있습니다.
모든 인클로저의 공통 매개 변수를 포함하는 상위 클래스 인클로저.
자신의 스페셜 속성이있는 하위 클래스입니다. 다형성과 상속을 사용
나는 완벽하게 잘 작동 첫 번째 프로그램을 얻을하지만 난 인클로저의 기본 속성마다 다시 계산해야합니다 내 프로그램의 경우
class speaker
{
}
class Enclosure
{
int m_boxHeight;
speaker * m_speakerBass;//...
public://some functions
}
class closedBox : public Enclosure
{
public:
closedbox(speaker &speakerbass):Enclosure(speakerbass)// some functions
protected:
int paramclosed1;//...
}
int main()
{
speaker speakerbass;
cout <<endl<< "Please choose in the available enclosure proposals" << endl;
cout << "1 Closed box enclosure" << endl;
// cout << "2 Bass reflex enclosure" << endl;
// cout << "3 and so on..." << endl;
int choice;
cin>>choice;
switch (choice)
{
case 1:
closedbox * ClBox2Simu;
ClBox2Simu= new closedbox(speakerbass);
delete ClBox2Simu;
break;
case 2:
//... same but with bassreflex class child
break;
default:
cout<<"Then good bye";
}
return 0;
}
를 부모 클래스의 멤버 데이터 수 아이에게 줄 수 있습니다. 즉, Enclosure
의 상자 크기는 각 하위 클래스 bassreflex
또는 closedbox
에서 동일합니다.
이
을 상위 클래스를 만들
부모 매개 변수를 사용하여 아이를 작성 최초의 초기 일반적인 계산 (문제를) 수행
따라서 나는 방법에 대한이 지금 할 경우
기본적으로 child = 금지 된 parent. 이 코드의 아이디어에서 :
class speaker
{
public: // some functions
protected:
int fs;
int paramSpeaker2; //...
}
class Enclosure
{
public: //some common function
protected:
int m_boxHeight;
speaker *m_speakerBass //...
}
class closedBox : public Enclosure
{
public:
closedbox(); // some functions
protected:
int paramclosed1; //...
}
class bassreflex : public Enclosure
{
public:
bassreflex(); // some functions
protected:
int paramclosed1; //...
}
int main()
{
Enclosure initialBox;// calculate dimension,choose speaker...
closedbox * ClBox2Simu;
ClBox2Simu= initialBox;// child= parent which is forbidden
//do stuff concerning closedbox
delete ClBox2Simu;
bassreflex * BassReflex2Simu;
BassReflex2Simu= initialBox; //forbidden
//do stuff concerning bassreflex
delete BassReflex2Simu;
//... and so on for other child class using enclosure
delete initialBox
return 0;
}
는 분명 희망! 만약 설명
왜 직접 하위 클래스를 초기화하지 않습니까? 읽는 시간 [생성자 및 멤버 초기화 목록] (http://en.cppreference.com/w/cpp/language/initializer_list) – Danh
"기본 속성 재 계산"의 위치 (또는 이유)를 이해하지 못합니까? – UnholySheep
첫 번째 예제의 동적 할당은 완전히 필요하지 않습니다. – molbdnilo