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.
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.
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.
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!
/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.
#include <iostream>
#include <string.h>
#include "cust.h"
#include "staff.h"
#include <cstdlib>
#include <fstream>
usingnamespace 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;
}
#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
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.