Bonjour a tous,
j'ai trouver là https://www.tutorialspoint.com/cplus...nheritance.htm un exemple qui fonctionne dans un fichier cpp comment faire avec les quatre fichier suivant Rectangle.h Rectangle.cpp Shape.h Shape.cpp ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 /* * E.h * * Created on: 6 mars 2021 * Author: vincent */ #ifndef RECTANGLE_H_ #define RECTANGLE_H_ #include "Shape.h" class Rectangle: public Shape { public: Rectangle(); virtual ~Rectangle(); }; #endif /* RECTANGLE_H_ */
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 /* * E.cpp * * Created on: 6 mars 2021 * Author: vincent */ #include "Rectangle.h" #include <iostream> using namespace std; // Derived class class Rectangle: public Shape { public: int getArea() { return (width * height); } };
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 /* * E.cpp * * Created on: 6 mars 2021 * Author: vincent */ #include "Rectangle.h" #include <iostream> using namespace std; // Derived class class Rectangle: public Shape { public: int getArea() { return (width * height); } };
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 /* * B.h * * Created on: 6 mars 2021 * Author: vincent */ #ifndef SHAPE_H_ #define SHAPE_H_ class Shape { public: Shape(); virtual ~Shape(); }; #endif /* SHAPE_H_ */
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #include <iostream> #include "Shape.h" #include "Rectangle.h" using namespace std; int main(void) { Rectangle Rect; Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object. cout << "Total area: " << Rect.getArea() << endl; return 0; }
Partager