0% found this document useful (0 votes)
116 views4 pages

POO Handout - Lab 4 PDF

This document provides an explanation of constructors and destructors in C++. It presents sample code for a Student class with various constructor and destructor implementations. For each code example, it asks the expected and actual results, as well as reasons for errors. It also asks how many times the destructor would be called at the end of sample programs using the Student class.

Uploaded by

Raluca Teodora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views4 pages

POO Handout - Lab 4 PDF

This document provides an explanation of constructors and destructors in C++. It presents sample code for a Student class with various constructor and destructor implementations. For each code example, it asks the expected and actual results, as well as reasons for errors. It also asks how many times the destructor would be called at the end of sample programs using the Student class.

Uploaded by

Raluca Teodora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

POO Handout Lab 4

First Name: _____________________________ Last Name: ______________________________

C++ Lab 4 Constructors and destructors

Considering the next class:


class Student{
private:
char* name;
char address[50];
unsigned int age;
public:
void print(){
cout<<"Name: "<<this->name<<endl;
cout<<"Adress: "<<this->address<<endl;
cout<<"Age: "<<this->age<<endl;
}
};
Analyze the next lines:

Expected value/error Actual result/error Reason for having an error


Student s1;

s1.print();

Student* s2 = new Student();

s2->print();

If the class is updated to:


class Student{
private:
char* name;
char address[50];
unsigned int age;
public:
Student(unsigned int v_age){
this->age=v_age;
}
void print(){
cout<<"Name: "<<this->name<<endl;
cout<<"Adress: "<<this->address<<endl;
cout<<"Age: "<<this->age<<endl;
}
};

www.acs.ase.ro/cpp Copyright 2017 [email protected]


POO Handout Lab 4

Analyze the next lines:

Expected value/error Actual result/error Reason for having an error


Student s1;

Student* s2 = new Student();

Student s3(21);

s3.print();

Student* s4 = new Student(22);

s4->print();

If the class is updated to:


class Student{
private:
char* name;
char address[50];
unsigned int age;
public:
Student(){
this->name = new char[strlen("Missing")+1];
strcpy(this->name, "Missing");
strcpy(this->address, "Missing");
this->age=22;
}
Student(char* v_name, char* v_address, unsigned int v_age){
this->name = new char[strlen(v_name)+1];
strcpy(this->name, v_name);
strcpy(this->address, v_address);
this->age=v_age;
}
Student(char* v_name, unsigned int v_age=22):age(v_age){
this->name = new char[strlen(v_name)+1];
strcpy(this->name, v_name);
strcpy(this->address,"Missing");
this->age=v_age;
}

void print(){
cout<<"Name: "<<this->name<<endl;
cout<<"Adress: "<<this->address<<endl;
cout<<"Age: "<<this->age<<endl;
}
~Student(){

www.acs.ase.ro/cpp Copyright 2017 [email protected]


POO Handout Lab 4

if(this->name!=NULL){
delete[] this->name;
}
}
};

Analyze the next lines:

Expected value/error Actual result/error Reason for having an


error
Student s1;
s1.print();

Student* s2 = new Student();


s2->print();

Student s3("Ion
Popescu","Romana, No 7",23);
s3.print();

Student* s4 = new Student("Mihai


Ionescu","Victoriei, No 4",
20);
s4->print();

Student s5("Ion Ion", 18);


s5.print();

Student s6("Ion Ionel");


s6.print();

How many times is the destructor called at the end of the above program?

If the class is updated to:


class Student{
private:
char* name;
char address[50];
unsigned int age;
public:
Student(){
this->name = new char[strlen("Missing")+1];
strcpy(this->name, "Missing");
strcpy(this->address, "Missing");
this->age=22;
}
Student(unsigned int v_age, char* v_name, char* v_address){
this->age=v_age;
this->name = new char[strlen(v_name)+1];
www.acs.ase.ro/cpp Copyright 2017 [email protected]
POO Handout Lab 4

strcpy(this->name, v_name);
strcpy(this->address, v_address);
}

Student(char* v_name, char* v_address, unsigned int v_age=22):age(v_age){


this->name = new char[strlen(v_name)+1];
strcpy(this->name, v_name);
strcpy(this->address, v_address);
this->age=v_age;
}

void print(){
cout<<"Name: "<<this->name<<endl;
cout<<"Adress: "<<this->address<<endl;
cout<<"Age: "<<this->age<<endl;
}
~Student(){
if(this->name!=NULL){
delete[] this->name;
}
}
};

Analyze the next lines:

Expected value/error Actual result/error Reason for having an


error
Student s1(21, "Ion
Popescu","Romana, No 7");
s1.print();
Student* s2 = new Student(20,
"Mihai Ionescu","Victoriei, No
4");
s2->print();
delete s2;
Student s3("Ion Ion","Mihail
Moxa, 11");
s3.print();
Student* s4 = new
Student("Blank","Missing",19);
s4->print();
delete s4;

How many times is the destructor called at the end of the above program?

www.acs.ase.ro/cpp Copyright 2017 [email protected]

You might also like