Course list http://www.c-jump.com/bcc/

Lab 16: CDrawBox header and implementation file


  1. Description
  2. Copying Visual Studio Project
  3. Class declaration and implementation
  4. Refactoring the code
  5. Advice
  6. How to submit

Description



Copying Visual Studio Project



Class declaration and implementation


  • Class declaration:

    
    // person.h
    class Person {
        string name;
    public:
        Person();
        Person( string name );
        string name() const;
        void name( string name_ );
    }//class Person
    
    
  • Class implementation:

    
    // person.cpp
    #include "person.h"
    
    Person::Person()
    {
    }
    
    Person::Person( string name )
        : name( name )
    {
    }
    
    string Person::name() const
    {
        return name;
    }
    
    void Person::name( string name_ )
    {
        name = name_;
    }
    
    

Refactoring the code



Advice


  • A header  should never contain 

    • ordinary function definitions:
      int f( ) { return 123; }

    • data definitions:
      int x = 123;

  • A header may contain:

    • class definitions:
      class Person { /*...*/ };

    • function declarations:
      int f( );

    • constant data definitions:
      const float PI = 3.141593;

    • enumerations:
      enum { RED, GREEN, BLUE };


How to submit