cs31 lecture 17

Manipulating x the standard template library provides

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: layId()
; void }; }; Car::Car()
 { Car::Car()
 uniqueId = 0; uniqueId } void Car::displayId()
 { std::cout << uniqueId << std::endl; std::cout } 121 Destructor x A destructor is a special method of a class, which is destructor invoked each time an object is deleted invoked Syntax: the constructor is a method with the exact same name as Syntax: the class, prefixed by ~ the Example: ~Car()
 x There can be only one destructor There No argument can be passed x There can be no destructor in the C++ program A C++ compiler usually provides a default destructor, which C++ simply does nothings simply 122 Example struct Car { /// constructor / Car()
; Car()
; /// destructor / ~Car()
; /// attributes / int uniqueId; int /// methods / void displayId()
; void }; }; Car::Car()
 { Car::Car()
 std::cout << “Object constructed” << std::endl; std::cout uniqueId = 0; uniqueId } Car::~Car()
 { std::cout << “Object destroyed” << std::endl; std::cout } void Car::displayId()
 { std::cout << uniqueId << std::endl; std::cout 123 Why constructor and destructors are useful x Memory management Some attributes may be dyna...
View Full Document

Ask a homework question - tutors are online