C++第三篇--程序结构

1. 初识程序结构

将类中的成员函数全部放在类外实现,类中只负责声明该函数

person.cpp
#include <stdio.h>

class Person{
private:
char *name;
int age;
char *work; public:
void setName(char *name); int setAge(int age); void setWork(char *work); void printInfo(void); }; void Person::setName(char *name)
{
this->name = name;
} int Person::setAge(int age)
{
if(age<0 || age>200){
this->age = 0;
//return -1;
}
else
{
this->age = age;
} return age;
} void Person::setWork(char *work)
{
this->work = work;
} void Person::printInfo(void)
{
printf("name is %s,age is %d,work is %s\n",name,age,work);
} int main(int argc,char **arcv)
{
Person per; per.setName("LKQ");
per.setAge(20);
per.setWork("Student"); per.printInfo(); return 0;
}

2. 改进上文程序结构

主要分为两个层次,一个类,一个主函数

  • 实现Person类

    • Person.h:提供函数接口
    • Person.c:实现函数
  • 实现主函数

person.h
#include <stdio.h>
class Person{
private:
char *name;
char age;
char *work; public:
void setName(char *name); int setAge(int age); void setWork(char *work); 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>200){
this->age = 0;
//return -1;
}
else
{
this->age = age;
} return age;
}
void Person::setWork(char *work)
{
this->work = work;
} void Person::printInfo(void)
{
printf("name is %s,age is %d,work is %s\n",name,age,work);
}
main.cpp
#include <stdio.h>
#include "person.h" int main(int argc,char **arcv)
{
Person per; per.setName("LKQ");
per.setAge(20);
per.setWork("student"); per.printInfo(); return 0;
}
Makefile
person: main.o person.o
g++ -o $@ $^ %.o : %.cpp
g++ -c -o $@ $< clean:
rm -f *.o person

3. 添加一个Dog类

3.1 引入命名空间:为了解决多个人参加一个工程时,函数命名相同,调用同名函数时候声明属于哪个命名空间就可以解决。

dog.h
namespace c{
class Dog{
private:
char *name;
int age;
char *work; public:
void setName(char *name);
int setAge(int age);
void printInfo();
}; void PersonVersion(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;
}
else
{
this->age = age;
} return age;
} void Dog::printInfo(){printf("name is %s,age is %d,work is %s\n",name,age,work);} void PersonVersion(void)
{
printf("Dog V1 by linkaiqiang");
}
}
person.h
#include <stdio.h>

namespace A{
class Person{
private:
char *name;
char age;
char *work; public:
void setName(char *name); int setAge(int age); void setWork(char *work); void printInfo(void); };
void PersonVersion(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>200){
this->age = 0;
//return -1;
}
else
{
this->age = age;
} return age;
} void Person::setWork(char *work)
{
this->work = work;
} void Person::printInfo(void)
{
printf("name is %s,age is %d,work is %s\n",name,age,work);
} void PersonVersion(void){
printf("Person V1 by linkaiqiang");
}
}
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(20);
per.setWork("Student");
per.printInfo(); c::Dog dog; dog.setName("xiaozhao");
dog.setAge(1);
dog.printInfo(); A::PersonVersion();
printf("\n"); c::PersonVersion();
printf("\n");
return 0;
}
Makefile
person: main.o person.o dog.o
g++ -o $@ $^ %.o : %.cpp
g++ -c -o $@ $< clean:
rm -f *.o person

3.2 直接使用类

修改main.cpp
#include <stdio.h>
#include "person.h"
#include "dog.h" //global namespace
/*将A::person放入全局变量,以后可以用Person来表示A::person,下面同理*/
using A::Person;
using c::Dog; /*
using A::PersonVersion;
using c::PersonVersion;
*/
//会发生冲突,如果是两个命名空间当中相同函数定义为全局 int main(int argc,int **arcv)
{ //local namespace
Person per; per.setName("zhangsan");
per.setAge(20);
per.setWork("Student");
per.printInfo(); Dog dog; dog.setName("xiaozhao");
dog.setAge(1);
dog.printInfo(); A::PersonVersion();
printf("\n"); c::PersonVersion();
printf("\n");
return 0;
}

