current position:Home>The foundation of embedded Linux: the introduction of c++ (I) class

The foundation of embedded Linux: the introduction of c++ (I) class

2022-06-24 09:10:10da..

List of articles

Ps: This C++ Series is the basis of video recorded by Mr. weidongshan , Please support Mr. Wei . This column is :
1. Learning notes .
2. Organize and summarize knowledge points .
3. Summary of personal project experience .

1. Class introduction

C Languages are process-oriented Come up with a question :
Write a program , Output Zhang San , Li Si's names , Age .

#inlude <stdio.h>
int main(int argc,char **argv)// Address of multidimensional array , Row to column 
{
    
	char *zs_name = "zhangsan";
	int zs_age =10; 
	
	char *ls_name = "lisi";
	int ls_age =10; 

	printf("name = %s,age = %d\n",zs_name,zs_age);
	printf("name = %s,age = %d\n",ls_name,ls_age);
	return 0;
}

To improve the : Write a program , Output the names of hundreds of people , Age .

#include <stdio.h>

int main(int argc, char **argv)
{
    
	char *names[] = {
    "zhangsan", "lisi"};
	char ages[] = {
    10, 16};
	char *work[] = {
    "teacher", "doctor"};
	
	int i;

	for (i = 0; i < 2; i++)
	{
    
		printf("name = %s, age = %d\n", names[i], ages[i]);	
	}
	
	return 0;
}

Further improvement , Use the pointer , Structure .

#include <stdio.h>


struct person {
    
	char *name;
	int age;
	char *work;

	void (*printInfo)(struct person *per);
};

void printInfo(struct person *per)
{
    
	printf("name = %s, age = %d, work = %s\n", per->name, per->age, per->work);	
}

int main(int argc, char **argv)
{
    
	struct person persons[] = {
    
		{
    "zhangsan", 10, "teacher",printInfo},
		{
    "lisi", 16, "doctor",printInfo},
	};
	
	
	persons[0].printInfo(&persons[0]);
	persons[1].printInfo(&persons[1]);

	return 0;
}

Can it be improved ?

#include <stdio.h>

struct person {
    
	char *name;
	int age;
	char *work;

	void printInfo(void)
	{
    
		printf("name = %s, age = %d, work = %s\n", name, age, work); 
	}
};

int main(int argc, char **argv)
{
    
	struct person persons[] = {
    
		{
    "zhangsan", 10, "teacher"},
		{
    "lisi", 16, "doctor"},
	};
	
	persons[0].printInfo();
	persons[1].printInfo();
	
	return 0;
}

This is a little more elegant , Again C++ Chinese vs struct Two extensions are made :
First of all , You can write functions directly in the structure .
second , This function can directly access these attributes in the structure .

So I took this opportunity to struct Define as a new category :class.

#include <stdio.h>

class person {
    
public:
	char *name;
	int age;
	char *work;

	void printInfo(void)
	{
    
		printf("name = %s, age = %d, work = %s\n", name, age, work); 
	}
};

int main(int argc, char **argv)
{
    
	struct person persons[] = {
    
		{
    "zhangsan", 10, "teacher"},
		{
    "lisi", 16, "doctor"},
	};
	
	persons[0].printInfo();
	persons[1].printInfo();
	
	return 0;
}

The following will focus on object-oriented programming :
1. encapsulation
2. Inherit
3. polymorphic

copyright notice
author[da..],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/175/202206240746598731.html

Random recommended