Classes

Can someone give me a layout of how to go about coding each functions please?

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
#ifndef CCC_EMPL_H
#define CCC_EMPL_H

#include <string>

using namespace std;

/**
   A basic employee class that is used in many examples
   in the book "Computing Concepts with C++ Essentials"
*/
class Employee
{
public:
   /**
      Constructs an employee with empty name and no salary.
   */
   Employee();
   /**
      Constructs an employee with a given name and salary.
      @param employee_name the employee name
      @param initial_salary the initial salary
   */
   Employee(string employee_name, double initial_salary);
   /**
      Sets the salary of this employee.
      @param new_salary the new salary value
   */
   void set_salary(double new_salary);
   /**
      Gets the salary of this employee.
      @return the current salary
   */
   double get_salary() const;
   /**
      Gets the name of this employee.
      @return the employee name
   */
   string get_name() const;
private:
   string name;
   double salary;
};

#endif 


//this is other header function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef MANAGER_H
#define MANAGER_H

#include <string>
#include "ccc_empl.h"

using namespace std;

class Manager : public Employee
{
public:
   Manager(string name, double salary, string dept);
   string get_department() const;
   void print() const;
private:
   string department;
};

#endif 


// and this is my main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

#include "manager.h"

int main()
{  
   Manager harry("Hacker, Harry", 45000.00, "general");

   double new_salary = harry.get_salary() + 3000;
   harry.set_salary(new_salary);
   harry.print();

   return 0;
}
closed account (zwA4jE8b)
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
#ifndef CCC_EMPL_H
#define CCC_EMPL_H

#include <string>

using namespace std;

/**
   A basic employee class that is used in many examples
   in the book "Computing Concepts with C++ Essentials"
*/
class Employee
{
public:
   /**
      Constructs an employee with empty name and no salary.
   */
   Employee();
   /**
      Constructs an employee with a given name and salary.
      @param employee_name the employee name
      @param initial_salary the initial salary
   */
   Employee(string employee_name, double initial_salary);
   /**
      Sets the salary of this employee.
      @param new_salary the new salary value
   */
   void set_salary(double new_salary);
   /**
      Gets the salary of this employee.
      @return the current salary
   */
   double get_salary() const;
   /**
      Gets the name of this employee.
      @return the employee name
   */
   string get_name() const;
private:
   string name;
   double salary;
};

Employee::Employee()
{
//code goes here
}

Employee::Employee(string employee_name, double initial_salary)
{
//code goes here
}

void Employee::set_salary(double new_salary)
{
//code goes here
}

//Ando so on.
#endif  


method implementation is as such

return type - classname::function

The private variables in the class are the ones your going to set from the parameters
Last edited on
closed account (zwA4jE8b)
so ill let you figure out where it goes but you will be using the code

1
2
name = employee_name
salary = initial_salary


that is very similar to one in another function

that is just for the employee class.

It is up to you to write the manager class methods.
Last edited on
Is all of that suppose to go above main?
I am getting a lot of errors I don't know where to put the functions or how to declare them...

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
#include <iostream>

using namespace std;

#include "manager.h"

class Manager
{
	string employee_name;
	double initial_salary;

	Manager::Manager(string name, double salary, string dept)
	{

	}
	Manager::getName() 
	{
		return Name;
	}
	Manager::getSalary() 
	{
		return Salary;
	}
	Manager::setSalary(double new_salary) 
	{
		this.salary = new_salary;
	}
	Manager::Employee(string employee_name, double initial_salary)
	{
		name = employee_name
		salary = initial_salary
	}
}

int main()
{  
   Manager harry("Hacker, Harry", 45000.00, "general");

   double new_salary = harry.get_salary() + 3000;
   harry.set_salary(new_salary);
   harry.print();

   return 0;
}

closed account (zwA4jE8b)
no, the header files are separate *.h files.

If you are using Visual studio, they are neatly arranged in the explorer.

I have only used visual studio so far, so I do not know how other IDE's look and handle files.

your main.cpp will look something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

#include "manager.h"


int main()
{  
   Manager harry("Hacker, Harry", 45000.00, "general");

   double new_salary = harry.get_salary() + 3000;
   harry.set_salary(new_salary);
   harry.print();

   cin.get();

   return 0;
}


ccc_empl.h and manager.h will be separate files.

you should look into getting a book on c++. I hear c++ primer is good I have 'C++ programming - program design including data structures'

Last edited on
I got this for my manager.cpp but I am getting errors for set_department and department.
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
#include<iostream>
using namespace std;
#include "manager.h"

Manager::Manager(string name, double salary, string dept)
:Employee(name, salary), department(dept)
{
}

string Manager::get_department() const
{
	return department;
}

string Manager::set_department(string dept)
{
	Manager::department = dept;
}

void Manager:: print() const
{
	cout << Employee::get_name() << endl;
	cout << Manager::get_department() << endl;
	cout << Employee::get_salary() << endl;
}

closed account (zwA4jE8b)
1
2
3
4
Manager::Manager(string name, double salary, string dept)
:Employee(name, salary), department(dept)
{
}


should be

1
2
3
4
5
Manager::Manager(string name, double salary, string dept)
:Employee(name, salary)
{
department = dept;
}



1
2
3
4
5
6
void Manager:: print() const
{
	cout << get_name() << endl;
	cout << department << endl;
	cout << get_salary() << endl;
}



you don't need to resolve the scope for the class you are already in.
1
2
3
4
string Manager::set_department(string dept)
{
	department = dept;
}


and like i said before it should be manager.h
Last edited on
closed account (zwA4jE8b)
http://cplusplus.com/doc/tutorial/
http://cplusplus.com/doc/tutorial/classes
Topic archived. No new replies allowed.