[go: up one dir, main page]

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

Lab 08

1. The document discusses different types of methods that can be used to implement behaviors in classes including constructors, queries, updates, and destructors. 2. It provides exercises to demonstrate constructors, destructors, and static/const attributes and methods using C++ classes. Students are tasked with rewriting code snippets to utilize constructor initializer lists, copy constructors, destructors, and resolving errors in static/const implementations.

Uploaded by

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

Lab 08

1. The document discusses different types of methods that can be used to implement behaviors in classes including constructors, queries, updates, and destructors. 2. It provides exercises to demonstrate constructors, destructors, and static/const attributes and methods using C++ classes. Students are tasked with rewriting code snippets to utilize constructor initializer lists, copy constructors, destructors, and resolving errors in static/const implementations.

Uploaded by

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

Lab 08: Defining Classes (Pt2)

Objectives
 To expose students to different type of methods that can be used to implement behaviors:
 Four basic type of behaviors:
1. Constructors
2. Queries
3. Updates
4. Destructors
 Class Objects: C++ features
1. const Attributes, static Attributes, and static const Attributes
2. const Methods and static Methods

Lecture review and exercises

a) What is a default constructor?

b) What is the purpose of a constructor?

c) Is it mandatory to declare/define constructor in a class?

d) What happens if you do not provide any constructor?

e) Can constructor return a value?

f) What is the advantage of using a constructor initializer list?

g) What is the general syntax of constructor initializer list?

h) What is the type of the parameter a copy constructor has?

i) What does a destructor do?


Exercise 1: Constructors
Compile and run the following program. Study how the program works.

#include <iostream>
using namespace std;

class Employee {
string name;
double salary;
public:
Employee (); // default constructor
Employee (string name, double salary = 0); // overloaded constructor
void print() const;
};

int main() {
Employee e1; // call default constructor
Employee e2 ("Kelly"); // call overloaded constructor,
// use default argument for salary
Employee e3 ("Michael", 2000); // call overloaded constructor
Employee e4 = e2; // call copy constructor
Employee e5 (e3); // call copy constructor

e1.print();
e2.print();
e3.print();
e4.print(); // same output as e2
e5.print(); // same output as e3
return 0;
}

Employee::Employee() {
name = "[No name]";
salary = 0;
}

Employee::Employee (string name, double salary) {


this->name = name;
this->salary = salary;
}

void Employee::print() const {


cout << "Name = " << name
<< "\nSalary = " << salary << endl;
}

Perform the following tasks:


1. Rewrite the 2 constructors using constructor initializer list.
2. You can combine the 2 constructors (default constructor and overloaded constructor) into
just one constructor only. Re-write the class declaration and implementation for that.
Exercise 2: Destructor

Compile and run the following program. Study how the program works.

#include <iostream>
using namespace std;

const int SIZE = 3;

class Student {
int* marks; // dynamic attribute
public:
Student();
void print();
void setMark (int location, int newmark);
};
Student::Student () { // default constructor
marks = new int[SIZE];
marks[0] = 97;
marks[1] = 98;
marks[2] = 99;
}
void Student::print() {
cout << "Marks = ";
for (int i = 0; i < SIZE; i++)
cout << marks[i] << " ";
cout << endl;
}
void Student::setMark (int location, int newmark) {
marks[location] = newmark;
}
int main() {
Student s1;
Student s2 = s1;
s1.print(); // Output 97 98 99
s2.print(); // Output 97 98 99, looks okay
s1.setMark (0, 1);// set marks[0] = 1
s1.print(); // Output 1 98 99
s2.print(); // Output 1 98 99. Why?
return 0;
}

Answer the following questions:


a) The reason that s2 is also 1 98 99 is because the marks pointer in both s1 and s2 actually point to
the same dynamic array. They share the same dynamic array which is not wanted in this case
(each student should have its own marks). Explain why they end up sharing the same dynamic
array?
b) The problem does not exist if the marks array is an automatically allocated array instead of
dynamic array? Try it.
c) The solution if marks must remain as a dynamic array is to define a copy constructor and use
deep copy to copy the array. Write the code.
d) The print method should be declared as const method. Why? Write the code.
e) Since Student class has a dynamic array as its attribute, we should write a destructor to delete the
dynamic array when the object is destroyed. Write the code. cout a message in the destructor so
that we know that the destructor is indeed called.
Exercise 3: const/static attributes, and const/static methods

The following program has errors. The main function and the attribute declaration have nothing
wrong. Correct the methods and attribute initialization.

#include <iostream>
using namespace std;

class Employee {
public:
Employee (string name) { // overloaded constructor
name = name;
numEmployees++;
}

~Employee() { numEmployees--; } // destructor

void print() {
cout << "Name = " << name << endl
<< "Company = " << company << endl;
}
int getNumEmployees() {
return numEmployees;
}
private:
const string name; // const attribute
static int numEmployees; // static attribute
static const string company; // static const attribute
};

int main() {
cout << "Count: " << Employee::getNumEmployees() << endl;
{
const Employee e1 ("Michael");
cout << "Count: " << e1.getNumEmployees() << endl;
e1.print();
Employee e2 ("Kelly");
cout << "Count: " << e2.getNumEmployees() << endl;
e2.print();
}
cout << "Count: " << Employee::getNumEmployees() << endl;

return 0;
}

Output:
Count: 0
Count: 1
Name = Michael
Company = XYZ
Count: 2
Name = Kelly
Company = XYZ
Count: 0

You might also like