cs31 lecture 17

The constructor is a method with the exact same name

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: allocated to store /// it. / bar += 42; // We use the variable (reading/writing its content)
 /// The function terminates. The data region for ‘bar’ is freed / } x Example for an object: void foo()
 { myInt bar; // The variable is declared. A data region is allocated to store /// it. Then a specific method is called: the constructor / bar += 42; // We use the variable (reading/writing its content)
 // The function terminates. First a specific method is called: the destructor. Then the data region for ‘bar’ is freed Then } 119 Constructor x A constructor is a special method of a class, which is constructor invoked each time an object is created invoked Syntax: the constructor is a method with the exact same name as Syntax: the class the x There can be multiple constructors There depends on the way the variable is being declared, i.e. std::string foo(512, ‘\0)
; // calls a constructor taking an int and a char /// as arguments / x There can be no constructor in the C++ program A C++ compiler usually provides a default constructor, which C++ simply does nothings simply 120 Example struct Car { /// constructor / Car()
; Car()
; /// attributes / int uniqueId; int /// methods / void disp...
View Full Document

This note was uploaded on 04/03/2014 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Ask a homework question - tutors are online