Data member of class

  1, Data member

1. Examples

class Cube//Cube class
{
	long color;//Data member
	double x, y, z, side;//Data member
};

2. Data members can be basic type, array, pointer, reference, common body, enumeration type, void pointer, const restriction and other data types.

class ADT
{
	long color;//Basic type
	double x, y, z, side;//Basic type
	int a[10];//array
	char *s;//Pointer
	char &r;//quote
	void *p;//void pointer
};

3. The data member of the class can also be a member object (for example, the class Line embeds the sub objects start and end of the class Point)

class Point//Point class
{
public:
	void set(int a, int b);//Function declaration
	int x, y;
};
class Line//Line class
{
public:
	void set(Point a, Point b);
	Point start, end;//Member object / / class Line embeds sub objects start and end of class Point
};

4. A class can also be defined in the class, but the new class defined in the class can only be used inside the class.

class ADT
{
	struct Point { int x, y; };//Define structure
	union UData { Point p; long color; };//Define Commons
	enum COLORS{RED,GREEN,BLUE,BLACK,WHITE};//Define enumeration type
	class Nested //Nesting definition 
	{
		Point start;//Data member
		UData end;//Data member
		COLORS color;//Data member
	};
	typedef Point* LPPOINT;//Is a declaration type alias (LPPOINT is the alias of Point *)
};//End of class definition

2, Member function

class Data
{
public:
	void set(int d);//Member function prototype declaration
	int get()//Member function definition
	{
		return data;
	}
private:
	int data;//Data member
};
void Data::set(int d)//External definition of member function, qualified with Data:: function
{
	data = d;//Accessing data members of a class
}
void set(int d)
{
	 //Function body
}

explain:

(1) (::)   Is a scope qualifier       If similar   :: set(10)     :: (10) These two methods mean that the function does not belong to any class. The function is not a member function, but a global ordinary function. At this time (::) is a namespace domain qualifier.

  (2)   1. It is a good programming habit to declare member functions inside the class and define member functions outside the class (this can not only reduce the length of the class body, make the class body structure clear and easy to read, but also help to separate the class interface and Implementation)

        2. If the function body of a member function is not too complex and has only 4-5 lines, it can generally be defined in the class body.

  3, Inline member function (inline is a recommendation to the compiler. Inline does not work when debugging)

Function: embed the corresponding code into the calling function

The class body does not contain control structures such as loops. When it meets the requirements of inline functions, C + + will default to inline functions even without inline annotation. (implicit)

class Data
{
	int getx()//C + + defaults to inline member functions (the function body is very simple and is defined inside the class)
	{
		return x;
	}
	inline int  gety()//Explicitly specify inline member functions
	{
		return y;
	}
	inline void setxy(int _x, int _y);//Explicitly specify inline member functions
	void display();//The function body is implemented outside the class, and C + + will not default it as an inline function
	int x, y;
};
inline void Data::setxy(int _x, int _y)//inline member function 
{
	x = _x,y = _y;
}
void Data::display()//Non inline member function
{
	//Function body
}

To judge whether a function is inline, there are the following:
1. Meet the requirements of inline function

2. It is defined in the class body (the function body is in the class) and automatically becomes inline;

3. If the specified inline is explicitly specified in the class body or displayed in the external definition, or explicitly specified at the same time, the function is inline;

  3, Overloading of member functions and default parameters

class MAX
{
	int Max(int x, int y)
	{
		return x > y ? x : y;
	}
	int Max()
	{
		return Max(Max(a, b), Max(c, d));
	}//Overload Max (there must be at least one different parameter number, type and type of member function)
	int Set(int i = 1, int j = 2, int k = 3, int l = 4)
	{
		a = i, b = j, c = k, d = l;
	}//Default parameters
	int a, b, c, d;
};

4, How member functions are stored

  for example

#include<iostream>
using namespace std;
class Time
{
	int h, m, s;//Data member
	void settime(int a, int b, int c)
	{
		h = a, m = b, s = c;
	}//Member function
};
int main()
{
	cout << "Time The memory occupied is" << sizeof(Time) << endl;
	system("pause");
}

Obviously, the data members h, m and s in the class each account for four bytes, and the class Time accounts for a total of 12 bytes (the storage space of the Time class only depends on the space occupied by the data members). C + + stores the code of the member function outside the object space.

Tags: C C++ Back-end

Posted on Sun, 28 Nov 2021 15:29:17 -0500 by dbradbury