___ ___ erkimt How to create a class inside the other in C ++? [closed] ______ qstntxt ___
I need to create a class within another C ++
For example:
I already have the Calculator class created. Must enter inside the OperadorAritmetico class. How should I create? How will get the .h header file and .cpp source?
Someone to give me strength?
Thanks!
Just use class
%% within the class
. Remember that the visibility applies the same as any other method the variable class. In the example below the class OperadorAritmetico
is private.
// Calculadora HPP
class Calculadora {
class OperadorAritmetico {
public:
int soma(int a, int b);
};
OperadorAritmetico operador;
public:
int soma(int a, int b);
};
The source did not forget to put the full path of the method.
// Calculadora.cpp
#include <iostream>
#include "Calculadora.hpp"
int Calculadora::OperadorAritmetico::soma( int a, int b )
{
return a+b;
}
int Calculadora::soma( int a, int b )
{
return operador.soma( a, b );
}
In this simple test method soma
of% Calculadora
internally calls the soma
% of OperadorAritmético
.
int main( void )
{
Calculadora calc;
std::cout << "3 + 5 = " << calc.soma( 3, 5 ) << std::endl;
}