About some C++, Qt, Python terms

Miscellaneous fish used to sort out the terminology that python did not understand in detail before, often see Chang Xin (

Definition:

Definition refers to the allocation of memory space to an entity for use in the program. In C++ and Python, this entity can be a variable, function or class. In C++, a definition is usually what implements an entity after a declaration.
For example f(int a, int b)

// 定义整数变量x
int x;

// 定义函数add,计算两个整数的和
int add(int a, int b) {
    
    
    return a + b;
}
# 定义整数变量x
x = 10

# 定义函数add,计算两个整数的和
def add(a, b):
    return a + b

Declaration:

Declaration refers to declaring the existence of entities in the program, but not allocating memory space for them. In C++ and Python, this allows the compiler or interpreter to be told ahead of time the name and type of the entity before it is used.
For example f(int, int)

// 声明整数变量y
extern int y;

// 声明函数add,用于计算两个整数的和
int add(int a, int b);

Python does not require explicit declaration

Instance:

An instance is a concrete object of a class. In C++ and Python, instantiation is creating an instance of a class and allocating space in memory to store its attributes and methods.

// 定义一个简单的类Person
class Person {
    
    
public:
    int age;
};

// 创建Person类的实例p1
Person p1;
p1.age = 30; // 设置实例p1的age属性为30
# 定义一个简单的类Person
class Person:
    pass

# 创建Person类的实例p1
p1 = Person()
p1.age = 30  # 设置实例p1的age属性为30

Call (Call):

Invocation refers to the process of executing a function or method. In C++ and Python, call a function or method by using its name and appropriate parameters.
For example f(a, b)

// 定义函数multiply,计算两个整数的乘积
int multiply(int a, int b) {
    
    
    return a * b;
}

// 调用函数multiply,并将结果存储在变量result中
int result = multiply(5, 3);
# 定义函数multiply,计算两个整数的乘积
def multiply(a, b):
    return a * b

# 调用函数multiply,并将结果存储在变量result中
result = multiply(5, 3)

Inheritance:

Inheritance is an important concept in object-oriented programming that allows one class (called a subclass or derived class) to inherit the properties and methods of another class (called a parent or base class).

// 定义一个基类Animal
class Animal {
    
    
public:
    void makeSound() {
    
    
        cout << "Some generic sound" << endl;
    }
};

// 定义一个派生类Dog,继承自Animal
class Dog : public Animal {
    
    
public:
    void makeSound() {
    
    
        cout << "Woof! Woof!" << endl;
    }
};

// 创建Dog类的实例d1,并调用其makeSound方法
Dog d1;
d1.makeSound(); // 输出:Woof! Woof!
# 定义一个基类Animal
class Animal:
    def makeSound(self):
        print("Some generic sound")

# 定义一个派生类Dog,继承自Animal
class Dog(Animal):
    def makeSound(self):
        print("Woof! Woof!")

# 创建Dog类的实例d1,并调用其makeSound方法
d1 = Dog()
d1.makeSound()  # 输出:Woof! Woof!

Animal is the parent item of Dog

Encapsulation:

Encapsulation is an object-oriented programming concept that encapsulates data and code in one unit and hides implementation details from the outside. This ensures data security and simplifies code usage.

class Person {
    
    
private:
    int age;

public:
    void setAge(int newAge) {
    
    
        age = newAge;
    }

    int getAge() {
    
    
        return age;
    }
};

// 创建Person类的实例p1
Person p1;
p1.setAge(25);
int currentAge = p1.getAge(); // 通过公共方法访问私有属性age

In Python, there is no strict access control, but encapsulation is achieved by convention defining attributes as private and providing public methods.

Packaging:

Packaging refers to organizing related modules and resources together for easy distribution, installation, and use. In C++, packaging usually refers to grouping related classes, header files, and source files together to form a library file. In Python, packaging generally refers to organizing modules into packages for easy distribution and installation.
Packaging example in C++: Create a library file using the C++ SDK (Software Development Kit).

Packaging example in Python: organize related modules in a folder, and include an __init__.py file in the folder, making it a package.

Constructor:

Constructors are special methods that are called automatically when an instance of a class is created to initialize the properties and state of the object. In C++, the name of the constructor is the same as the class name, and in Python, the __init__ method is used as the constructor.

class Person {
    
    
public:
    int age;

    // 构造函数
    Person() {
    
    
        age = 0;
    }

    Person(int initialAge) {
    
    
        age = initialAge;
    }
};

// 创建Person类的实例p1,并调用带参数的构造函数
Person p1(30);
class Person:
    def __init__(self):
        self.age = 0

    def __init__(self, initialAge):
        self.age = initialAge

# 创建Person类的实例p1,并调用带参数的构造函数
p1 = Person(30)

:: (Scope Resolution Operator):

In C++, :: is the scope resolution operator, which is used to specify to find an identifier (such as a member variable or method of a class) in a specific scope. It is also used to access elements in a namespace.
There is no :: operator in Python because Python uses a dot (.) as the scope resolution operator.

Launch (Signal) and emit:

In Qt, a signal is a mechanism for sending messages between objects. Signals are special member functions that can be emitted with the emit keyword when a specific event occurs. Other objects can connect to these signals and execute the associated slot function when the signal is emitted.

class MyButton : public QPushButton {
    
    
    Q_OBJECT

signals:
    void clickedWithCount(int count);

public slots:
    void onButtonClick() {
    
    
        static int clickCount = 0;
        clickCount++;
        emit clickedWithCount(clickCount);
    }
};

// 创建MyButton的实例,并连接到槽函数
MyButton button;
QObject::connect(&button, &MyButton::clickedWithCount, [](int count) {
    
    
    cout << "Button clicked " << count << " times" << endl;
});

button.click(); // 触发信号,槽函数输出 "Button clicked 1 times"
button.click(); // 触发信号,槽函数输出 "Button clicked 2 times"

In Python, there is no explicit signal and slot mechanism, but you can use the signal and slot module in Qt for Python to achieve similar functionality.

Messages and slots are a mechanism in Qt for communicating between objects. An object emits a signal (information), and other objects are connected to the signal's slot function. When the signal is emitted, the slot function will be executed.
In the Qt example above, clickedWithCount is a signal and onButtonClick is a slot.

SDK (Software Development Kit):

SDK, short for Software Development Kit, is a collection for developing applications, usually including libraries, API documentation, sample code, and tools. An SDK can help developers more easily use the functionality of a specific platform or framework.
In C++ and Qt, Qt SDK is a toolkit for developing cross-platform applications, including Qt library, Qt Creator IDE, Qt documentation, etc.

In Python, the Python SDK (also known as the Python Standard Library) is the core library of the Python language, providing many built-in modules and functions for performing various tasks.

Dynamic (Dynamic) and static (Static):

Dynamic and static are often used to describe properties of a language or type system.
Dynamic language (such as Python): The variable type is determined at runtime, does not need to explicitly declare the type, has greater flexibility, but may cause runtime errors in some cases.

Static language (such as C++): The variable type is determined at compile time, and the type needs to be explicitly declared, which is more strict and type-safe, but may lead to more cumbersome code writing.

In C++, variables can have static or dynamic storage duration, which is related to the variable's lifetime and scope. In Python, variables are dynamically typed, and the type is inferred by the interpreter at runtime.

Modifier

Modifiers are used to control access and attributes of members in a class. They help implement object-oriented programming concepts such as encapsulation, inheritance, and polymorphism.

public:

In C++ and Qt, the public modifier is used to specify public access to class members. Public members can be accessed inside and outside the class, including objects of the class and other classes.
Example in C++:

class MyClass {
    
    
public:
    int publicVar;
    void publicMethod() {
    
    
        // code here
    }
};

Example in Qt:

class MyClass : public QObject {
    
    
    Q_OBJECT

public:
    int publicVar;
    void publicMethod() {
    
    
        // code here
    }
signals:
    void mySignal();
};

In Python, members of a class are public by default without an explicit public keyword.

protected:

In C++ and Qt, the protected modifier is used to specify protected access to class members. Protected members can be accessed inside the class and in derived classes, but not outside the class.
Example in C++:

class MyClass {
    
    
protected:
    int protectedVar;
    void protectedMethod() {
    
    
        // code here
    }
};

In Qt, due to the special requirements of the signal and slot mechanism, the signal in Qt must be defined in the protected or public part.

In Python, there is no explicit protected keyword, but properties or methods can be implicitly protected by preceding the property or method name with an underscore.

private:

In C++ and Qt, the private modifier is used to specify private access to class members. Private members can only be accessed inside the class and cannot be directly accessed outside the class and in derived classes.
Example in C++:

class MyClass {
    
    
private:
    int privateVar;
    void privateMethod() {
    
    
        // code here
    }
};

Example in Qt:

class MyClass : public QObject {
    
    
    Q_OBJECT

private:
    int privateVar;
    void privateMethod() {
    
    
        // code here
    }
};

In Python, a similar effect is achieved by prefixing the property or method name with two underscores, i.e. making the property or method private.

signal and slot:

In Qt, signal is a special member function used to transmit messages, and is used to implement the information and slot mechanism. It only declares, not defines, because it is handled by Qt's meta-object system. Signal is used to issue notifications, and slot is a slot function used to receive notifications.
Example in C++ and Qt:

class MyClass : public QObject {
    
    
    Q_OBJECT

signals:
    void mySignal();

public slots:
    void mySlot() {
    
    
        // code here
    }
};

In Python, there is no direct signal and slot mechanism. However, a similar signal and slot mechanism can be used by using a Qt for Python library such as PyQt or PySide.
———————————————————
There are examples of definition declaration calls:

#include <iostream>

// 函数声明,只需写参数类型
int add(int, int);

// 函数定义,实现两个整数相加的功能
int add(int a, int b) {
    
    
    return a + b;
}

int main() {
    
    
    a = 5;
    b = 3;
    // 函数调用,传递参数值并获得返回结果
    int result = add(a, b);

    // 输出结果
    std::cout << "Result: " << result << std::endl;

    return 0;
}

pointer, address

Pointer is an important concept related to memory address and data type. Let's explain the following terms step by step:

Pointer:
A pointer is a variable whose value is a memory address, that is, the location of a piece of data in computer memory. Pointers can point to variables of any data type, including basic data types, arrays, structures, classes, etc.

Get address (&):
In C++, use the & operator to get the memory address of the variable, that is, the pointer of the variable. For example, for variable a, &a represents the memory address of a.

int a = 10;
int* ptr = &a; // 获取变量a的地址,并将其赋值给指针ptr


*a:
在C++中,*号用于两个不同的情况:
a. 定义指针变量:例如,int* ptr; 定义了一个指向整数的指针变量ptr。
b. 解引用操作:例如,*ptr 表示获取指针ptr指向地址的值。这个过程称为解引用,它允许我们访问指针指向地址的数据。

int a = 10;
int* ptr = &a; // 定义指向整数的指针ptr,并将其指向a的地址
int value = *ptr; // 解引用ptr,获取a的值,value现在为10
&a:
&a表示变量a的地址,它是一个指向a的指针。

指针的使用:
指针通常用于动态内存分配、函数参数传递、数组操作等。它们使得我们能够在程序运行时管理内存和操作数据。

int main() {
    
    
    int a = 10;
    int* ptr = &a; // ptr指向变量a的地址

    *ptr = 20; // 解引用ptr并将新的值20赋给a

    std::cout << "a: " << a << std::endl; // 输出20,因为通过ptr修改了a的值

    return 0;
}

The above example defines a pointer ptr to an integer, which points to the address of variable a. By dereferencing the pointer ptr, we can modify the value of a. Modifying the value pointed to by the pointer also modifies the variable pointed to by the pointer.

Guess you like

Origin blog.csdn.net/weixin_40459958/article/details/131926406