嵌入式linux基础:c++(三)程序结构

简介: 嵌入式linux基础:c++(三)程序结构

1.程序结构


上一文说到了函数写在了类里面,那么可不可以写在类外面。

#include <stdio.h>
class Person {
private:
  char *name;
  int age;
  char *work;
public:
  void setName(char *name);//在class里面只是申明
  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)//将这个写到类的外面去,写成person::表示,person这个类里面的这个函数。
{
  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实现person类,由B实现main函数。B只关心怎么用person内。那么A就要提供person.h以及person.cpp

1671002725655.jpg

代码如下:

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); 
}


继续修改代码,让程序更加复杂 假设进入一个新人要实现Dog类。

1671002770471.jpg


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 {//由于打印了两个完全一样的printVersion,需要区分开来。
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 $@ $^
%.o : %.cpp
  g++ -c -o $@ $<
clean:
  rm -f *.o person


你看到的A:: C:: 这些会不会太麻烦了。做一些小修改:

main.c

#include <stdio.h>
#include "person.h"
#include "dog.h"
/* global namespace */
/* 把A::person 放入global namespace ,以后可以使用Person来表示A ::Person */
using A::Person;
/* 把C::Dog 放入global namespace ,以后可以使用Dog来表示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;
}


再来改进一下:

#include <stdio.h>
#include "person.h"
#include "dog.h"
using namespace A;//他把A空间的东西都导出来
using namespace C;//他把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();//导出来以后会冲突啊,所以这一句加上A::printVersion();
  C::printVersion();//导出来以后会冲突啊,所以这一句加上C::printVersion();
  return 0;
}

1671002869583.jpg


最后的修改如下:

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);
}
相关文章
|
1月前
|
安全 Linux Shell
Linux上执行内存中的脚本和程序
【9月更文挑战第3天】在 Linux 系统中,可以通过多种方式执行内存中的脚本和程序:一是使用 `eval` 命令直接执行内存中的脚本内容;二是利用管道将脚本内容传递给 `bash` 解释器执行;三是将编译好的程序复制到 `/dev/shm` 并执行。这些方法虽便捷,但也需谨慎操作以避免安全风险。
|
2月前
|
网络协议 Linux
Linux查看端口监听情况,以及Linux查看某个端口对应的进程号和程序
Linux查看端口监听情况,以及Linux查看某个端口对应的进程号和程序
145 2
|
2月前
|
Linux Python
linux上根据运行程序的进程号,查看程序所在的绝对路径。linux查看进程启动的时间
linux上根据运行程序的进程号,查看程序所在的绝对路径。linux查看进程启动的时间
47 2
|
8天前
|
消息中间件 分布式计算 Java
Linux环境下 java程序提交spark任务到Yarn报错
Linux环境下 java程序提交spark任务到Yarn报错
18 5
|
10天前
|
C++
【C++基础】程序流程结构详解
这篇文章详细介绍了C++中程序流程的三种基本结构:顺序结构、选择结构和循环结构,包括if语句、三目运算符、switch语句、while循环、do…while循环、for循环以及跳转语句break、continue和goto的使用和示例。
14 2
|
2月前
|
PHP C++ Python
右手坐标系,空间点绕轴旋转公式&程序(Python和C++程序)
右手坐标系,空间点绕轴旋转公式&程序(Python和C++程序)
26 0
|
2月前
|
Linux Windows Python
最新 Windows\Linux 后台运行程序注解
本文介绍了在Windows和Linux系统后台运行程序的方法,包括Linux系统中使用nohup命令和ps命令查看进程,以及Windows系统中通过编写bat文件和使用PowerShell启动隐藏窗口的程序,确保即使退出命令行界面程序也继续在后台运行。
|
1天前
|
Linux
Linux常用命令包括
Linux常用命令包括
10 5
|
1天前
|
Linux
Linux命令
Linux命令
12 5
|
5天前
|
Linux Python Perl
Linux命令删除文件里的字符串
Linux命令删除文件里的字符串
18 7
下一篇
无影云桌面