C + + advanced theory, class definition, inheritance

class Box   //Define class: the class includes methods
{
// public determines the access properties of class members. You can also specify that class members are private or protected
public: //The default is private, 

    double length;   // length
    double breadth;  // width
    double height;   // height
    // Member function declaration
    double get(void)
    {
        return length * breadth * height;
    }
    void set(double len, double bre, double hei)
    {
        length = len;
        breadth = bre;
        height = hei;
    }
};

Class member function Note the following wording, outside the class:

#include <iostream>
 
using namespace std;
 
class Box
{
   public:
      double length;         // length
      double breadth;        // width
      double height;         // height
 
      // Member function declaration
      double getVolume(void);
      void setLength( double len );
      void setBreadth( double bre );
      void setHeight( double hei );
};
 
// Member function definition
double Box::getVolume(void)
{
    return length * breadth * height;
}
 
void Box::setLength( double len )
{
    length = len;
}
 
void Box::setBreadth( double bre )
{
    breadth = bre;
}
 
void Box::setHeight( double hei )
{
    height = hei;
}
 
// Main function of program
int main( )
{
   Box Box1;                // Declare Box1, type Box
   Box Box2;                // Declare Box2, type Box
   double volume = 0.0;     // For storage volume
 
   // box 1 details
   Box1.setLength(6.0); 
   Box1.setBreadth(7.0); 
   Box1.setHeight(5.0);
 
   // box 2 details
   Box2.setLength(12.0); 
   Box2.setBreadth(13.0); 
   Box2.setHeight(10.0);
 
   // Volume of box 1
   volume = Box1.getVolume();
   cout << "Box1 Volume of:" << volume <<endl;
 
   // Volume of box 2
   volume = Box2.getVolume();
   cout << "Box2 Volume of:" << volume <<endl;
   return 0;
}

Constructor & destructor  : (you can also see how to copy constructors)

#include <iostream>
 
using namespace std;
 
class Line
{
   public:
      void setLength( double len );
      double getLength( void );
      Line();   // This is a constructor declaration
      ~Line();  // This is the destructor declaration
 
   private:
      double length;
};
 
// Member function definitions, including constructors
Line::Line(void)
{
    cout << "Object is being created" << endl;
}
Line::~Line(void)
{
    cout << "Object is being deleted" << endl;
}
 
void Line::setLength( double len )
{
    length = len;
}
 
double Line::getLength( void )
{
    return length;
}
// Main function of program
int main( )
{
   Line line;
 
   // Set length
   line.setLength(6.0); 
   cout << "Length of line : " << line.getLength() <<endl;
 
   return 0;
}

Friend function

The friend function of a class is defined outside the class, but has access to all private and protected members of the class. Although the prototype of a friend function has appeared in the definition of a class, a friend function is not a member function.

A friend can be a function, which is called a friend function; A friend can also be a class, which is called a friend class. In this case, the whole class and all its members are friends.

If you want to declare a function as a friend of a class, you need to use keywords before the function prototype in the class definition   Friend, as follows:

(experience the meaning with examples)

#include <iostream>
 
using namespace std;
 
class Box
{
   double width;
public:
   friend void printWidth( Box box );
   void setWidth( double wid );
};
 
// Member function definition
void Box::setWidth( double wid )
{
    width = wid;
}
 
// Note that printWidth() is not a member function of any class
void printWidth( Box box )
{
   /* Because printWidth() is a friend of Box, it can directly access any member of the class */
   cout << "Width of box : " << box.width <<endl;
}
 
// Main function of program
int main( )
{
   Box box;
 
   // Set width using member function
   box.setWidth(10.0);
   
   // Output width using friend function
   printWidth( box );
 
   return 0;
}

Inline function  : (basically useless)

Inline: the purpose of introducing inline functions is to solve the efficiency problem of function calls in the program. Let's say that when the program is compiled by the compiler, the compiler replaces the call expression of inline functions in the program with the function body of inline functions, while other functions are replaced at runtime. This is actually a space cost for time saving. Therefore, inline functions are generally small functions with lines 1-5. Be careful when using inline functions:

  • 1. Loop statements and switch statements are not allowed in inline functions;
  • 2. The definition of inline function must appear before the first call of inline function;
  • 3. The class in the class structure indicates that the internally defined function is an inline function.
#include <iostream>
 
using namespace std;

inline int Max(int x, int y)
{
   return (x > y)? x : y;
}

