Basic classes/constructor question

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


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
class Person
{

 public:
    Person (char firstname[], char secondname[], int age, char eyecolour[], float weight);

    void createFirstName(char firstname[]);
    char* showFirstName;

    void createSecondtName(char secondname[]);
    char* showSecondName;

    void createAge(int age);
    int showAge;

    void createEyeColoure(char EyeColour[]);
    char* showEyeColour;

    void createWeight(float weight);
    float showWeight;


 private:
    char firstname[50];
    char secondname[50];
    char eyecolour[50];
    int age;
    float weight;
};
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.
No idea what the void means.[quote]

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)


EDIT: doh, too slow =x
Last edited on
This is the updated header file, hopefully there are no conflicts now, thanks for the heads up about the brackets, completley forgot about them.


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

#ifndef RANDOMCLASSHEADER_H_INCLUDED
#define RANDOMCLASSHEADER_H_INCLUDED



#endif // RANDOMCLASSHEADER_H_INCLUDED





class Person
{

    Person();

 public:
    Person(char firstName[], char secondName[], int Age, char EyeColour[], float Weight);

    void createFirstName(char firstName[]);
    char* showFirstName();

    void createSecondName(char secondName[]);
    char* showSecondName();

    void createAge(int Age);
    int showAge();

    void createEyeColoure(char EyeColour[]);
    char* showEyeColour();

    void createWeight(float Weight);
    float showWeight();


 private:
    char firstName[50];
    char secondName[50];
    char EyeColour[50];
    int Age;
    float Weight;
};




I'm getting a Person:Person() is private error when trying to compile here :/
edit: That seems to be sorted by deleting Person();
Last edited on
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:

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
#include <iostream>
using namespace 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;
}




and for my randomtestclass.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
#include <iostream>
using namespace std;
#include "randomclassheader.h"
#include <string.h>




Person::Person(char firstName[], char secondName[] ,char EyeColour[] ,int Age, float weight)
{
	strcpy(firstname, firstName);
	return firstName;

}



void Person::createFirstName(char createFirstName[])
{
    strcpy(firstname, createFirstName);
    return firstName;
}


void Person::createSecondName(char createSecondName[])
{
    strcpy(secondname, createSecondName);
    return secondName;
}


void Person::createWeight(float createWeight[])
{
    weight == Weight;
    return Weight;
}


void Person::createAge(char createAge[])
{
    age == Age;
    return Age;
}


void Person::createEyeColoure(char createEyeColour[])
{
    strcpy(eyecolour, createEyeColoure);
    return EyeColour;
}



The errors I'm getting are:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

/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 forvoid 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 forvoid 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 ===|
Topic archived. No new replies allowed.