Class Multiple Inheritance

So I have gotten a lot of help from this forum. I want to thank you ahead of time. I am in the last parts of my C++ class and we are into Multiple Inheritance of classes and his explanation is less than satisfactory. Here is what he gave us.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  #include  <iostream>
#include  <cstring>
#include  <cstdlib>
using namespace std;

class Storage
{
private:
    int store;
public:
    Storage()
    {
        store = 0;
        cout << endl << "1. Default constructor for Storage called..." << endl;
    }
    Storage(int passIn)
    {
        store = passIn;
        cout << endl << "2. constructor for Storage with one arg called..." << endl;
    }
    int  getSt()
    {
        return store;
    }
    void setSt(int st)
    {
        store = st;
    }
    ~Storage()
    {
        cout << endl << "3. Default destructor  for Storage called..." << endl;
    }
};

class MFR
{
private:
    char mf1[20];
public:
    MFR()
    {
        strcpy_s(mf1, "\0");
        cout << endl << "1. Default constructor for MFR  called..." << endl;
    }
    MFR(char* mfp)
    {
        strcpy_s(mf1, mfp);
        cout << endl << "2. constructor for MFR with one arg called..." << endl;
    }
    char*  getMF()
    {
        return mf1;
    }
    void setMF(char* man)
    {
        strcpy_s( mf1, man);
    }
    ~MFR()
    {
        cout << endl << "3. Default destructor  for MFR called..." << endl;
    }
};

class CPU : public Storage, public MFR
{
private:
    int speed;
public:
    CPU()			//default constructor
    {
        cout << endl << "4. Default constructor for CPU called..." << endl;
    };
    CPU(int sp, int st, char * mf2) : Storage (st), MFR(mf2)  //creating a reference to
    {                                       //storage class, allow initialization
        speed = sp;
        cout << endl << "5. constructor for CPU with one arg called..." << endl;
        cout << endl << "6. constructor for Storage with one arg called as well..." << endl;
        cout << endl << "7. constructor for MFR with one arg called as well..." << endl;
    }
    int  getCPU()
    {
        return speed;
    }
    void setCPU(int sp)
    {
        speed = sp;
    }
    void setCPU(int sp, int str, char* m)
    {
        speed = sp;
        Storage :: setSt(str);
        MFR :: setMF(m);
    }
    void showCPU()
    {
        cout << endl << "Speed = " << speed << endl;
        cout << endl << "Storage = " << Storage :: getSt() << endl;
        cout << endl << "MFR = " << MFR :: getMF() << endl;
    }
    ~CPU()
    {
        cout << endl << "7. Default destructor  for CPU called..." << endl;
    }
};



int main()
{

CPU cp1;						//cp1 uses default constructor of CPU
cp1.showCPU();					//display cp1
CPU cp2 (100, 1200, "Intel");  //cp2 uses overloaed constructor of CPU
cp2.showCPU();				//display cp2
int c = 400;
int s = 4500;
char* m = "Dell";
CPU cp3(c, s, m); // cp3 uses overloaded constructor
cp3.showCPU();    //disply through object cp3
c = 600;		//thses parametrs can be read from the keyboard
						//and then passed to cp4
s = 444888;
m = "hp";
CPU cp4;
cp4.setCPU(c, s, m);    //initialize  through setCPU
cp4.showCPU();

return 0;
}


Now this does not compile. I dont think he intended it to compile but hes not making a lot of sense in the class and even though we have had the homework for 2 weeks hes just now giving us the part that says it requires Multiple Inheritence so now i have to rewrite my entire program. So this is simply to ask if anyone can give me a cliff notes, nutshell, quick explanation of Multiple Inheritence.

The Homework is reletively straight forward. Create 3 classes:
1 class for Registration number
serial number
model number
2 class for all the cpu specs

3 for cpu breakdown

Sorry for the noobish question.
If you want the program i had created prior to this setup let me know. It does not compile yet either but i was almost finished with it i can post it here.... ah wtheck here it is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <string>

using namespace std;

class GeneralSpec
{
private:
	string Manufacturer;
	string Model;
	string ProType;
	int Year;
	int Weight;
public:
	void SetSpec(string a, string b, string c, int d, int e);
	string GetSpec();
};

class GeneralInfo
{
private:
	int RegNum;
	string Serial; 
	string Owner;
public:
	void SetInfo(int f, string g, string h);
	string GetInfo();
};

class Computer
{
private:
	
public:
	GeneralInfo Info;
	GeneralSpec Spec;

	Computer();
	~Computer();
};
string GeneralInfo::GetInfo()
{
	return RegNum, Serial, Owner;
}
void GeneralInfo::SetInfo(int f, string g, string h)
{
	RegNum = f;
	Serial = g;
	Owner = h;
}
string GeneralSpec::GetSpec()
{
	return Manufacturer, Model, ProType, Year, Weight;
}
void GeneralSpec::SetSpec(string a, string b, string c, int d, int e)
{
	Manufacturer = a;
	Model = b;
	ProType = c;
	Year = d;
	Weight = e;
}
int main()
{
	Computer PC1;
	Computer PC2;
	PC1.Info.SetInfo(545565, "GH554H451I", "George");
	PC1.Spec.SetSpec("HP", "GP7000", "AMD", 2013, 3);

return 0;
}


So the HW is intended to get 1 pre set computer and compare it to a copmuter spec the user inputs...i havent gotten to the part of asking the user to input info yet obviously.
Last edited on
Hi,
Let me try to explain to you... if it satisfies or not? i don't know.

Ok. So inheritance as the word implies is the taking/passin down of traits from a parent(base class) to an offspring(derived class). When a class inherits from another class, it takes all functions and variables except those under the private scope.
The class that inherits is the derived class, and the class that the derived class is obtained fom is the base class.

Syntax: class <derived class>:<inheritance scope> <base class>{};

An example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class test
{
public:
void print(){cout<<"how";
protected:
int colour;
private:
string secret;
};

class child:public test   //inheriting publicly from test
{};
/*in this case, the function print is present in the two classes because it is in the public scope of the base class. the variable colour is also inherited because it is protected not private. the private member, secret is not inherited. the inherited members are still under the same scope in the derived class as in the base class. if it were to be inheriting privately, the members would have been private in the derived class so likewise inheritin "protectedly"*/

// so multiple inheritance
class tes:public child, private test
{...}


HTH,
Aceix.
if tou still don't get it: http://cplusplus.com/doc/tutorial/inheritance/

Thanks,
Aceix.
Last edited on
So this is just an idea of passing down the public from class to class? Nothing more?
Topic archived. No new replies allowed.