How to use a function in a CLASS

Hi, I successfully made my first programme with a CLASS. The code is separated by main.cpp / Fusee.cpp / Fusee.h. Now I want to had a function f.allume(); int eh main.cpp. Basicaly the function is suppose to ignite a rocket for take off. So the only thing the function should do is to change this display / \ to /WW\

How can I do that without touching the 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
 #include <ostream>
#include <string>

#ifndef FUSEE_H
#define FUSEE_H
using std::ostream;

class Fusee
{
	int m_numEtage;
	int nLargeur;
	int nHauteur;
	

public:

	Fusee(int numEtage);//constructor qui ressoit le nombre d'étage de la fusée
	void largeur(int nLargeur);//nouvelle largeur d'un étage
	void hauteur(int nHauteur);//nouvelle hauteur d'un étage
	void allume();
	void dessinerFusee(ostream& out, Fusee& f);
	
	friend ostream& operator<<(ostream& out, Fusee& f);
};


#endif 


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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
Fusee::Fusee(int numEtage)
{
	nLargeur = 8;
	nHauteur = 4;
	m_numEtage = numEtage;
	
}

void Fusee::largeur(int largeur)
{
	nLargeur = largeur;
}
void Fusee::hauteur(int hauteur)
{
	nHauteur = hauteur;
}
void Fusee::allume()
{
	
}

void Fusee::dessinerFusee(ostream& out, Fusee& f)
{
	
	int i, j, k = 0;//////////////debut affichage CARGO
	for (i = 1; i <= nHauteur; i++) // lignes
	{
		out << " ";
		// Esapces du milieu 
		for (j = i; j <= nHauteur; j++) 
		{
			out << " ";
		}
		// affiche des /
		while (k != (2 * i - 1)) 
		{
			if (k == 0 )
			{
				out << "\/";
			}
			else
			{
				out << " ";
			}
			if (k == 2 * i - 2)
			{
				out << "\\";
			}
			k++;
			;
		}
		k = 0;
		out << endl; // prochaine ligne
	}
	// affiche la derniere ligne du CARGO
	out << " ";
	out << "\/";
	for (i = 0; i < 2 * nHauteur; i++)
	{
		out << "_";
	}
	out << "\\";
	out << endl;//////////////fin de l'affichage du CARGO 

	
	for (int i = 0; i < m_numEtage; i++)//////////////début affichage du RÉSERVOIR
	{
		

		for (int i = 1; i < nHauteur; i++)
		{
			out << " ";
			out << "|";
			for (int j = 0; j < nLargeur; j++)
			{
				out << " ";
			}
			out << "|\n";
		}

		out << " ";
		
		
		out << "|";
		for (int i = 1; i <= nLargeur; i++)
		{
			
			out << "_";
		}
		out << "|";
		out << "\n";
	}

	
	
}

ostream& operator<<(ostream& out, Fusee& f)
{
	f.dessinerFusee(out, f);
	return out;
}
Last edited on
You have to call the function somewhere. If not in main, you need to call it from something called by main.
You can do weird stuff that would avoid an explicit call -- like calling it from the constructor or having an event driven program of some sort --- but calling it from the constructor is not a good design for this, and the other ways to do it are advanced and use nonstandard language extensions.


Just call it from main.


Topic archived. No new replies allowed.