Header Guards

Pages: 12
That human_identity variable is not c1's human_identity. You'd have to do this:

1
2
3
4
cout << "Enter your identity number: " <<endl
cin >> human_identity;
c1.setID(human_identity);
cout << c1.getID();

Or, if c1 hasn't been declared yet,

1
2
3
4
cout << "Enter your identity number: " <<endl
cin >> human_identity;
Human Customer c1(human_identity);
cout << c1.getID();

rej3kt wrote:
Does that make any difference to my program Fillipe?

You mean inlining? Inlining very small functions removes the cost of a function call and makes your code slightly faster, but that doesn't really matter for a small program. However, since Human is such a simple class and all of its functions can be inlined, it allows you to avoid having a .cpp file for the class.

EDIT: I guess c1 is a Customer
Last edited on
Thanks for all the help I've got it compiling. I think I'll leave inlining to another day though.

This is what I've tried:
1
2
3
4
5
6
  Human h1(human_identity);
    cout << "Enter your identification number: " << endl;
    cin >> human_identity;
    h1.setID(human_identity);
   Customer c1(cust_name, cust_gender, cust_phone, cust_pcode, cust_dob);
    cout << "This is your id number accessed from another class: " << c1.getID();


It's spitting out some random numbers that shouldn't be in c1.getID(); Obviously I haven't declared getID(); in my customer file as I'm trying to inherit it from my human class.

The cpp for the human class is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Human::Human()
{
}
Human::Human(int identity)
{
   human_identity = identity;


}

void Human::setID(int identity)
{
	human_identity = identity;
}
int Human::getID()
{
	return human_identity;
}
The constructor you made for class Customer doesn't take any value to fill human_identity, it seems, so it will be left uninitialized.
I didn't think it should have taken any value. I thought the whole point of inheritance was that I can use the method / members from another class without having to rewrite them all.
I'm not a big fan of inheritance and I usually go with composition, but according to this: http://www.cplusplus.com/doc/tutorial/inheritance/ , "although the constructors and destructors of the base class are not inherited themselves, its default constructor (i.e., its constructor with no parameters) and its destructor are always called when a new object of a derived class is created or destroyed."

You forgot to make the default constructor for Human initialize variable human_identity. However, that will only set it to a default value. You still need to pass it some value.
Am I not "passing it some value" in my int main where the person is entering information that is to be stored in human_identity, and then it is copied to the class through the .cpp file function that says human_identity=identity; ?

I'm so confused about inheritance now, I thought it was simple, I thought all that inheritance did was a variable such customer(char phone[]) and allow that to be called from another class. I don't understand all this initialising human_identity. Sorry I'm really struggling with this, I've watched and read quite a few tutorials now as well.

Unfortunatly I have to use inheritance in my assignment for college. Got to add association and aggregation in aswel after, what ever they are. Will have to research them after I've got this bloody inheritance working.
You're calling setID() on h1, not on c1. Those are different objects.
I thought it didn't matter where I called it from because I'm using inheritance so I should be able to use the methods of another class (methods from human should be able to be used by the customer class?)
Yes, but h1 and c1 are still different objects. h1 has a data member named human_identity while c1 has its own data member with the same name, but they're not the same object. You could have a thousand Customer or Human objects, each with a different value. Example:

1
2
3
int a = 1;
int b = 2;
a = 3;  // does not change the value of b! 
Makes sense, what do you propose I do? Create a function in customer.cpp to write to human_identity?
Just do this:

c1.setID(foo);

Or write a constructor for Customer that also takes a value for human_identity.
This is seriously the most confusing thing I've ever done in c++. I will never use inheritance again after this.

Here's the constructor I made in cust.cpp

1
2
3
4
5
6
7
8
void Customer::setID(int identity)
{
	human_identity = identity;
}
int Customer::getID()
{
	return human_identity;
}


and here's the error it's giving me
1
2
3
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp|69|error: no ‘void Customer::setID(int)’ member function declared in class ‘Customer’|
/home/rej3kt/Desktop/C++ Class/custclass/cust.cpp|73|error: no ‘int Customer::getID()’ member function declared in class ‘Customer’|
||=== Build finished: 2 errors, 0 warnings ===|


