Klasa C ++ konstruktora i destruktora
Konstruktor klasy
Konstruktor klasy jest specjalna funkcja składowa klasy, wykonuje za każdym razem nowy obiekt klasy jest tworzony.
Nazwa Nazwa konstruktora klasy jest identyczna i nie wróci dowolnego typu, nie powróci nieważne. Konstruktor może być używany do ustawiania wartości dla określonej zmiennej członkowskim.
Poniższe przykłady pomagają lepiej zrozumieć pojęcie konstruktora:
#include <iostream> using namespace std; class Line { public: void setLength( double len ); double getLength( void ); Line(); // 这是构造函数 private: double length; }; // 成员函数定义,包括构造函数 Line::Line(void) { cout << "Object is being created" << endl; } void Line::setLength( double len ) { length = len; } double Line::getLength( void ) { return length; } // 程序的主函数 int main( ) { Line line; // 设置长度 line.setLength(6.0); cout << "Length of line : " << line.getLength() <<endl; return 0; }
Gdy powyższy kod jest kompilowany i wykonany, że daje następujące wyniki:
Object is being created Length of line : 6
Konstruktor z parametrami
Domyślny konstruktor bez argumentów, ale w razie potrzeby, konstruktor może mieć parametry. Zatem po utworzeniu przedmiotu do obiektu zostanie przypisana do wartości początkowej, jak pokazano w poniższym przykładzie:
#include <iostream> using namespace std; class Line { public: void setLength( double len ); double getLength( void ); Line(double len); // 这是构造函数 private: double length; }; // 成员函数定义,包括构造函数 Line::Line( double len) { cout << "Object is being created, length = " << len << endl; length = len; } void Line::setLength( double len ) { length = len; } double Line::getLength( void ) { return length; } // 程序的主函数 int main( ) { Line line(10.0); // 获取默认设置的长度 cout << "Length of line : " << line.getLength() <<endl; // 再次设置长度 line.setLength(6.0); cout << "Length of line : " << line.getLength() <<endl; return 0; }
Gdy powyższy kod jest kompilowany i wykonany, że daje następujące wyniki:
Object is being created, length = 10 Length of line : 10 Length of line : 6
Korzystanie z listy inicjalizacji zainicjować pole
Korzystanie z listy inicjalizacji zainicjować pola:
Line::Line( double len): length(len) { cout << "Object is being created, length = " << len << endl; }
Powyższa składnia jest równoważna z następującą składnią:
Line::Line( double len) { cout << "Object is being created, length = " << len << endl; length = len; }
Załóżmy, że masz klasę C, mający wiele pól X, Y, Z, itp muszą być inicjowane z tego samego powodu, można użyć powyższej składni, wystarczy użyć oddzielonych przecinkami w różnych dziedzinach, a mianowicie:
C::C( double a, double b, double c): X(a), Y(b), Z(c) { .... }
Klasa destructor
KlasaDestruktor jest specjalną funkcją składową klasy, zostanie wykonany za każdym razem obiekt jest tworzony usunięty.
Nazwa Nazwa destruktora klasy jest identyczna, tylko poprzedzonego tyldy (~) jako przedrostek, nie zwraca żadnej wartości, to nie może mieć żadnych argumentów. Destructor pomoc z programu (takich jak zamykanie plików do wolnej pamięci, itd.) Przed wydaniem zasobów.
Poniższe przykłady pomagają lepiej rozwiązać pojęcie funkcji struktury geograficznej:
#include <iostream> using namespace std; class Line { public: void setLength( double len ); double getLength( void ); Line(); // 这是构造函数声明 ~Line(); // 这是析构函数声明 private: double length; }; // 成员函数定义,包括构造函数 Line::Line(void) { cout << "Object is being created" << endl; } Line::~Line(void) { cout << "Object is being deleted" << endl; } void Line::setLength( double len ) { length = len; } double Line::getLength( void ) { return length; } // 程序的主函数 int main( ) { Line line; // 设置长度 line.setLength(6.0); cout << "Length of line : " << line.getLength() <<endl; return 0; }
Gdy powyższy kod jest kompilowany i wykonany, że daje następujące wyniki:
Object is being created Length of line : 6 Object is being deleted