OOP

Hi, I have just started in OOP and I need help in certain area

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# include "header.h"

class Person 
{
private:
	char *fname ;
	char *lname ;
	float salary ;
public :
	Person() ;
	Person(char *fn,char *ln, float sal) ;
	~Person() ;
	void display_all() ;	
	float& set_salary() ;
	void display_salary() ;
	void set_salary(float sal) ;

};


Person::Person()
{
	fname = " " ;
	lname = " " ;
	salary = 0 ;
}

Person :: Person(char *fn,char *ln, float sal)
{
	fname = fn ;
	lname = ln ;
	salary = sal ;
}

Person ::~Person()
{
	fname = NULL ;
	lname = NULL ;
	delete fname ;
	delete lname ;
}

void Person::display_all() 
{
	cout << "First Name : " 
		 << fname 
		 <<"\nLast Name : "
		 << lname 
		 <<"\nSalary : "
		 << salary ;

}

void Person::set_salary(float sal)
{
	salary = sal ;
}

void display_salary()
{
	cout << "\nSalary : " ;
}

float& Person::set_salary () 
{
	
}


int main ()
{
	Person p("Lau","Tong Hing", (float)785.56);
	p.display_all() ;
	p.set_salary((float)875.65) ;
	p.display_salary();
	p.set_salary((float)895.95) ;
	p.display_salary() ;
	system("pause") ;
	return 0 ;
}


I do not understand the below code is for.... Someone please enlighten me
1
2
3
4
float& Person::set_salary () 
{
	
}
your set_salary() member function is useless, nothing to be done inside the function.. and it will generate an error for not returning a value

read more http://cplusplus.com/doc/tutorial/classes/
This is unrelated to your problem.. but...

char pointers are not strings.

You should really use std::string for strings until you understand more about pointers and memory management. Things you're doing wrong:

1) don't delete something unless you allocated it with new
2) arrays (as in char arrays) would generally be deleted with delete[], not delete.
3) Don't set values to NULL before deleting them, since that makes it impossible to delete them.

But anyway... yeah... use std::string and you don't have to worry about any of that.

------------------------------------------

As for your actual question:

That is a rather strange function. Are you sure it's not supposed to be float Person::get_salary()? In that case, it'd just return salary;
Sorry for the late reply....

Thanks Disch for pointing out my mistakes.

-------------------------------------------------------

This function,

1
2
3
4
float& Person::set_salary () 
{
	
}


is given by my teacher and i have no idea what is it for. Perhaps is a transcription error made by my teacher, I will go clarify with him again.

Last edited on
I have clarified with him....

The function is just another way to set the salary in the private by using the address
Wtf? That is the most useless function I have ever seen, it defeats the entire point of having a private variable by letting you modify it however you want. If you are going to have a function like that, just make it public.
closed account (S6k9GNh0)
firedraco, it's part of the rule of abstraction. >.>
Although in this case, I can relate.
Terminal case of "hurr durr don't access members directly".
There was just a thread about this:
http://www.cplusplus.com/forum/general/19240/
Topic archived. No new replies allowed.