I've reread what you put several times and it still doesn't make sense to me. Inheritance should let me use the method of one class in another class, so why when I use it with an object from the other class is it throwing up errors Grrr I've never been so frustrated. If I'm having to write a constructor for it then where is the inheritance?!
You're apparently confused as to what inheritance actually is.
It certainly would help if you posted all of your current code instead of just snippets that are unrelated to the problem.
Those are not constructors. Where are you learning C++ from? You should read the bit about classes again, IMO. Customer class inherits those two methods from Human. You don't need to write them again. That's the point of inheritance. Just use them.

By the way, a constructor is a function with no return type and named the same as the class itself.
I'll post my whole code for you then Athar.

customer.cpp
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
#include "cust.h"
#include "staff.h"
#include "human.h"
#include <string.h>

Customer::Customer()
{
}
Customer::Customer(char name[], char gender[], int phone, char pcode[], float dob)
{
    strcpy(cust_name, name);
    strcpy(cust_gender, gender);
    cust_phone = phone;
    strcpy(cust_pcode, pcode);
    cust_dob = dob;


}

void Customer::setName(char name[])
{
	strcpy(cust_name, name);
}
char* Customer::getName()
{
	return cust_name;
}



void Customer::setGender(char gender[])
{
    strcpy(cust_gender, gender);
}
char* Customer::getGender()
{
    return cust_gender;
}


void Customer::setPhone(int phone)
{
    cust_phone = phone;
}
int Customer::getPhone()
{
    return cust_phone;
}


void Customer::setPcode(char pcode[])
{
    strcpy(cust_pcode, pcode);
}
char* Customer::getPcode()
{
    return cust_pcode;
}

void Customer::setDob(float dob)
{
    cust_dob = dob;
}
float Customer::getDob()
{
    return cust_dob;
}


human.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "cust.h"
#include "staff.h"
#include "human.h"
#include <string.h>


Human::Human()
{
}
Human::Human(int identity)
{
   human_identity = identity;


}

void Human::setID(int identity)
{
	human_identity = identity;
}
int Human::getID()
{
	return human_identity;
}


main.cpp
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
#include <iostream>
#include <string.h>
#include "cust.h"
#include "staff.h"
#include <cstdlib>
#include <fstream>
using namespace std;

int main()
{
    int human_identity;
    char cust_name[50];
    char cust_gender[50];
    int cust_phone;
    char cust_pcode[50];
    float cust_dob;
    int number;
    char username[50];
    int selection;

    //attempting to test inheritance but fucking it up
    Human h1(human_identity);
    cout << "Enter your identification number: " << endl;
    cin >> human_identity;
    h1.setID(human_identity);

    Customer c1(cust_name, cust_gender, cust_phone, cust_pcode, cust_dob);

    cout << "This is your id number accessed from another class: " << c1.getID();


    cout << "Please enter your name to start: "<<endl;
    cin.getline(username, 50);


    cout << "Please choose a menu: "<<endl;
    cout << "1) Enter your customer information" <<endl;
    cout << "2) Enter your staff information" <<endl;
    cin >> selection;

if (selection == 1)
{

    do{

    cout << "\n\n\nWelcome, please enter a number to go to that section on the menu:\n1) Enter the name of a customer"<<endl;
    cout << "2) Enter the gender of a customer" << endl;
    cout << "3) Enter the phone number of the customer"<<endl;
    cout << "4) Enter the postcode of the customer"<<endl;
    cout << "5) Enterthe date of birth for the customer"<<endl;
    cout << "6) Save any informaion entered" <<endl;
    cout << "7) Retrieve any previously entered information" <<endl;
    cout << "8) Exit program"<<endl;
    cin >> number;

    ofstream myfile;

 switch (number){
  case 1:
    cout << "Enter the name of the customer: " << endl;
    cin >> cust_name;
    c1.setName(cust_name);
    break;
  case 2:
    cout << "Enter the gender of the customer: " << endl;
    cin >> cust_gender;
    c1.setGender(cust_gender); break;
  case 3:
    cout << "Enter the phone number of the customer: " << endl;
    cin >> cust_phone;
    c1.setPhone(cust_phone); break;
  case 4:
    cout << "Enter the postcode of the customer: " << endl;
    cin >> cust_pcode;
    c1.setPcode(cust_pcode);
                       break;
  case 5:
    cout << "Enter the date of birth of the customer: " << endl;
    cin >> cust_dob;
    c1.setDob(cust_dob); break;
  case 6:

   cout << "Saved!"<<endl;

    // writing any information in the following variables to a text file
   myfile.open ("class2.txt");
   myfile << c1.getName() << endl;
   myfile << c1.getGender() << endl;
   myfile << c1.getPhone() << endl;
   myfile << c1.getPcode() << endl;
   myfile << c1.getDob() <<endl;

   myfile.close();
   break;


    case 7:
    break;

 case 8:
    cout << "Press enter to close"<<endl;
    break;
    return 0;

}

    }while(number !=8);


}else
   // Testing the class

    cout << "\n\n\n\n\n\n\n\n\n" ;
    cout << "Your car name is: " << c1.getName() <<endl;
    cout << "Your car colour is: " << c1.getGender() <<endl;
    cout << "Your car is " << c1.getPhone() << " years old"<<endl;
    cout << "Your car is worth £" <<c1.getPcode() <<endl;
    cout << "Your car does " <<c1.getDob()<< "mpg"<<endl;


    return 0;
}


