嵌入式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);
}
相关文章
|
6天前
|
C语言 图形学 C++
|
11天前
|
编译器 C++
C++:继承性_程序
C++:继承性_程序
11 1
|
16天前
|
存储 算法 程序员
【C++进阶】深入STL之vector:构建高效C++程序的基石
【C++进阶】深入STL之vector:构建高效C++程序的基石
17 1
|
3天前
|
存储 算法 编译器
程序与技术分享:C++模板元编程简介
程序与技术分享:C++模板元编程简介
|
3天前
|
存储 JavaScript 前端开发
程序与技术分享:C++程序设计实验考试准备资料(2019级秋学期)
程序与技术分享:C++程序设计实验考试准备资料(2019级秋学期)
|
3天前
|
自然语言处理 C语言 C++
程序与技术分享:C++写一个简单的解析器(分析C语言)
程序与技术分享:C++写一个简单的解析器(分析C语言)
|
3天前
|
存储 算法 编译器
程序与技术分享:C++模板元编程学习笔记
程序与技术分享:C++模板元编程学习笔记
|
4天前
|
C++
Clion CMake C/C++程序输出乱码
Clion CMake C/C++程序输出乱码
7 0
|
6天前
|
程序员 C语言 C++
【C语言】:柔性数组和C/C++中程序内存区域划分
【C语言】:柔性数组和C/C++中程序内存区域划分
9 0
|
6天前
|
C++ UED 开发者
逆向学习 MFC 篇:视图分割和在 C++ 的 Windows 窗口程序中添加图标的方法
逆向学习 MFC 篇:视图分割和在 C++ 的 Windows 窗口程序中添加图标的方法
7 0