Including headers & .cpp files

closed account (1vD3vCM9)
Hi everyone.
I racently try to build an calculator, but.
I saw that there is an option to Add .cpp files and header files! Which didnt work for me.
I followed a tutorial on the web.
Here is what i typed:


main.cpp
1
2
3
4
5
6
7
8
9
 #include <iostream>
#include <string>
#include "myclass.h"

int main()
{
      MyClass a;
      Return(0);
}



myclass.cpp
1
2
3
4
5
6
7
8
9
10
#include "myclass.h"
#include <iostream>
#include <string>

Using namespace std;

Void MyClass::foo();
{
      Cout << "hello world!"
}



myclass.h
1
2
3
4
5
6
7
8
9
10
11
#ifndef MYCLASS_H_INCLUDED
#define MYCLASS_H_INCLUDED
#include "myclass.h"

class MyClass{
public:
void foo();
int bar;
};

#endif // MYCLASS_H_INCLUDED 


How can i make this work?
Please a good explnation.
Thanks
Last edited on
the first thing i noticed is you might have some things backwards?.?. i just learned this subject myself. but my object orientated programming and classes layout was like this.

in your myclass.cpp you dont need to include any header files or namespaces EXCEPT #include "myclass.h"

but you do need to include all the same header files and namespace that are in your main.cpp in your myclass.h

so thats one thing ive noticed that you had backwards

now i have some questions?

is int bar; your private: // member variable? and if so what is it doing?
also your question and program is kinda of vague, because i dont see anything that says calculator unless im mistaking. what i can do is show you a simple practice program i did pertaining to classes. it calculates a patience BMI.

main.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
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <string>

#include "BMI.h"

using namespace std;

int main()
{

	string name;
	int hieght;
	double weight;

	cout << "Enter your name: ";
	cin >> name;
	cout << "\n";

	cout << "Enter your hieght (inches): ";
	cin >> hieght;
	cout << "\n";

	cout << "Enter your weight (pounds): ";
	cin >> weight;
	cout << "\n\n";


	//Object

	BMI Patient_1(name, hieght, weight);

	cout << "\n" << "Patient name : " << Patient_1.getName() << "\n";
	cout << "Patient hieght: " << Patient_1.getHieght() << "\n";
	cout << "Patient wieght: " << Patient_1.getWieght() << "\n";
	cout << "BMI: " << Patient_1.calculateBMI() << "\n\n";

	// Access mutator functions
	cout << "Enter your name: ";
	cin >> name;
	cout << "\n";

	cout << "Enter your hieght (inches): ";
	cin >> hieght;
	cout << "\n";

	cout << "Enter your weight (pounds): ";
	cin >> weight;
	cout << "\n\n";

	BMI Patient_2;

	Patient_2.setName(name);
	Patient_2.setHieght(hieght);
	Patient_2.setWieght(weight);

	cout << "\n" << "Patient name : " << Patient_2.getName() << "\n";
	cout << "Patient hieght: " << Patient_2.getHieght() << "\n";
	cout << "Patient wieght: " << Patient_2.getWieght() << "\n";
	cout << "BMI: " << Patient_2.calculateBMI() << "\n\n";

	cout << "\n\n";
	system("PAUSE");
	return 0;
}


BMI.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
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
79
80
81
82
83
84
85
86
87
88
// function definition

#include "BMI.h"


// Basically calling your default constructor
BMI::BMI()
{
	// member variables must be set to null state(*note ==> string variable is automatically set to null state)

	newhieght = 0;
	newweight = 0.0;



}

// calls overload constructor

BMI::BMI(string name, int hieght, double weight)
{

	newname = name;
	newhieght = hieght;
	newweight = weight;



}

//calls destructor
BMI::~BMI()
{

}

//calls accessor functions

string BMI::getName() const
{
	return newname;

}

int BMI::getHieght() const
{
	return newhieght;


}

double BMI::getWieght() const
{
	return newweight;

}

// calls mutator function

void BMI::setName(string name)
{

	newname = name;

}

void BMI::setHieght(int hieght)
{
	newhieght = hieght;

}

void BMI::setWieght(double wieght)
{

	newweight = wieght;

}

// calls function to calculate BMI

double BMI::calculateBMI() const
{
	// formula for BMI
	return ((newweight * 703) / (newhieght * newhieght));


}


BMI.h
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
// Header ==> function declarations 

#include <iostream>
#include <string>

using namespace std;

#ifndef BMI_H
#define BMI_H


class BMI
{
public:
	// DEfault constructor
	BMI();

	// overload constructor
	BMI(string, int, double);

	//Destructor
	~BMI();

	//Accessor functions
	string getName() const;
		// getName - name of patient
		// @return string - returns name of patient

	int getHieght() const;
		// getHieght - hieght of patient
		// @return int - returns hieght of patient

	double getWieght() const;
		// getWieght - wieght of patient
		// @return double - returns wieght of patient

	//Mutator functions
	void setName(string);
		// setName - sets name of patient
		// @param string - name of patient

	void setHieght(int);
		// setHieght - sets hieght of patient
		// @param - hieght of patient

	void setWieght(double);
		// setWieght - sets weight of patient
		// @param - wieght of patient

	double calculateBMI() const;
		// calculateBMI - calculates the BMI
		// @param - BMI calculations

private:
	// memeber variables
	string newname;
	int newhieght;
	double newweight;



};




#endif 
closed account (1vD3vCM9)
About the questions:
This.. Isnt connected to thw calculator AT all.
I just pasted the code i learned from that video
And thanks for answering.
Topic archived. No new replies allowed.