[program-l] splitting C++ class in to header file and CPP file

  • From: Adil Shaikh <ah.shaikh97@xxxxxxxxx>
  • To: "program-l@xxxxxxxxxxxxx" <program-l@xxxxxxxxxxxxx>
  • Date: Sat, 8 Dec 2018 13:55:50 +0530

hello, 
I have been trying hard to get this code compiled, but I had no lluck. I’m new 
to C++, so, I have no idea how to go about it. 

First, I created a C++ source file which contained everything, my class and 
main function. All was good. 
But when I tried to separate the class into header file and actual 
implementation of class into CPP source file. 
I just can’t compile it. 

I’m pasting my source code below:

Person.h
//headder file for person class
#pragma once

class person
{
    private:
string pName;
int pAge;

    public:
    person(string name, int age): pName(name), pAge(age) {}
    
    
    
void displayName();
    void displayAge();
};

Person.cpp
        //implimentation of person  class 
#include "person.h"
#include <iostream>

using namespace std;

void person::displayName()
{
    cout <<"my name is " << pName<<endl;
}

void person::displayAge ()
{
        cout <<"I am "<<pAge<<endl;
}

Testperson.cpp
#include "person.h"
#include <iostream>

using namespace std;



int main()
{
    person p1("adil", 21), p2("akash", 22), p3("dhaval", 25);
    
    person pList [3] = {p1, p2, p3};

    pList[0].displayName();
    pList[0].displayAge();
pList[1].displayName();
pList[1].displayAge();
pList[2].displayName();
pList[2].displayAge();
    
    return 0; 
}

I’m using G++ compiler.
Please help me out, I’m tired.

Other related posts: