current position:Home>Embedded Linux foundation: c++ (III) program structure

Embedded Linux foundation: c++ (III) program structure

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

List of articles

1. Program structure

The previous article mentioned that functions are written in classes , So can it be written outside the class .

#include <stdio.h>

class Person {
    
private:
	char *name;
	int age;
	char *work;

public:
	void setName(char *name);// stay class It's just a statement 
	int setAge(int age);
	void printInfo(void);
};

//void setName(char *name); persom::
void Person::setName(char *name)
{
    
	this->name = name;
}

int Person::setAge(int age)// Write this out of the class , It's written in person:: Express ,person This function in this class .
{
    
	if (age < 0 || age > 150)
	{
    
		this->age = 0;
		return -1;
	}
	this->age = age;
	return 0;
}

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

int main(int argc, char **argv)
{
    
	Person per;

	//per.name = "zhangsan";
	per.setName("zhangsan");
	per.setAge(200);
	per.printInfo();
	
	return 0;
}

A team , from A Realization person class , from B Realization main function .B Only care about how to use it person Inside . that A It's about providing person.h as well as person.cpp

 Insert picture description here

The code is as follows :

main.cpp

#include <stdio.h>
#include "person.h"

int main(int argc, char **argv)
{
    
	Person per;

	//per.name = "zhangsan";
	per.setName("zhangsan");
	per.setAge(200);
	per.printInfo();
	
	return 0;
}

person.h

#include <stdio.h>

class Person {
    
private:
	char *name;
	int age;
	char *work;

public:
	void setName(char *name);
	int setAge(int age);
	void printInfo(void);
};

person.cpp


#include <stdio.h>
#include "person.h"

void Person::setName(char *name)
{
    
	this->name = name;
}

int Person::setAge(int age)
{
    
	if (age < 0 || age > 150)
	{
    
		this->age = 0;
		return -1;
	}
	this->age = age;
	return 0;
}

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


Continue to modify the code , Make the program more complex Suppose a newcomer wants to achieve Dog class .

 Insert picture description here
main.cpp

#include <stdio.h>
#include "person.h"
#include "dog.h"

int main(int argc, char **argv)
{
    
	A::Person per;
	per.setName("zhangsan");
	per.setAge(16);
	per.printInfo();

	C::Dog dog;
	dog.setName("wangcai");
	dog.setAge(1);
	dog.printInfo();

	A::printVersion();
	C::printVersion();
	return 0;
}

person.h


#include <stdio.h>

namespace A {
    // Because two identical printVersion, It needs to be distinguished .

class Person {
    
private:
	char *name;
	int age;
	char *work;

public:
	void setName(char *name);
	int setAge(int age);
	void printInfo(void);
};

void printVersion(void);
}


person.cpp



#include <stdio.h>
#include "person.h"

namespace A {
    

void Person::setName(char *name)
{
    
	this->name = name;
}

int Person::setAge(int age)
{
    
	if (age < 0 || age > 150)
	{
    
		this->age = 0;
		return -1;
	}
	this->age = age;
	return 0;
}

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

void printVersion(void)
{
    
	printf("Person v1, by weidongshan\n");
}

}


dog.h


namespace C {
    

class Dog {
    
private:
	char *name;
	int age;
public:
	void setName(char *name);
	int setAge(int age);
	void printInfo(void);
};

void printVersion(void);

}


dog.cpp


#include <stdio.h>
#include "dog.h"

namespace C {
    

void Dog::setName(char *name)
{
    
	this->name = name;
}

int Dog::setAge(int age)
{
    
	if (age < 0 || age > 20)
	{
    
		this->age = 0;
		return -1;
	}
	this->age = age;
	return 0;
}

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

void printVersion(void)
{
    
	printf("Dog v1, by weidongshan\n");
}

}


Makefile

person: main.o person.o dog.o
	g++ -o [email protected] $^

%.o : %.cpp
	g++ -c -o [email protected] $<

clean:
	rm -f *.o person	

What you see A:: C:: Would these be too much trouble . Make some minor changes :
main.c


#include <stdio.h>
#include "person.h"
#include "dog.h"

/* global namespace */

/*  hold A::person  Put in global namespace , Can be used later Person To express A ::Person */
using A::Person;

/*  hold C::Dog  Put in global namespace , Can be used later Dog To express C::Dog */
using C::Dog;


int main(int argc, char **argv)
{
    
	/* local namespace */
	//using A::Person;
	//using C::Dog;

	Person per;
	per.setName("zhangsan");
	per.setAge(16);
	per.printInfo();

	Dog dog;
	dog.setName("wangcai");
	dog.setAge(1);
	dog.printInfo();

	A::printVersion();
	C::printVersion();
	return 0;
}


Let's improve :


#include <stdio.h>
#include "person.h"
#include "dog.h"

using namespace A;// He put the A Everything in space comes out 
using namespace C;// He put the C Everything in space comes out 

int main(int argc, char **argv)
{
    
	/* local namespace */
	//using A::Person;
	//using C::Dog;

	Person per;
	per.setName("zhangsan");
	per.setAge(16);
	per.printInfo();

	Dog dog;
	dog.setName("wangcai");
	dog.setAge(1);
	dog.printInfo();

	A::printVersion();// There will be conflicts after they are exported , So this sentence adds A::printVersion();
	C::printVersion();// There will be conflicts after they are exported , So this sentence adds C::printVersion();
	return 0;
}


 Insert picture description here

The final revision is as follows :

main.cpp

#include <stdio.h>
#include "person.h"
#include "dog.h"

using namespace A;
using namespace C;

int main(int argc, char **argv)
{
    
	/* local namespace */
	//using A::Person;
	//using C::Dog;

	Person per;
	per.setName("zhangsan");
	per.setAge(16);
	per.printInfo();

	Dog dog;
	dog.setName("wangcai");
	dog.setAge(1);
	dog.printInfo();

	A::printVersion();
	C::printVersion();
	return 0;
}


person.h

#include <stdio.h>

namespace A {
    

class Person {
    
private:
	char *name;
	int age;
	char *work;

public:
	void setName(char *name);
	int setAge(int age);
	void printInfo(void);
};

void printVersion(void);
}

person.cpp

#include <iostream>
#include "person.h"

namespace A {
    

void Person::setName(char *name)
{
    
	this->name = name;
}

int Person::setAge(int age)
{
    
	if (age < 0 || age > 150)
	{
    
		this->age = 0;
		return -1;
	}
	this->age = age;
	return 0;
}

void Person::printInfo(void)
{
    
	std::cout<<"name = "<<name<<" age = "<<age<<" work = "<<work<<std::endl; 
	//cout<< "name = "<<name<< " age = " <<age<< " work = "<<work<< std::endl; 
}

void printVersion(void)
{
    
	std::cout<<"Person v1, by weidongshan"<<std::endl;
}

}


dog.cpp

#include <iostream>
#include "dog.h"

namespace C {
    

using namespace std;

void Dog::setName(char *name)
{
    
	this->name = name;
}

int Dog::setAge(int age)
{
    
	if (age < 0 || age > 20)
	{
    
		this->age = 0;
		return -1;
	}
	this->age = age;
	return 0;
}

void Dog::printInfo(void)
{
    
	cout<<"name = "<<name<<" age = "<<age<<endl; 
}

void printVersion(void)
{
    
	cout<<"Dog v1, by weidongshan"<<endl;
}

}

dog.h

namespace C {
    

class Dog {
    
private:
	char *name;
	int age;
public:
	void setName(char *name);
	int setAge(int age);
	void printInfo(void);
};

void printVersion(void);

}

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

Random recommended