3.3使用namespace导入所有类和函数

修改main.cpp
#include <stdio.h>
#include "person.h"
#include "dog.h" using namespace A;
using namespace c; //将所有函数,所有类导入 int main(int argc,int **arcv)
{ //local namespace
Person per; per.setName("zhangsan");
per.setAge(21);
per.setWork("student");
per.printInfo(); Dog dog; dog.setName("xiaozhao");
dog.setAge(1);
dog.printInfo(); A::PersonVersion();
printf("\n");
c::PersonVersion();
printf("\n");
return 0;
}

4. 引入iostream

之前的程序都是用printf实现打印函数,现在C++标准输出输入流实现

dog.h
namespace C {
class Dog{
private:
char *name;
int age; public:
void setName(char *name);
int setAge(int age);
void printInfo();
}; void printVersion(void);
}
dog.cpp
#include <iostream>
#include "dog.h"
#include <stdio.h> using namespace std; 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;
}
else
{
this->age = age;
} return 0;
} void Dog::printInfo()
{
cout<<"name is "<<name<<" age is "<<age<<endl;
} void printVersion(void)
{
cout<<"Dog V1 by linkaiqiang"<<endl;;
}
}
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 setWork(char *work); 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::setWork(char *work)
{
this->work = work;
} void Person::printInfo(void)
{
std::cout<<"name = "<<name<<" age = "<<age<<" work = "<<work<<std::endl;
} void printVersion(void)
{
std::cout<<"Person v1, by weidongshan"<<std::endl;
} }
main.cpp
#include "person.h"
#include "dog.h"
#include <stdio.h> using namespace A;
using namespace C; //将所有函数,所有类导入 int main(int argc,int **argv)
{ //local namespace
Person per; per.setName("zhangsan");
per.setAge(20);
per.setWork("student");
per.printInfo(); Dog dog; dog.setName("xiaozhao");
dog.setAge(1);
dog.printInfo(); A::printVersion();
printf("\n");
C::printVersion();
printf("\n");
return 0;
}
Makefile
person: main.o person.o dog.o
g++ -o $@ $^ %.o : %.cpp
g++ -c -o $@ $< clean:
rm -f *.o person

5. 总结

  • 类定义(.h)/类实现(.cpp)

    • .h/.cpp文件中:
namespace a
//声明或定义函数;
int fun();
void fun2()...
  • 命名空间

    • 直接使用: a::fun, a::fun2
    • using声明:using a::fun; // 以后调用fun即表示a::fun
    • using编译:using namespace a ; // 以后调用fun, fun2即可

