C++第三篇--程序结构
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++第三篇--程序结构的更多相关文章
- 【第三篇】SAP ABAP7.5x新语法之程序结构&SubScreen
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文地址:SAP ABAP7.5x系列之程序结构& ...
- [置顶] android利用jni调用第三方库——第三篇——编写库android程序整合第三方库libhello.so到自己的库libhelloword.so
0:前言: 在第二篇中,我们主要介绍了丙方android公司利用乙方C++公司给的动态库,直接调用库中的方法,但是这样方式受限于: 乙方C++公司开发的动态库是否符合jni的规范,如果不规范,则不能直 ...
- 使用wepy开发微信小程序商城第三篇:购物车(布局篇)
使用wepy开发微信小程序商城 第三篇:购物车(布局篇) 前两篇如下: 使用wepy开发微信小程序商城第一篇:项目初始化 使用wepy开发微信小程序商城第二篇:路由配置和页面结构 基于上两篇内容,开始 ...
- EnjoyingSoft之Mule ESB开发教程第三篇:Mule message structure - Mule message结构
目录 1. 探索Mule Message结构 2. Mule Message的Payload 3. Mule Message的Property 4. Mule Message的Attachment 5 ...
- C语言程序的三种基本结构
1.程序结构:在C语言程序中,一共有三种程序结构:顺序结构.选择结构(分支结构).循环结构: 顺序结构:从头到尾一句接着一句的执行下来,直到执行完最后一句: 选择结构:到某个节点后,会根据一次判断的结 ...
- 《Java从入门到放弃》JavaSE篇:程序结构
程序的结构一般分为三种: 顺序结构. 选择结构. 循环结构. 一.顺序结构:这个不用多说吧,跟我们平时写文章的顺序一样,从上往下. 二.选择结构:从名字就能看出,要选择嘛,到底是要漂亮滴妹子,还是要有 ...
- java中的数据类型,运算符,字符串,输入输出,控制流,大数值,数组; 《java核心技术卷i》 第三章:java基本程序结构;
<java核心技术卷i> 第三章:java基本程序结构: 每次看书,去总结的时候,总会发现一些新的东西,这次对于java的数组有了更深的了解: java中的数据类型,运算符,字符串,输入输 ...
- C语言学习系列(三)C程序结构
一.C程序结构 C 程序主要包括以下部分: 预处理器指令 函数 变量 语句 & 表达式 注释 new C program demo: #include <stdio.h> /*预处 ...
- python程序的三种执行结构
一.分支结构:根据条件判断的真假去执行不同分支对应的子代码 1.1 if判定 完整语法如下: if 条件1: #条件可以是任意表达式,如果条件1为True,则依次执行代码. 代码1 代码2 ... e ...
随机推荐
- Python教程(1.2)——Python交互模式
上一节已经说过,安装完Python,在命令行输入"python"之后,如果成功,会得到类似于下面的窗口: 可以看到,结尾有3个>符号(>>>).>&g ...
- 你真的知道组件中的v-model吗?
v-model的神奇 html <div id="app"> <input v-model="poin"> {{ poin }} < ...
- 软件raid 5
软件raid 5的实现 RAID 5 是一种存储性能.数据安全和存储成本兼顾的存储解决方案. RAID 5可以理解为是RAID 0和RAID 1的折中方案.RAID 5可以为系统提供数据安全保障,但保 ...
- docker- 构建 oracle2c-r2(12.2.0.1) 的镜像
需求 由于公司要数据库需要使用新的oracle版本(12c-r2 ->12.2.0.1),需要从之前的oracle11g迁移到12c.所以,我们今天就先来介绍一下如何构建oracle12c的镜像 ...
- Spring学习(9)--- @Autowired注解(二)
可以使用@Autowired注解那些众所周知的解析依赖性接口,比如:BeanFactory,ApplicationContext,Environment,ResourceLoader,Applicat ...
- java面试基础题(三)
程序员面试之九阴真经 谈谈final, finally, finalize的区别: final:::修饰符(关键字)如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承.因此 ...
- c# networkcomms 3.0实现模拟登陆总结
最近项目需要做一个客户查询状态系统,当前上位机缺少服务功能,于是找到了networkcomms 开源框架,作为项目使用. 最新版networkcomms 下载地址:https://github.com ...
- WPF中带水印的Textbox
很多时候我们都希望通过水印来告诉用户这里该填什么样格式的数据,那么我们就希望有这样的一个控件. 为了方便起见,先定义一个依赖属性专门来存放水印中显示的字符串. public sealed class ...
- Spring配置注解详解
- mysql 左连接 右连接 内链接
一般所说的左连接,右连接是指左外连接,右外连接.做个简单的测试你看吧.先说左外连接和右外连接:[TEST1@orcl#16-12月-11] SQL>select * from t1;ID NAM ...