Hàm lớp C++

Tài liệu này mô tả phong cách viết mã đơn giản nhất có thể để tạo các lớp trong C. Nó sẽ mô tả các hàm tạo, biến đối tượng, phương thức đối tượng, biến lớp, phương thức lớp, thừa kế, đa hình, không gian tên với bí danh và kết hợp tất cả lại với nhau trong một dự án ví dụ

1. Lớp C

Một lớp bao gồm một loại thể hiện và một đối tượng lớp

Một loại thể hiện là một

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
0 chứa các thành viên biến được gọi là biến thể hiện và các thành viên hàm được gọi là phương thức thể hiện. Một biến của kiểu thể hiện được gọi là một thể hiện

Một đối tượng lớp là một biến

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
1 toàn cục chứa các biến lớp và phương thức lớp. Các thành viên này thuộc về cả lớp mà không có bất kỳ tham chiếu nào đến bất kỳ trường hợp nào

Một lớp có tên là "Phức hợp" nên đặt tên cho loại thể hiện là

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
2 và đối tượng lớp là
	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
3, đồng thời đặt các định nghĩa giao diện trong "Phức hợp. h" và việc triển khai trong "Complex. c"

	Complex.h:
		struct Complex {
			...
		};
		extern const struct ComplexClass {
			...
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		const struct ComplexClass Complex={...};

+ Cơ sở lý luận

Trên Đối tượng lớpCác thành viên của đối tượng lớp cũng có thể đã được triển khai dưới dạng các biến và hàm toàn cục và điều này thậm chí sẽ cho phép triển khai hàm tạo dưới dạng hàm macro. Tuy nhiên, để đơn giản trong thiết kế và để đơn giản trong việc xác định và sử dụng không gian tên, thiết kế đã chọn được ưu tiên hơn

2. nhà xây dựng

Các thể hiện phải được khởi tạo bởi các hàm tạo khi được khai báo và các hàm tạo phải là các phương thức của lớp. Hàm tạo tốt nhất nên trả về một kiểu thể hiện, nhưng cũng có thể trả về một con trỏ tới một kiểu thể hiện

Lớp Complex có hai biến thể hiện

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
4 và
	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
5, và một hàm tạo có tên là
	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
6

________số 8

3. phương pháp

Các phương thức thể hiện phải được khai báo là các thành viên kiểu thể hiện trỏ đến nguyên mẫu hàm mong muốn và con trỏ đó phải được đặt bởi hàm tạo. Thông thường, con trỏ phương thức được đặt thành hàm

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
7 được xác định trong tệp triển khai

Để có thể truy cập dữ liệu của đối tượng, các phương thức của đối tượng phải nhận một con trỏ tới đối tượng làm đối số đầu tiên của nó. Đối số này thường được đặt tên là

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
8

Khi chúng tôi thêm một phương thức thể hiện

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
9 để tính giá trị tuyệt đối của một số phức, chúng tôi nhận được

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5

Các phương thức lớp phải được khởi tạo giống như các phương thức cá thể, nhưng không có hạn chế đối với nguyên mẫu

4. Di sản

Một lớp cơ sở phải được biểu diễn dưới dạng một biến thành viên có cùng tên và kiểu như chính lớp cơ sở đó

Một lớp con có thể ghi đè lên các con trỏ phương thức thể hiện của lớp cơ sở để cung cấp tính đa hình. Lớp con phải ghi đè bằng một hàm nguyên mẫu giống hệt và đặt con trỏ phương thức của lớp cơ sở trong hàm tạo sau khi hàm tạo của lớp cơ sở được gọi

Bất cứ khi nào một phương thức thể hiện bị ghi đè được gọi, chúng tôi đảm bảo rằng nó được gọi bởi một thể hiện của lớp cơ sở. Vì phương thức thể hiện nhận một con trỏ tới lớp cơ sở làm đối số đầu tiên của nó, nên chúng ta có thể lấy lớp con bằng cách sử dụng macro

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
30 từ
	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
31

Các tệp sau đây cho thấy một ví dụ đơn giản về thừa kế và đa hình

+Nhân viên. h

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
3

+Nhân viên. c

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
6

+Quản lý. h

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
7

+Quản lý. c

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
8

+thừa kế. c

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
9

Lớp Người quản lý ghi đè phương thức thể hiện print() của Nhân viên bằng dòng từ Người quản lý. c

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
0

Điều gì tạo nên sự kế thừa. c in

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
1

5. Kiểm soát quyền truy cập vào các thành viên

Trong các ngôn ngữ hướng đối tượng, mỗi thành viên có một thuộc tính truy cập và trình biên dịch sẽ thực thi thuộc tính truy cập đó

Với các Lớp trong C, chúng ta nên sử dụng các chú thích để chỉ định các thuộc tính truy cập. Chẳng hạn, chúng tôi sử dụng ký hiệu sau

	Complex.h:
		struct Complex {
			double re, im;
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
0

6. Các lớp trừu tượng, phương thức và giao diện trừu tượng

Trong các ngôn ngữ hướng đối tượng, chúng ta có thể chỉ định một lớp trừu tượng để đảm bảo rằng lớp đó không thể được khởi tạo. Các phương thức và giao diện trừu tượng có thể được sử dụng để đảm bảo rằng các lớp con ghi đè lên các phương thức

Chẳng hạn, với các Lớp trong C, chỉ cần đảm bảo rằng bất kỳ người dùng nào của lớp đều hiểu được những ý định đó

	Complex.h:
		struct Complex {
			double re, im;
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
1

Các con trỏ phương thức thể hiện trừu tượng nên được khởi tạo thành

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
32

7. không gian tên

Một không gian tên xác định một tiền tố chung của tất cả các mã định danh được xuất bởi một lớp và đường dẫn của các tệp tiêu đề và tệp triển khai của lớp đó

Chẳng hạn, một lớp

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
3 có không gian tên
	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
34 nên có tệp triển khai của nó trong
	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
35 và tệp tiêu đề của nó trong
	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
36 có chứa

	Complex.h:
		struct Complex {
			double re, im;
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
2

Khi chúng tôi sẽ sử dụng lớp, chúng tôi có thể đặt bí danh cho các mã định danh để làm cho chúng dễ quản lý hơn, bằng cách sử dụng chỉ thị

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
37

	Complex.h:
		struct Complex {
			double re, im;
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
3

8. Một dự án ví dụ

Trong dự án ví dụ này, chúng tôi sẽ tạo và kiểm tra triển khai ngăn xếp kiểm tra giới hạn bằng cách mở rộng triển khai ngăn xếp đơn giản hơn. Dự án sẽ minh họa mọi thứ về Lớp C bao gồm hàm tạo, phương thức, thừa kế, không gian tên và bí danh

Để biên dịch dự án này, bạn nên đọc C Project Building

Dự án thư viện

Chúng tôi tưởng tượng dự án ngăn xếp đơn giản đã được tải xuống từ mạng và tệp tiêu đề có thể được tham chiếu là

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
38. Tiêu đề ngăn xếp chứa

+Ngăn xếp. h

	Complex.h:
		struct Complex {
			double re, im;
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
4

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
39 là một giao diện cho các phần tử được lưu trữ trong ngăn xếp và không áp đặt hạn chế nào đối với các phần tử được lưu trữ do
	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
0 có phần thân trống

Chúng tôi chọn một không gian tên org_pvv_hakonhal_utils duy nhất và tạo thư mục

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
61 nơi chúng tôi sẽ đặt các tệp
	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
62 và
	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
63 của mình

Điều duy nhất lớp của chúng tôi sẽ làm ngoài

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
64, là kiểm tra các giới hạn khi
	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
65'ing và
	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
66'ing, vì vậy, loại phiên bản của chúng tôi chỉ giữ tham chiếu đến lớp cơ sở

+BStack. h

	Complex.h:
		struct Complex {
			double re, im;
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
5

Tệp triển khai có phần phức tạp hơn

+BStack. c

	Complex.h:
		struct Complex {
			double re, im;
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
6

Chúng ta hãy xem việc triển khai phương thức cá thể của

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
65,
	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
66 cũng tương tự. Vì nó đang sử dụng phương thức thể hiện
	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
65 của lớp cơ sở, nên chúng ta phải giữ một con trỏ tới phương thức thể hiện của lớp cơ sở, xem 10, 18 và 33-34

Thử nghiệm Dự án Thư viện

Việc triển khai lớp

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
70 khá lâu

+BStack_test. c

	Complex.h:
		struct Complex {
			double re, im;
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
7

Chúng tôi định nghĩa một lớp

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
71 mở rộng giao diện
	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
39 để nó có thể được thêm vào
	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
73 của chúng tôi, xem các dòng 8-27. Lớp này cũng chứa một int và một phương thức thể hiện để in nó. Lưu ý rằng vì chúng tôi đang xây dựng một tệp thực thi, lớp
	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
71 không cần phải có không gian tên

Vì chúng tôi đang sử dụng bí danh, các câu lệnh như trên dòng 30 thực sự đọc

	Complex.h:
		struct Complex {
			double re, im;
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
8

Do các phương thức thực thể của

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
65 và
	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
66 được định nghĩa bởi
	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
77, nên chúng ta cần "đi qua" phân lớp
	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
77 để gọi chúng, như đã thấy trên e. g. dòng 33-34

Khi chúng tôi truy xuất các phần tử trên ngăn xếp, chúng tôi cần cú pháp hơi khó xử trong 35 và 37. Về mặt khái niệm, chúng tôi nhận được một con trỏ tới lớp cơ sở

	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
79 của một biến
	Complex.h:
		struct Complex {
			double re, im;
			double (*abs)(struct Complex *this);
		};
		extern const struct ComplexClass {
			struct Complex (*new)(double real, double imag);
		} Complex;
	
	Complex.c:
		#include "Complex.h"
		static double abs(struct Complex *this) {
			return sqrt(this->re*this->re+this->im*this->im);
		}
		static struct Complex new(double real, double imag) {
			return (struct Complex){.re=real, .im=imag, .abs=&abs};
		}
		const struct ComplexClass Complex={.new=&new};
	
	Complex_test.c:
		...
		struct Complex c=Complex.new(3., -4.);
		printf("%g\n", c.abs(&c)); // Prints 5
80, vì vậy chúng tôi chỉ cần dịch chuyển nó

Chức năng lớp C là gì?

Master C và Lập trình C nhúng- Học khi bạn đi . Nó hoạt động trên bất kỳ đối tượng nào của lớp mà nó là thành viên và có quyền truy cập vào tất cả các thành viên của lớp cho đối tượng đó

Chức năng lớp có nghĩa là gì?

Trong toán học, đặc biệt là trong lĩnh vực lý thuyết nhóm và lý thuyết biểu diễn nhóm, hàm lớp là hàm trên nhóm G không đổi trên lớp liên hợp của G< . Nói cách khác, nó bất biến dưới ánh xạ liên hợp trên G. Các hàm như vậy đóng vai trò cơ bản trong lý thuyết biểu diễn. . In other words, it is invariant under the conjugation map on G. Such functions play a basic role in representation theory.

Hàm lớp trong C Plus Plus là gì?

Lớp học. Một lớp trong C ++ là khối xây dựng dẫn đến lập trình Hướng đối tượng. Đó là kiểu dữ liệu do người dùng định nghĩa, chứa các thành phần dữ liệu và hàm thành viên riêng, có thể được truy cập và sử dụng bằng cách tạo một thể hiện của lớp đó . Một lớp C++ giống như một bản thiết kế cho một đối tượng.

Tôi có thể sử dụng lớp trong C không?

Lớp C . Một kiểu thể hiện là một cấu trúc chứa các thành viên biến được gọi là biến thể hiện và các thành viên hàm được gọi là phương thức thể hiện. Một biến của kiểu thể hiện được gọi là một thể hiện