A brief discussion on C++|class encapsulation

Introduction:

C++ believes that everything is an object , and objects have their properties and behaviors .

People can be used as objects, with attributes such as name, age, height, weight, etc., and behaviors such as walking, eating, singing, etc.

A car can also be used as an object, with attributes such as tires, steering wheels, lights, etc., and behaviors such as carrying people, playing music, etc.

1. Encapsulation

1.1 The meaning of packaging

Encapsulation is one of the three major features of C++ object-oriented

The meaning of encapsulation:

  (1) Use attributes and behaviors as a whole to express things in life.

  (2) Control attributes and behaviors with permissions.

1.1.1 Encapsulation meaning one:

  When designing a class, attributes and behaviors are written together to represent things.

Syntax: class class name {access permissions: attributes/behavior};

Example : Set up a cuboid class and find its volume

Code:

#include <iostream>
using namespace std;
class cuboid {
public:
	int height;
	int weight;
	int length;
	int show_tiji() {
		return height * weight * length;
	}
};
int main() {
	cuboid a1;
	a1.height = 10;
	a1.weight = 10;
	a1.length = 90;
	cout << a1.show_tiji();
	return 0;
}

 1.1.2 The meaning of encapsulation 2

When designing a class, attributes and behaviors can be placed under different permissions and controlled.

There are three types of access rights:

1.public public permissions, accessible within the class and accessible outside the class

2.protected protection permissions, accessible within the class but not accessible outside the class

3.private private permissions, accessible within the class but not accessible outside the class

Code: 

#include <iostream>
using namespace std;
class people {
public:
	string name;
protected:
	string hose;
private:
	int m_Password;
public:
	void fun() {
		name = "小明";
		hose = "别墅";
		m_Password = 12345;
	}

};
int main() {
	people a;
	a.fun();
	cout << a.name << endl;
	//cout << a.hose << endl;   //报错
	//cout << a.m_Password << endl;   //报错
	return 0;
}

1.2The difference between struct and class

1.2.1Default permissions are different

Permissions are divided into member access permissions and inherited permissions

Default member permissions: class is private, struct is public

Default inheritance permissions: class is private, struct is public

Default member permission code: 

#include <iostream>
using namespace std;
class people1 {
	int a;
};
struct people2 {
	int a;
};
int main() {
	people1 a1;
	people2 a2;
	//a1.a = 100;   //报错
	a2.a = 100;
	return 0;
}

Default inherited permission code: 

#include <iostream>
using namespace std;
struct A {
	int m_a;
};

struct B :A {
	int m_b;
};
int main() {
	B b;
	b.m_a = 100;
	return 0;
}

struct defaults to public inheritance;

#include <iostream>
using namespace std;
class A {
public:
	int m_a;
};

class B :A {
public:
	int m_b;
};
int main() {
	B b;
	b.m_b = 100;
	//b.m_a = 100;
	return 0;
}

class default private inheritance

1.2.2 Can template parameters be defined?

Class can define template parameters template<class T>, but there is no template<struct T>

 Code:

#include <iostream>
using namespace std;
//template< typename T, typename Y >	
template< class T >	
void  Func(T& t,T& y)
{
	T temp = t;
	t = y;
	y = temp;
}

int main() {
	int a = 10, b = 20;
	Func(a, b);
	cout << a << ' ' << b << endl;

	return 0;
}

1.2.3 Class and struct inherit from each other

 Default inherited properties depend on subclasses not base classes

Code: 

#include <iostream>
using namespace std;
class A {
public:
	int m_A;
};
struct B:A {
	int m_B;
};

int main() {
	B b;
	b.m_A = 10;
	b.m_B = 20;
	cout << b.m_A <<' '<<b.m_B << endl;
	return 0;
}

struct as a subclass, public inheritance by default

Code: 

#include <iostream>
using namespace std;
struct A {
	int m_A;
};
class B:A {
public:
	int m_B;
};

int main() {
	B b;
	//b.m_A = 10;  报错
	b.m_B = 20;
	cout<<b.m_B << endl;
	return 0;
}