staff.cpp
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
#include "staff.h"
#include "cust.h"
#include "human.h"
#include <string.h>

Staff::Staff()
{
}

Staff::Staff(char rank[], char annualWage[], float hours)
{
    strcpy(staff_rank, rank);
    strcpy(staff_wage, annualWage);
    staff_hours = hours;

}

void Staff::setRank(char rank[])
{
	strcpy(staff_rank, rank);
}
char* Staff::getRank()
{
	return staff_rank;
}



void Staff::setWage(char annualWage[])
{
    strcpy(staff_wage, annualWage);
}
char* Staff::getWage()
{
    return staff_wage;
}


void Staff::setHours(float hours)
{
    staff_hours = hours;
}
float Staff::getHours()
{
    return staff_hours;
}


cust.h
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
#ifndef CUST_H_INCLUDED  // must be unique!
#define CUST_H_INCLUDED
#include "human.h"





class Customer : public Human
{



 public:
     Customer();
    Customer(char name[], char gender[], int phone, char pcode[], float dob);

    void setName(char name[]);
    char* getName();

    void setGender(char get[]);
    char* getGender();

    void setPhone(int phone);
    int getPhone();

    void setPcode(char pcode[]);
    char* getPcode();

    void setDob(float dob);
    float getDob();


 private:
    char cust_name[50];
    char cust_gender[50];
    int cust_phone;
    char cust_pcode[50];
    float cust_dob;
};

#endif

human.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef HUMAN_H_INCLUDED  // must be unique!
#define HUMAN_H_INCLUDED


class Human
{


    public:
    Human();
    Human(int identity);
    void setID(int identity);
    int getID();

    private:
    int human_identity;
};

#endif 


staff.h
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
#ifndef STAFF_H_INCLUDED  // must be unique!
#define STAFF_H_INCLUDED
class Staff
{


 public:
    Staff();

    Staff(char rank[], char annualWage[], float hours);

    void setRank(char rank[]);
    char* getRank();

    void setWage(char annualWage[]);
    char* getWage();

    void setHours(float hours);
    float getHours();



 private:
    char staff_rank[50];
    char staff_wage[50];
    float staff_hours;
};
#endif


As you can probably see there's quite a bit of code, that's why I wasn't posting it all. I think i'll read up about constructors, I think constructors are in the header file and basically declare the members and then methods are just functions which you set for like writing or reading from the members. We've been learning at college but we've broken up for a week and a bit and I've been trying to get as much done as I can so most of this is self taught.
Last edited on
Sorry but going to have to bump, I knew all the code would scare people off :)
Take a look at the code you posted for a minute. At least two of the files are just wrong.
I'm such an idiot, it's because I was in a rush to post it before I went out. I've edited and reposted the correct files :)
Finally fixed it and got inheritance working!!!!
Topic archived. No new replies allowed.
Pages: 12