Defining Classes in Separate Files in C++






This video tutorial explains how to create new files for classes and how to define classes in separate files and include it to the program in c++ programming.
You are gonna learn why we write classes in separate files, how to use the pre-processor to avoid multiple inclusion of the class definition, how to include the classes defined in separate files to your program in detail with example.

source code for this tutorial

code for the header file "Person.h"

#ifndef PERSON_H
#define PERSON_H


class Person
{
    public:
        Person();
        void whatYouGot();
    protected:
    private:
};

#endif // PERSON_H

 
code for "Person.cpp"

#include "Person.h"
#include <iostream>

using namespace std;
Person::Person()
{
    cout << "Person class constructor"<<endl;
}

void Person :: whatYouGot(){
cout << "i got a million"<<endl;
}


code for "main.cpp"

#include <iostream>
#include "Person.h"
using namespace std;

int main()
{
    Person anil;
    anil.whatYouGot();
    return 0;
}