Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. The best way to create a c++ class
Forum Updated to NodeBB v4.3 + New Features

The best way to create a c++ class

Scheduled Pinned Locked Moved C++ Gurus
4 Posts 3 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    landing kiss
    wrote on last edited by
    #1

    hi everybody,
    to create a c++ class i can do the following

    1. first way:

    Personnage.cpp
    @
    #include <iostream>
    using namespace std;
    class Personnage
    {
    public:
    Personnage();
    ~Personnage();
    int getVie();
    protected:
    private:
    int vie;
    };
    @
    in the file "main.cpp"
    i can write
    @
    #include <iostream>
    #include "Personnage.cpp"

    using namespace std;
    int main()
    {

    cout << "Hello world!" << endl;
    Personnage david;
    return 0;
    

    }
    @

    1. But it is also possible
      to create a header file that i will include in the main like this :

    Personnage.h
    @
    #ifndef PERSONNAGE_H
    #define PERSONNAGE_H

    class Personnage
    {
    public:
    Personnage();
    ~Personnage();
    int getVie();
    protected:
    private:
    int vie;
    };

    #endif
    @
    Personnage.cpp
    @
    #include<iostream>
    #include<string>
    #include "Personnage.h"

    using namespace std ;

    Personnage::Personnage():vie(2)
    {
    //

    }

    Personnage::~Personnage()
    {

    }
    int Personnage::getVie(){
    return vie ;
    }
    @

    and finally in the main.cpp
    @
    #include <iostream>
    #include "Personnage.h"

    using namespace std;
    /** \brief
    *
    * \param
    * \param
    * \return
    *
    */
    int main()
    {

    cout << "Hello world!" << endl;
    Personnage david;
    //cout<<david.getVie()<<endl;
    return 0;
    

    }
    @
    so i want to know what is the best way to create a c++ class is it good to include directly a sourcefile.cpp which contains the definition of the class ? thanks

    Edit: please use @ tags around code sections; Andre

    kobena

    1 Reply Last reply
    0
    • L Offline
      L Offline
      leon.anavi
      wrote on last edited by
      #2

      [quote author="landing kiss" date="1360936159"]
      so i want to know what is the best way to create a c++ class is it good to include directly a sourcefile.cpp which contains the definition of the class ? thanks
      [/quote]

      It is better to split source code into several files and to rely on header files. It is also recommended to use #include macro guards to make sure that the file was been include only once (aka to avoid double or multiple inclusion).

      http://anavi.org/

      1 Reply Last reply
      0
      • I Offline
        I Offline
        issam
        wrote on last edited by
        #3

        Hi,
        I have an example explaining the right way to create a C++ program.

        The source code can be downloaded here :

        "developpez.com":http://cpp.developpez.com/telecharger/detail/id/2955/Liste-de-Personnes

        :)

        http://www.iissam.com/

        1 Reply Last reply
        0
        • L Offline
          L Offline
          landing kiss
          wrote on last edited by
          #4

          thanks ! effectively it is better to avoid several inclusion

          kobena

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved