Hey all, this is my class header file, there's no problem with it as far as I can tell. The reason I'm here asking for help is because I'm failing to understand it, we did this in class and a .cpp file.
My understanding of it so far is: Public - Declares variables that you can use outside of your class, private declares variables you can only use in your class.
No idea what the void means. The char firstname[] char secondname[] etc are all properties/attributes/characteristics of the Person class, does this mean they're objects? The main problem is understanding what the void thing does, does it create a variable called create firstname which saves the information entered to the variable in private called firename[]?
Any help with an explanation would be great and the simpler the better! Thanks guys
void doesn't do anything. void createFirstName(char firstname[]); is a function declaration. A general form of function declaration is return_type function_name(list_of_arguments_if_any);. So void is a return type, meaning that a function doesn't return anything at all.
By the way, it looks like there are missing () on lines 8, 11, 14, 17, 20. showFirstName sounds like a function but is declared as a variable.
These are member function declarations. void means that the function does not return a value.
I think it's weird that you would cover classes before you covered functions in your class.
Every function (except constructors and destructors) have a "return type". The return type is used as "output" of the function. For example if you have a function that adds two numbers together and returns the sum:
1 2 3 4
int sum(int a, int b) // the return type is 'int'
{
return a+b; // this means you can return an integer
}
Whatever you return gets fed back to the the code that called the function. For example:
1 2
int foo = sum(3, 5); // sum will return 8, therefore...
cout << foo; // this will print "8"
In this context, void simply means the function doesn't return anything.
[quote]The char firstname[] char secondname[] etc are all properties/attributes/characteristics of the Person class, does this mean they're objects?
Well you have some name conflicts there. If you're referring to the firstname[], secondname[] etc on line 5, those are arguments for the constructor. If you're referring to the ones on lines 24+, those are members of the class (basically the same thing as properties/attributes/characteristics)
Sorry to double post but I think it's all sorted my problem is with my other two pages now, I'm trying to work through all the errors and these are the current versions of main.cpp and randomtestclass.cpp:
#include <iostream>
usingnamespace std;
#include "randomclassheader.h"
#include <string.h>
#include <cstdlib>
int main()
{
char firstname[50];
char secondname[50];
char eyecolour[50];
int age;
float weight;
Person(firstname, secondname, eyecolour, age, weight);
cout << "Please enter your first name:" << endl;
cin >> firstname;
cout << "Please enter your second name:";
cin >> secondname;
cout << "Please enter your eye colour:";
cin >> eyecolour;
cout << "Please enter your age:";
cin >> age;
cout << "Please enter your weight:";
cin >> weight;
cout << "This is your first name: " << firstname;
cout << endl << endl << endl << endl << "This is the name of the first person in the class: " <<endl;
return 0;
}
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp||In constructor ‘Person::Person(char*, char*, char*, int, float)’:|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp|11|error: ‘firstname’ was not declared in this scope|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp|12|error: returning a value from a constructor|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp||In member function ‘void Person::createFirstName(char*)’:|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp|20|error: ‘firstname’ was not declared in this scope|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp|21|error: return-statement with a value, in function returning 'void'|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp||In member function ‘void Person::createSecondName(char*)’:|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp|27|error: ‘secondname’ was not declared in this scope|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp|28|error: return-statement with a value, in function returning 'void'|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp|32|error: prototype for ‘void Person::createWeight(float*)’ does not match any in class ‘Person’|
/home/rej3kt/Desktop/random test class/Random test class/randomclassheader.h|32|error: candidate is: void Person::createWeight(float)|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp|39|error: prototype for ‘void Person::createAge(char*)’ does not match any in class ‘Person’|
/home/rej3kt/Desktop/random test class/Random test class/randomclassheader.h|26|error: candidate is: void Person::createAge(int)|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp||In member function ‘void Person::createEyeColoure(char*)’:|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp|48|error: ‘eyecolour’ was not declared in this scope|
/home/rej3kt/Desktop/random test class/Random test class/randomtestclass.cpp|49|error: return-statement with a value, in function returning 'void'|
||=== Build finished: 12 errors, 0 warnings ===|