problems declaring derived class objects in a base class

This assignment is not yet finished, but I can not figure out why it will not run as is. It is supposed to calculate "the money earned for 4 separate quarters, of 6 separate divisions" of a sales company using class inheritance, object composition and the "has a relationship". When I try to run this I get error messages that I don't understand. I posted them on the lines indicated by the compiler. I am using VS 2013.

javascript:tx('code')

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

static int corpTotal = 0;
static const int size = 4;
///////////////////////////////
// class Divsales //
//////////////////////////////
class DivSales
{
private:
Quarter1 Q1comp; // ERROR missing ';' before identifier 'Q1comp'
Quarter2 Q2comp; // ERROR missing ';' before identifier 'Q2comp'
Quarter3 Q3comp; // ERROR missing ';' before identifier 'Q3comp'
Quarter4 Q4comp; // ERROR missing ';' before identifier 'Q4comp'
int Array[size];
double Q0, Q1, Q2, Q3;

public:

//default constructor
DivSales(){}

DivSales(double Q0, double Q1, double Q2, double Q3):
Q1comp(Q0), Q2comp(Q1), Q3comp(Q2), Q4comp(Q3)
{

}
/*Error 9 error C2614: 'DivSales' : illegal member initialization: 'Q1comp' is not a base or member
Error 10 error C2614: 'DivSales' : illegal member initialization: 'Q2comp' is not a base or member
Error 11 error C2614: 'DivSales' : illegal member initialization: 'Q3comp' is not a base or member
Error 12 error C2614: 'DivSales' : illegal member initialization: 'Q4comp' is not a base or member c*/


};
/////////////////////////
// quarter1 //
/////////////////////////

class Quarter1 :protected DivSales
{
private:
double Q0;
public:
Quarter1(){}

Quarter1(double Q0)
{
this->Q0 = Q0;
}

};
/////////////////////////
// quarter2 //
/////////////////////////
class Quarter2 :protected DivSales
{
private:
double Q1 = 0;
public:
Quarter2(){}

Quarter2(double Q1)
{
this->Q1 = Q1;
}

};

/////////////////////////
// quarter3 //
/////////////////////////
class Quarter3:protected DivSales
{
private:
double Q2 = 0;
public:
Quarter3(){}

Quarter3(double Q2)
{
this->Q2 = Q2;
}

};

/////////////////////////
// quarter4 //
/////////////////////////
class Quarter4 :protected DivSales
{
private:
double Q3 = 0;
public:
Quarter4()
{}

Quarter4(double Q3)
{
this->Q3 = Q3;
}


};
/*************************************
* main() *
* the main takes the sales earned *
* for each quarter for each devison *
* of the company *
*************************************/
int main()
{
int Q0 = 1, Q1 = 1, Q2 = 1, Q3 = 1;
DivSales Div1(Q0, Q1, Q2, Q3);

for (int i = 0; i < 6; i++)
{


cout << "what were the sales figures for quarter 0? /n ";
cin >> Q0;
cout << "what were the sales figures for quarter 1? /n ";
cin >> Q1;
cout << "what were the sales figures for quarter 2? /n ";
cin >> Q2;
cout << "what were the sales figures for quarter 3? /n ";
cin >> Q3;
if (i == 1)
{
DivSales Div1(Q0, Q1, Q2, Q3);
}
else if (i == 2)
{
DivSales Div2(Q0, Q1, Q2, Q3);
}
else if (i == 3)
{
DivSales Div3(Q0, Q1, Q2, Q3);
}
else if (i == 4)
{
DivSales Div4(Q0, Q1, Q2, Q3);
}
else if (i == 5)
{
DivSales Div5(Q0, Q1, Q2, Q3);
}
else if (i == 6)
{
DivSales Div6(Q0, Q1, Q2, Q3);
}
}

system("pause");
return 0;
}

Last edited on
1
2
struct A { B b; };
struct B : public A {};


This is essentially the inheritance structure you have in your code and it is nonsensical.

In order for an instance of A to contain a B object, an A must know the full definition of the B type, but at the point where A needs to know this, A is still being defined. And since B inherits from A, B's definition is clearly not complete yet either.

If, somehow, you were able to get the compiler to compile this mess, construction of type B would result in a conundrum. A B object contains a B object. And the B object it contains, also contains a B object which also contains a B object which also contains a B object...

Also, there is no reason to have a type for each quarter. Have one type and four instances of the type.
Topic archived. No new replies allowed.