- Kód: Vybrat vše
class MyClass {
void Metoda();
};
Ak vytvorím napr. 4 objekty tejto triedy, ukladajú sa do pamäte aj metódy? alebo sa nejako špeciálne volajú?
Díky za odpovede
Your browser sent a request that this server could not understand.
class MyClass {
void Metoda();
};
#include <iostream>
using namespace std;
class Object {
private:
int a;
public:
Object(int a) {
this->a = a;
cout << "Volam konstruktor pro Object s hodnotou = " << a << endl;
}
~Object() {
cout << "Volam destruktor pro Object hodnoty = " << a << endl;
a=0;
}
void increment() {a++;}
int get() { return a; }
};
int main(int argc, char* argv[]) {
Object a(5);
Object b = Object(6);
Object* c = new Object(7);
cout << "a:" << a.get() << " b:" << b.get() << " c:" << c->get() << endl;
delete c;
}
Volam konstruktor pro Object s hodnotou = 5
Volam konstruktor pro Object s hodnotou = 6
Volam konstruktor pro Object s hodnotou = 7
a:5 b:6 c:7
Volam destruktor pro Object hodnoty = 7
Volam destruktor pro Object hodnoty = 6
Volam destruktor pro Object hodnoty = 5
#include <iostream>
class MyClass
{
public:
MyClass() // Constructor
{
m_a = 0;
m_b = 0;
m_result = 0;
};
~MyClass() // Destructor
{ };
private:
int m_a, m_b, m_result;
public:
void SetMethodA(int a) { m_a = a; };
void SetMethodB(int b) { m_b = b; };
int GetResult() { return m_result; };
void Calc_sum() { m_result = m_a + m_b; };
void Calc_subtract() { m_result = m_a - m_b; };
void Calc_multiply() { m_result = m_a * m_b; };
void Calc_divide() { m_result = m_a / m_b; };
};
int main()
{
MyClass myclass_1;
myclass_1.SetMethodA(123);
myclass_1.SetMethodB( 23);
myclass_1.Calc_subtract();
std::cout << "myclass_1: " << myclass_1.GetResult() << "\n";
MyClass myclass_2;
std::cout << "myclass_2: " << myclass_2.GetResult() << "\n";
return 0;
}
Uživatelé procházející toto fórum: Žádní registrovaní uživatelé a 23 návštevníků