CFORBEGINNERS.COM


C++ Classes 2

Using functions within classes is neccesary to add any functionality to objects. Many times we only want functions within a class to manipulate variables of that same class. In doing this, it allows a program to keep more members private. Here is the same Employee class from the previous c++ lesson with an added member function.

//Employee.h
class Employee
{
private:
  string name;
  int age;
  double salary;
  
public:
  Employee();
  void setMembers();
  void printMember();
  
};

We have made the setMembers and printMembers functions public so that objects of the Employee class that are within an outside function can use the functions

Organizing .h and .cpp files

Seperating a class between a .h file and a .cpp file is common practice for organization. A class is made up of two parts, the interface and the implementation. The interface is where the data members, functions, constructors, and destructor are declared.

//interface of Employee class

#pragma once

#include <iostream>
#include <string>
using namespace std;

class Employee
{
private:
  string name;
  int age;
  double salary;
  
public:
  Employee();
  void setMembers();
  void printMembers();
};

The implementation is of a class is where the functions, constructor, and destructor are defined.

//Employee.h
//implementation of Employee class
Employee::Employee()
{
//code goes here
}

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

The interface of a class goes into the .h file and the implementation goes into the .cpp file. This new way of organizing code calls for a new operator, the scope operator "::" The scope operator is needed whenever a function, constructor, or destructor is being defined outside of it class. The general syntax for defining a function, constructor, or destructor outside of its class is.

return type class_name::function_name (parameter(s))

The interface has already been established for the Employee class, now lets take a look at the implementation.

Employee.cpp
//implementation of Employee class

#include "Employee.h"include the interface

Employee::Employee()
  {
//same constructor in last lesson
   name = "";
   age = 0;
   salary = 0;
  }

void Employee::setMembers()
{
//declare local variables
	string empl_name;
	int empl_age;
	double empl_sal;

//accept user input
	cout<<"Please enter a name, age, and salary"<<endl;
	cin>>empl_name;
	cin>>empl_age;
	cin>>empl_sal;

//initialize object member variables
	name = empl_name;
	age = empl_age;
	salary = empl_sal;
}

void Employee::printMembers()
{
//print out object members
	cout<<name<<endl;
	cout<<age<<endl;
	cout<<salary<<endl;
}
//main.cpp

#include "Employee.h"

int main()
{
//declare three variables of Employee type
	Employee emp1,emp2,emp3;

//call setMembers function for each object
	emp1.setMembers();
	emp2.setMembers();
	emp3.setMembers();

//call printMembers function for each object
	emp1.printMembers();
	emp2.printMembers();
	emp3.printMembers();

system ("pause");
return 0;
}

How It Works

The program creates three employees, through the setMembers function the user inputs data that is then assigned to each employee (object) which is then printed out using the printMembers function. First let us take a better look at the setMembers function where most of the action takes place.

void Employee::setMembers()
{
//declare local variables
	string empl_name;
	int empl_age;
	double empl_sal;

//accept user input
	cout<<"Please enter a name, age, and salary"<<endl;
	cin>>empl_name;
	cin>>empl_age;
	cin>>empl_sal;

//initialize object member variables
	name = empl_name;
	age = empl_age;
	salary = empl_sal;
}

The setMembers function is called by main which allows access because setMembers is a public function. First local variables are declared to hold the values that the user inputs. Once all the data has been inputed the current objects members (name,age,salary) are initialized using the local variables. This function is called three individual times for each of the three objects. Now that each objects members have values we want to be able to print out the data, this is done with the printMembers function.

void Employee::printMembers()
{
//print out object members
	cout<<name<<endl;
	cout<<age<<endl;
	cout<<salary<<endl;
}

Like the setMembers function the printMembers function is called three times by main for each of three objects. The printMembers function prints out each current objects data members.

Notice how all the data members were declared private and thus only accessed by the Employee class member functions. By declaring setMembers and printMembers public main was able to access the functions which in turn accessed the data members. In other words, main accessed Employees private members indirectly through a public function. This is a common technique used in object oriented programming.