You are on page 1of 6

Object Oriented Paradigm

Separate header and cpp


Shafiq Ahmed
Separate Methods and their implementation

 We also separate methods and their implementation to achieve abstraction.


 For this purpose we separate function prototypes in a file called header file.
 And separate their implementation in a file called .cpp file.
3 Files
 Header file (.h file) of class
 It will contain the class structure i.e. attributes and prototypes of functions

 Source file (.cpp file) of class


 It will contain the class functions implementation.
 We include class header file on top of this file to connect class header and source file
 We use scope resolution operator (::) to connect class name class functions.
 Source file(.cpp) of main
 You can have an other file which can contain your main function (which is entry point of code)
 We include class header file on top of this file to connect class header and source file
Header file of Employee Class
#include <iostream>
using namespace std;

class Employee
{
int age;
char * name;

public:

Employee(); // default const


Employee(int a, char * n); // parm const
Employee(const Employee & obj); // copy const

void set_age(int x);


void set_name(char * n);

int get_age();
char * get_name();

void display();

//override dest
~Employee();
Source file of Employee class
#include "Employee.h"

Employee::Employee()
{
name = nullptr;
}
Employee::Employee(int a, char * n) Employee::~Employee()
{ {
set_age(a); if (name != nullptr)
set_name(n);
{ delete[] name;
}
}
void Employee::set_age(int x) }
{
if (x > 0)
age = x;
else
{
cout << "You enter wrong age \n";
}
}
void Employee::set_name(char * n)
{
if (n != nullptr)
{
int s = strlen(n);

name = new char[s];

for (int i = 0; i < s; i++)


{
name[i] = n[i];

}
}
To understand this look copy
}

int Employee::get_age()
const slides
{
return age;
}
char * Employee::get_name()
{
return name;
}

void Employee::display()
{
cout << "Your employee information is: ";
cout << age << " " << name << endl;
}

Employee::Employee(const Employee & obj)


{
set_age(obj.age);
set_name(obj.name);
}
Source file of main
#include "Employee.h"

int main()
{
Employee e1; // default

Employee e2(12, "sdad"); // parm

if (e2.get_age() > 25)


{
cout << "You are adult " << endl;
}

You might also like