クラスファイル -- 宣言部(ヘッダファイル)と実装部の分離

戻る
::::::::::::::
Makefile
::::::::::::::
# $Id: c++_header_file.html,v 1.1 2009/06/22 16:12:06 kishi Exp kishi $

CFLAGS = -c -Wall -O2
OBJECT = MyGraphTest.o MyGraph.o
EXE = MyGraphTest.exe

$(EXE): $(OBJECT)
	g++ $(OBJECT) -o $(EXE) && strip $(EXE)

MyGraphTest.o:  MyGraphTest.cpp MyGraph.h
	g++ $(CFLAGS) MyGraphTest.cpp

MyGraph.o:  MyGraph.cpp MyGraph.h
	g++ $(CFLAGS) MyGraph.cpp

clean:
	rm -f *.o *.exe *~ *.bak *.orig

indent:
	astyle -a -c -P *.cpp *.h

update:
	vu.sh *.h *.cpp
	

::::::::::::::
MyGraph.h
::::::::::::::
/**
* $Id: c++_header_file.html,v 1.1 2009/06/22 16:12:06 kishi Exp kishi $
*/
#include <iostream>
using namespace std;

class MyGraph {
    string name;
public:
    // constructor
    MyGraph ( string x ) : name ( x ) {}

    // destructor
    ~MyGraph () {}

    // methods
    void setName ( string _name );
    string getName ();

};
::::::::::::::
MyGraph.cpp
::::::::::::::
/**
* $Id: c++_header_file.html,v 1.1 2009/06/22 16:12:06 kishi Exp kishi $
*/

#include "MyGraph.h"

#include <iostream>
using namespace std;

void MyGraph::setName( string _name ) {
    name = _name;
}

string MyGraph::getName( void ) {
    return name;
}
::::::::::::::
MyGraphTest.cpp
::::::::::::::
/* $Id: c++_header_file.html,v 1.1 2009/06/22 16:12:06 kishi Exp kishi $ */

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

using namespace std;

int
main () {

    MyGraph mg1 ( "Mick" );
    MyGraph mg2 ( "Keith" );

    cout << mg1.getName() << endl;
    cout << mg2.getName() << endl;

    return 0;
}

戻る

inserted by FC2 system