Reading Assignment
--------------------------------------------------------------------------------------------------------------------
class with a parameterized constructor
--------------------------------------------------------------------------------------------------------------------
class vehicle
{
public:
double displacment;
int doors;
bool automatic_transmission;
std::string make;
std::string model;
// the constructor has NO return type at all , the name is ALWAYS the same as the class name
// your constructor can have any number of parameters
// you can have multiple constructors
vehicle()
{
displacment =0;
doors =0;
automatic_transmission=false;
}
vehicle(double in_displacement,int in_doors, bool in_automatic_transmission)
{
displacement = in_displacement;
doors = in_doors;
automatic_transmission =in_automatic_transmission;
}
vehicle(double in_displacement,int in_doors, bool in_automatic_transmission, std::string in_make , std::string in_model)
{
displacement = in_displacement;
doors = in_doors;
automatic_transmission =in_automatic_transmission;
make = in_make;
model = in_model;
}
}; // end of class
int main()
{
vehicle vh1; // uses the default constructor
vehicle vh2(2000,2,true); // use the constructor that has 3 parameters
vehicle vh2(2000,2,false,"Toyota","Celica"); // use the constructor that has 5 parameters
}
given the above
There is a complete and functional defenition for the class vehicle as well as a main that creates an instance of that class.
Any time an instance of the class vehicle is created the code of one of the three constructors will be ran.
--------------------------------------------------------------------------------------------------------------------
destructors
--------------------------------------------------------------------------------------------------------------------
a destructor is the opposite of a constructor, it runs when an instance is destroyed.
class student_list
{
private string * data;
int size;
student_list( int in_size = 10)
{
size = in_size;
data = new string[in_size];
}
};
int main()
{
student_list mylist; // mylist.data is set to a dynamic array of size 10
student_list mylist2(38); mylist2.data is set to a dynamic array of size 38
}
in this senario the arrays do not get cleaned up. We dynamically allocate memory in the constructor with the new command.
but we dont have anything to clean it up. we need some code that runs automatically when the objects are destroyed
class student_list
{
private string * data;
int size;
student_list( int in_size = 10)
{
size = in_size;
data = new string[in_size];
}
// here is a destructor it has no parameters and no return type
// it is always named the same as the class with a ~ preceeding it
// here we would clean up anything we created with new in the constructor
~student_list()
{
delete[] data;
}
};
int main()
{
student_list mylist; // mylist.data is set to a dynamic array of size 10
student_list mylist2(38); mylist2.data is set to a dynamic array of size 38
}