class problem

#include <iostream>
using namespace std;
class Person
{public:
void set(string nameVar,double weightVar,double heightVar);
double calculateBMI();
Person();

private:
string name;
double weight;
double height;
};
int main()
{
Person Charles;
Charles.set(Charles,62,1.65);
Charles.calculateBMI();

double BMI=calculateBMI();

cout<<BMI;




}
double Person::calculateBMI()
{
double BMI=weight/(height*height);

return BMI;

}

void Person::set(string nameVar,double weightVar,double heightVar)
{
nameVar=name;
weightVar=weight;
heightVar=height;
}

why i use charles.calculateBMI doesnot work,is it my class got problem?
Last edited on
the string constants should be,for example,as"Charles", then it work
ps.I'm a newbie, but i'm glad to share with you guys what i know.So if it wrong,please don't blame on me.Thankyou.ps.terrriblely sorry for my poor English presentation,and I hope to improve my English standard from you guys:)
closed account (48T7M4Gy)
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 std::cout;
using std::string;

class Person
{
    public:
    Person(){}; // <--
    
    double calculateBMI()
    {
        double BMI=weight/(height*height);
        return BMI;
    }
    
    void set( string nameVar, double weightVar, double heightVar )
    {
        name = nameVar; // <--
        weight = weightVar; // <--
        height = heightVar; // <--
    }

private:
    string name;
    double weight;
    double height;
};

int main()
{
    Person Charles;
    Charles.set("Charles", 62, 1.65); // <--
    Charles.calculateBMI();

    double BMI = Charles.calculateBMI();
    cout << BMI;
    
    return 0; // <--
}
Last edited on
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
#include <iostream>
#include<string>

using namespace std;


class person
{
private:
	string name;
	double weight;
	double height;

public:
	void set(string nameVar,double weightVar,double heightVar);
	double calculateBMI(void);
};




void person::set(string nameVar,double weightVar,double heightVar)
{
name=nameVar;
weight=weightVar;
height=heightVar;
}

double person::calculateBMI(void)
{
double BMI=weight/(height*height);

return BMI;

}


int main()
{
person a;
a.set("hi",62,1.65);
double BMI=a.calculateBMI();

cout<<BMI;
}
Last edited on
Topic archived. No new replies allowed.