# Q: Class + pointer

• 06-10-2010
princess
Q: Class + pointer
Hello,
I have tried to solve an exercise, because i want to make sure if i've understand what i've learned.
I hope that you can let me know if thier is any mistakes, and i will try to solve it.

Question 1
Consider the following definition of the class Employee:
Code:

```class Employee { private: string * firstName; string * lastName; static int empCount; public: Employee(string first, string last); ~Employee(); string getFirstName(); static int getEmpCount(); static int counting; };```
PART (A)
Write the definition of the constructor function Employee(string first, string
last); which dynamically allocates memory for firstName and lastName and copies first
and last to them. The function should increment empCount by 1. Use deep copy.
Code:

```Employee :: Employee(string first, string last) { first=new string; last=new string; first=*firstname; last=*lastname; empCount++; }```
PART (B)
Write the definition of the function ~Employee() which deallocates the dynamic memory and
decrease the value of empCount by 1.
Code:

```Employee :: ~ Employee() { delete firstname; delete lastname; empCount--; }```
PART (C)
Write the definition the function getEmpCount() which returns the value of empCount.
Code:

```int Employee:: getempCount() { return empCount; }```
PART (D)
In the main function add statements to do the following:
· Using the following declarations: string first=”Hilary”, last=”Sam”, create an object of
type class Employee named xEmp.
Code:

`Employee xEmp("Hilary","Sam");`
· Increment the value of counting in xEmp by 1.
Code:

`xEmp.counting++;`
· Print the value of empCount.
Code:

`cout<<xEmp.getempCount;`
• 06-10-2010
Elysia
Well, Part B,C are definitely correct.
I am concerned about part D2) Increment the value of counting. If it's just supposed to increment the variable counting inside the class, then it's right.
D3 isn't right since you've misspelled GetEmpCount.

Btw, your indentation could use some work.
• 06-10-2010
Sebastiani
Code:

```Employee :: Employee(string first, string last) { first=new string; last=new string; first=*firstname; last=*lastname; empCount++; }```
You've mixed up your class members with the parameters there.

Also, someone should point out to your teacher that those parameters should be const references rather than temporaries, and that using new/delete without RAII is just asking for trouble...;)
• 06-10-2010
princess
thank you very much for your help
• 06-11-2010
princess
Hi again,

what about D1, is it right ?

part A
Code:

```Employee :: Employee(const string &first, const string &last) { first=new string; last=new string; firstname=first.firstname; lastname=last.lastname; empCount++; }```
so, is it right now ?!
• 06-11-2010
Programmer_P
Quote:

Originally Posted by princess
Hi again,

what about D1, is it right ?

part A
Code:

```Employee :: Employee(const string &first, const string &last) { first=new string; last=new string; firstname=first.firstname; lastname=last.lastname; empCount++; }```
so, is it right now ?!

No...
You need to change it to:
Code:

```Employee :: Employee(const string &first, const string &last) { firstname=new string; lastname=new string; *firstname = first; *lastname=last; empCount = 0; empCount++; }```
• 06-11-2010
princess
thank you very much

`Employee xEmp("Hilary","Sam");`