C++第三篇--程序结构的更多相关文章

  1. 【第三篇】SAP ABAP7.5x新语法之程序结构&SubScreen

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文地址:SAP ABAP7.5x系列之程序结构& ...

  2. [置顶] android利用jni调用第三方库——第三篇——编写库android程序整合第三方库libhello.so到自己的库libhelloword.so

    0:前言: 在第二篇中,我们主要介绍了丙方android公司利用乙方C++公司给的动态库,直接调用库中的方法,但是这样方式受限于: 乙方C++公司开发的动态库是否符合jni的规范,如果不规范,则不能直 ...

  3. 使用wepy开发微信小程序商城第三篇:购物车(布局篇)

    使用wepy开发微信小程序商城 第三篇:购物车(布局篇) 前两篇如下: 使用wepy开发微信小程序商城第一篇:项目初始化 使用wepy开发微信小程序商城第二篇:路由配置和页面结构 基于上两篇内容,开始 ...

  4. EnjoyingSoft之Mule ESB开发教程第三篇:Mule message structure - Mule message结构

    目录 1. 探索Mule Message结构 2. Mule Message的Payload 3. Mule Message的Property 4. Mule Message的Attachment 5 ...

  5. C语言程序的三种基本结构

    1.程序结构:在C语言程序中,一共有三种程序结构:顺序结构.选择结构(分支结构).循环结构: 顺序结构:从头到尾一句接着一句的执行下来,直到执行完最后一句: 选择结构:到某个节点后,会根据一次判断的结 ...

  6. 《Java从入门到放弃》JavaSE篇:程序结构

    程序的结构一般分为三种: 顺序结构. 选择结构. 循环结构. 一.顺序结构:这个不用多说吧,跟我们平时写文章的顺序一样,从上往下. 二.选择结构:从名字就能看出,要选择嘛,到底是要漂亮滴妹子,还是要有 ...

  7. java中的数据类型,运算符,字符串,输入输出,控制流,大数值,数组; 《java核心技术卷i》 第三章:java基本程序结构;

    <java核心技术卷i> 第三章:java基本程序结构: 每次看书,去总结的时候,总会发现一些新的东西,这次对于java的数组有了更深的了解: java中的数据类型,运算符,字符串,输入输 ...

  8. C语言学习系列(三)C程序结构

    一.C程序结构 C 程序主要包括以下部分: 预处理器指令 函数 变量 语句 & 表达式 注释 new C program demo: #include <stdio.h> /*预处 ...

  9. python程序的三种执行结构

    一.分支结构:根据条件判断的真假去执行不同分支对应的子代码 1.1 if判定 完整语法如下: if 条件1: #条件可以是任意表达式,如果条件1为True,则依次执行代码. 代码1 代码2 ... e ...

随机推荐

  1. javaSE_05Java中方法(函数)与重载、递归-思维导图

    思维导图看不清楚时: 1)可以将图片另存为图片,保存在本地来查看 2)右击在新标签中打开放大查看

  2. 隐马尔科夫模型HMM(一)HMM模型

    隐马尔科夫模型HMM(一)HMM模型基础 隐马尔科夫模型HMM(二)前向后向算法评估观察序列概率 隐马尔科夫模型HMM(三)鲍姆-韦尔奇算法求解HMM参数(TODO) 隐马尔科夫模型HMM(四)维特比 ...

  3. 搭建arm交叉工具链

    1.将arm-linux-gcc-4.4.3压缩包,拷到home/armtoolchain下,进行压缩. 2.压缩命令:tar -xzvf arm-linux-gcc-4.4.3.tgz,解压后得到了 ...

  4. dede系统自定义变量删除方法

    之前添加了个联系电话的系统变量,忘记写描述,结果就显示个冒号,很难看.这样的就要删除了重来,那么织梦怎么删除添加的变量呢?其实很简单.两种方法: 第一种:执行SQL语句.在织梦后台执行-系统-SQL命 ...

  5. iter迭代器的应用

    迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束. 用户不用关心迭代器的内部结构,仅需通过next方法不断去读取下一个内容 不能随机访问任意一个内容,只 ...

  6. 移动端响应式布局+rem+calc()

    1.媒体查询:@media only screen and (max-width: ) {},在最初做pc端时,使用各种媒体查询,因为pc的屏幕分辨率总共就几种,不嫌麻烦的重复使用类名.有很大的缺陷就 ...

  7. 移动webAPP前端开发技巧汇总2

    一.关于单位的使用 可能在传统的PC端来说,1px=1px的比例.而在移动端却不是这样,1px = ?. 因为出现了一个像素密度这样个东西,就不能在移动端使用“PX”这个单位.可能在你的大屏手机是1p ...

  8. ecshop邮件订阅按“订阅”没反应

    原订阅邮件所使用的JS文件transport.js和JQuery冲突,会更改transport.js文件,用以下代码可同样实现订阅功能. <input type="text" ...

  9. Mac, Linux中配置Latex中文字体

    对于中文的latex文档,在Linux下一般可以使用系统自带的开源字体:文泉驿(WenQuanYi)来实现,即如下的最小例子,通过xelatex命令来编译即可生成中文文档. \documentclas ...

  10. 在Cenos系统安装Python3.5版本,使P2和P3共存

    首先Cenos安装好后,系统自带python2.6版本 输入>>>exit()     退出 使用迅雷下载python3.5 链接:https://www.python.org/ft ...