[C ++]-classes and objects, this pointer

One, oop object-oriented

In c, we have various definitions of functions. But in C ++, we are the abstract type of entity. The following picture describes the relationship between their oop ideas.
Insert picture description here
Four characteristics of oop language: abstract encapsulation (hidden) inheritance polymorphism

Second, classes and objects

Let's describe a commodity class to specify a class and an object.
First: define a commodity class, which is equivalent to a struct in c language.

1. Class definition

const int NAME_LEN = 20;
class CGoods
{
public:
	void init(const char* name, double price, int amount);//做商品数据初始化
	void show();//打印商品信息
	void setName(char* name) { strcpy(_name, name); }
	void setPrice(double price) { _price = price ;}
	void setAmount(int amount) { _amount = amount ;}

	const char* getName() {return _name;}
	double getPrice() { return _price ;}
	int getAmount() { return _amount; }
private:
	char _name[NAME_LEN];
	double _price;
	int _amount;
};

In the above class, we can understand
(1) private : attributes (member variables) are generally private

(2) public : The member method provides a common method to the outside to access private properties **. The methods implemented in the class are automatically processed into inline functions **

(3) setXXX and getXXX : Set an interface to access member variables individually or get the value of member variables, and provide a set of getXXX or setXXX methods for member variables

(4) class : abstract data type of goods, not occupying space CGoods can define countless objects, each object has its own member variable, but they share a set of member methods. The memory size of the object and the member variable
can be passed. Only use the cmd command in VS to view the memory size of the object.Insert picture description here

2. Out-of-class definition

Methods defined outside the class must be scoped in front of the function name of the class, which is an ordinary function. If you want to become an inline function, you must explicitly add inline

void CGoods::init(const char* name, double price, int amount)
{
	strcpy(_name, name);
	_price = price;
	_amount = amount;
}
void CGoods::show()
{
	cout << "name:" << _name << endl;
	cout << "price:" << _price << endl;
	cout << "amount:" << _amount << endl;
}

3. Main function instantiation class

int main()
{
	CGoods good1;//类实例化了一个对象,对象占用空间,在栈上
	good1.init("面包", 10.0, 200);
	good1.show();
	good1.setPrice(20.5);
	good1.setAmount(100);
	good1.show();

	CGoods good2;
	good2.init("空调", 10000.0, 50);
	good2.show();

	return 0;
}

The running result is:
Insert picture description here

Three, this pointer

Function: To distinguish different objects in the method.
Let's think about such a problem.
(1) How does init (name, price, amount) know which object to initialize the information to?
good1.init ("bread", 10.0, 200); in fact, good1.init (& good1, "bread", 10.0, 200); the object method of the class into which the object address is passed
is compiled, all the method parameters, Will add a this pointer to receive the address of the object calling the method

(2) Why can a set of methods handle multiple objects?
Because when an object calls a method, he passes the address of the object to its function through actual parameters, and these functions will use the tils pointer generated at compile time to accept the address of the incoming object

The following is the calling method of this pointer

void CGoods::init(const char* name, double price, int amount)
{
	strcpy(_name, name);
	this->_price = price;
	this->_amount = amount;
}
void CGoods::show()
{
	cout << "name:" << this->_name << endl;
	cout << "price:" << this->_price << endl;
	cout << "amount:" << this->_amount << endl;
}
Published 98 original articles · won praise 9 · views 3674

Guess you like

Origin blog.csdn.net/qq_43412060/article/details/105060327