// Main function of program
int main( )
{

   cout << "Max (20,10): " << Max(20,10) << endl;
   cout << "Max (0,200): " << Max(0,200) << endl;
   cout << "Max (100,1010): " << Max(100,1010) << endl;
   return 0;
}

This pointer:

this   Pointers are implicit arguments to all member functions. Therefore, inside the member function, it can be used to point to the calling object.

Friend function has no   this   Pointer because the friend is not a member of the class. Only member functions have   this   Pointer.

#include <iostream>
 
using namespace std;
 
class Box
{
   public:
      // Constructor definition
      Box(double l=2.0, double b=2.0, double h=2.0)
      {
         cout <<"Constructor called." << endl;
         length = l;
         breadth = b;
         height = h;
      }
      double Volume()
      {
         return length * breadth * height;
      }
      int compare(Box box)
      {
         return this->Volume() > box.Volume();
      }
   private:
      double length;     // Length of a box
      double breadth;    // Breadth of a box
      double height;     // Height of a box
};
 
int main(void)
{
   Box Box1(3.3, 1.2, 1.5);    // Declare box1
   Box Box2(8.5, 6.0, 2.0);    // Declare box2
 
   if(Box1.compare(Box2))
   {
      cout << "Box2 is smaller than Box1" <<endl;
   }
   else
   {
      cout << "Box2 is equal to or larger than Box1" <<endl;
   }
   return 0;
}

Pointer to class:

A pointer to a C + + class is similar to a pointer to a structure. To access a member of a pointer to a class, you need to use the member access operator  ->, It's like accessing a pointer to a structure. Like all pointers, you must initialize the pointer before using it

#include <iostream>
 
using namespace std;

class Box
{
   public:
      // Constructor definition
      Box(double l=2.0, double b=2.0, double h=2.0)
      {
         cout <<"Constructor called." << endl;
         length = l;
         breadth = b;
         height = h;
      }
      double Volume()
      {
         return length * breadth * height;
      }
   private:
      double length;     // Length of a box
      double breadth;    // Breadth of a box
      double height;     // Height of a box
};

int main(void)
{
   Box Box1(3.3, 1.2, 1.5);    // Declare box1
   Box Box2(8.5, 6.0, 2.0);    // Declare box2
   Box *ptrBox;                // Declare pointer to a class.

   // Save the address of the first object
   ptrBox = &Box1;

   // Now try using the member access operator to access members
   cout << "Volume of Box1: " << ptrBox->Volume() << endl;

   // Save the address of the second object
   ptrBox = &Box2;

   // Now try using the member access operator to access members
   cout << "Volume of Box2: " << ptrBox->Volume() << endl;
  
   return 0;
}

Static member of class:

static   Keyword to define class members as static. When we declare that the members of a class are static, this means that no matter how many class objects are created, there is only one copy of the static members.

Static members are shared among all objects of the class. If there are no other initialization statements, all static data will be initialized to zero when the first object is created. We cannot place the initialization of static members in the class definition, but we can use range resolution operators outside the class  ::  To redeclare a static variable to initialize it, as shown in the following example.

If you declare a function member static, you can separate the function from any specific object of the class. Static member functions can be called even when the class object does not exist. Static functions only use the class name and range resolution operator  ::  You can access it.

Static member functions can only access static member data, other static member functions, and other functions outside the class.

Static member functions have a class scope, and they cannot access the class's this pointer. You can use static member functions to determine whether some objects of a class have been created.

#include <iostream>
 
using namespace std;
 
class Box
{
   public:
      static int objectCount;
      // Constructor definition
      Box(double l=2.0, double b=2.0, double h=2.0)
      {
         cout <<"Constructor called." << endl;
         length = l;
         breadth = b;
         height = h;
         // Increase by 1 each time you create an object
         objectCount++;
      }
      double Volume()
      {
         return length * breadth * height;
      }
      static int getCount()
      {
         return objectCount;
      }
   private:
      double length;     // length
      double breadth;    // width
      double height;     // height
};
 
// Initialize static members of class Box
int Box::objectCount = 0;
 
int main(void)
{
  
   // The total number of output objects before they were created
   cout << "Inital Stage Count: " << Box::getCount() << endl;
 
   Box Box1(3.3, 1.2, 1.5);    // Declaration box1
   Box Box2(8.5, 6.0, 2.0);    // Declaration box2
 
   // Total number of output objects after object creation
   cout << "Final Stage Count: " << Box::getCount() << endl;
 
   return 0;
}

Tags: C++ Back-end

Posted on Fri, 12 Nov 2021 04:51:57 -0500 by QWERTYtech