C++ Class

  • Define a class

    class CBox
    {
    // public data members
    public:
      double m_Length;
      double m_Breadth;
      double m_Height;//Constructor
    CBox(double lv = 1.0, double bv = 1.0, double hv = 1.0)
    {
    m_Length = lv;
    m_Breadth = bv;
    m_Height = hv;
    }// Same as above
    CBox(double lv = 1.0, double bv = 1.0, double hv = 1.0) : m_Length(lv), m_Breadth(bv), m_Height(hv)
    {
    }// member function
    double Volume()
    {
      return m_Length * m_Breadth * m_Height;
    }
    }; // Don't forget this semicolon
  • Access modifiers
    • public
    • protected
    • private //Default
    • static
  • Constant object of class
    • No changes allowed to the object after instantiation
    • Examle 
      • const CBox standardBox(3.0,5.0,8.0);
    • Costant object can only access constant member function
      • int Compare(CBox box) const {…};
    • Should declare all member functions that do not alter the class object as const
    • Methods with same signatures except const are consider two different methods
  • Instantiate class
    • CBox box1;
    • CBox box2;
    • CBox box3(10.0, 20.0, 30.0);
  • Access member data
    • box1.m_Length;
  • Access member functions
    • box1.Volume();
  • Pointer to class
    • CBox box1(1.0, 2.0, 3.0);
    • CBox* pBox1 = &box1;
    • pBox1->Volume();
  • Class reference
    • Complex class object should be pass as reference to avoid copying
    • CBox(const CBox& initB);
  • Destructor
    • ~CBox();
    • Always implement a copy constructor if you dynamically allocate memory for class members

        // Copy Constructor definition
        CMessage(const CMessage& initM)
        {
          // Allocate space for text
          pmessage = new char[ strlen(initM.pmessage) + 1 ];
          // Copy text to new memory
          strcpy(pmessage, initM.pmessage);
        }
  • Exercise
    • Create a new project
      • File -> New -> Projects -> Win32 Console Application
    • Create a new class 
      • In Class View
        • Right click Classes -> New Class -> MyClass
      • Two files are created
        • MyClass.cpp
          • Contains class implementations
        • MyClass.h
          • Contains class definition
    • Add data members
    • Add inline function members (short functions)
    • Add other function members
      • Add function prototype to header file
      • Add function implementation to cpp file
    • Add global functions to support MyClass operations
      • File -> New -> C++ Source File -> MyClassOperations.cpp
    • Add new header file for MyClassOperations.cpp
      • File -> New -> C++ Header File -> MyClassOperations.h
    • Use MyClass in main() function
      • #include MyClass.h
      • #include MyClassOperations.h
This entry was posted in cpp. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

This site uses Akismet to reduce spam. Learn how your comment data is processed.