1.2.4 Discussion on {} assignment of initial value

Because C++ is an extension of C, it is compatible with the features of struct in C in the past.

#include <iostream>
using namespace std;
struct A
{
	char 	a1;
	int		a2;
	double	a3;
};


int main() {
	A a = { 'p', 7, 451.154 }; //定义时赋初值,在struct时没问题,在class时出错
	return 0;
}

Add a constructor (or virtual function) to the struct, and the struct cannot be assigned with {}. The reason is that the assignment is done in {}, and only an initialization list is used to initialize the data in order. If the above is written as A a = {'p',7}; then a1 and a2 are initialized, but a3 is not. Such a simple copy operation can only occur on simple data structures and should not be placed on objects. Adding a constructor or a virtual function will make strcut more reflective of the characteristics of an object, but the {} operation will no longer be valid. Because such functions (constructors and virtual functions) are added, the internal structure of the class changes. What about adding an ordinary member function? You will find that {} is still available. In fact, you can understand an ordinary function as an algorithm for a data structure, which does not break its data structure characteristics.
 

1.2.5 Summary 

  • Structures are Value Types, while classes are Reference Types.
  • Structures use stack storage (Stack Allocation), while classes use heap storage (Heap Allocation).

Value type variables store data directly, while reference type variables hold a reference to the data, and the data is stored in the data heap.
 

Concept: The syntax of class and struct are basically the same, from declaration to use, they are very similar, but struct has more constraints than class. In theory, class can do everything that struct can do, but struct can do everything that class can do. It may not be possible.
Type: struct is a value type and class is a reference type, so they have all the differences between value types and reference types. Efficiency
: Since the execution efficiency of the stack is higher than that of the heap, the stack resources are very is limited and not suitable for processing large objects with complex logic. Therefore, struct is often used to handle small objects treated as base types, while class handles a certain business logic
relationship: struct can not only inherit and be inherited, but also implement interfaces, but Class Fully scalable. There are differences in the internal structure. struct can only add constructors with parameters, cannot use modifiers such as abstract and protected, and cannot initialize instance fields.
 

1.2.6 Selection skills

(1) When representing lightweight objects such as points and rectangles that are mainly used to store data, struct is preferred.

(2) When representing large objects with large amounts of data and complex logic, class is preferred

(3) Class is the best choice when expressing abstract and multi-level object hierarchies. 

1.3 Benefits of setting member attributes to privatization

1.3.1 Control read and write permissions

Readable and writable permissions, read-only permissions, write-only permissions

Code: 

#include <iostream>
using namespace std;
class people {
public:
	void get_name(string name_1) {
		name = name_1;          //写权限
	}
	string show_name() {
		return name;           //读权限
	}

	void get_mima(string mima1) {
		mima = mima1;          //写权限
	}
	int show_age() {           //读权限
		return age;
	}
private:
	//可读,可写
	string name;
	//只读
	int age;
	//只写
	string mima;

};
int main() {
	people p;
	p.get_name("小明");
	cout << p.show_name() << endl;
	p.get_mima("56789");
	cout << p.show_age();  //没有设置,随机值
	return 0;
}

 1.3.2 Validity of write permission detection data

 Code:

#include <iostream>
using namespace std;
class people {
public:
	void get_name(string name_1) {
		name = name_1;          //写权限
	}
	string show_name() {
		return name;           //读权限
	}

	void get_mima(string mima1) {
		if (mima1.size() < 3) {
			cout << "密码太短,无效!" << endl;
			return;
		}
		mima = mima1;          //写权限
	}
	int show_age() {           //读权限
		return age;
	}
private:
	//可读,可写
	string name;
	//只读
	int age;
	//只写
	string mima;

};
int main() {
	people p;
	p.get_name("小明");
	cout << p.show_name() << endl;
	p.get_mima("59");
	cout << p.show_age();  //没有设置,随机值
	return 0;
}

Guess you like

Origin blog.csdn.net/m0_73731708/article/details/132927260