Object and Class Issues

In this code I got age to work, but I think im misunderstanding HOW it works as I am trying to get it to also do weight but am running into a bunch of different errors.

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

class Person
{
    private: // age is private, so we cant access age and weight
        int age;
        float weight;

    public: // provides ways to access (show/change) the private age and weight
        void showAge();
        void changeAge(int newAge);
        void showWeight();
        void changeWeight(float newWeight);
        Person(); // Default Constructor
        Person(int newAge); // Explicit Constructor
        Weight();
        Weight(float newWeight);
};

void Person::showAge()
{
    cout << age << endl;
}

void Person::showWeight()
{
    cout << weight << endl;
}

void Person::changeAge(int newAge)
{
    age = newAge;
}

void Person::changeWeight(float newWeight)
{
    weight = newWeight;
}

Person::Person() // Default Constructor
{
    age = 101;
}

Person::Weight()
{
    weight = 202;
}

Person::Person(int newAge) // Explicit Constructor
{
    age = newAge;
}

Person::Weight(float newWeight)
{
    weight = newWeight;
}

int main()
{
    Person alex; // use Default Constructor
    alex.showAge(); // Garbage w/o Default Constructor, 101
    alex.changeAge(36); // changes value of age to 36
    alex.showAge(); //shows 36
    alex.showWeight();
    alex.changeWeight(195.5);
    alex.showWeight();

    Person child(8);// use Explicit Constructor
    Person.Weight child(120.2)
    child.showAge();
    child.showWeight();


    return 0;
}
constructors are special in that they do not have a type. Typically functions have a type, even if it is void.

Weight() is an error, then, because its a function that has no type and is not of the same name as its owning class so it isn't a constructor.

it should probably be
float Weight(); //will return the value of weight


another issue is your setters. While overloading a function (having 2 versions that have the same name, like Weight() and Weight(float) ) is useful at times, it is generally frowned upon to have the overloads do different things. I would have a GetWeight() and SetWeight(float) so the reader can see what is going on. What you are doing is legal, but disorienting (much moreso in a big program than a small one).
Last edited on
It is a very good thing to learn to debug because as you improve your programming skills your programs are going to be longer and longer and more complexed and sometimes it is necessary to now what is happening in the memory.
I look in error list and
1.line 72 error -you do not have ;
2.line 72 one more error -you can not wright classname.something
you can call functions only on Class objects i.e child.Weigt()
3.You defined function Weight but if it has no return value you must wright void behind
void Weight()
void Weight(float newWeight)
It is the same as showAge function there you have void
First you create an object and after you call function on that object.
Person child(12);
child.Weight(123.23);
Last edited on
Topic archived. No new replies allowed.