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 ...
随机推荐
- 浅谈Android studio中OKHttp安装及简单使用
Google貌似在6.0版本里面删除了HttpClient相关API,鉴于okhttp的口碑相当好,介绍一下OKHttp的安装及使用: 一.安装 对于Android Studio的用户,在Projec ...
- Android计时器实现
Wyy.java package com.test; import android.app.Activity;import android.app.Service;import android.os. ...
- Grunt压缩图片
今天我们来说一下用Grunt来压缩图片和JS吧! 首先要安装插件: 这是压缩图片的; npm install --save-dev gulp-imagemin 这是压缩JS的: npm install ...
- node.js搭建代理服务器请求数据
1.引入node.js中的模块 var http = require("http"); var url = require("url"); var qs = r ...
- c++字符串的输入的思考
字符串的输入,是学习c++的一个重点,也是一个极富有细节意味的知识点,如果你不了解这些细节,你可能会在写程序时犯错而一脸懵逼不知所措. 与此同时,我们要了解c++缓冲区的概念,程序的输入都建有一个缓冲 ...
- 使用ConfuserEx加密混淆程序以及如何脱壳反编译
一,准备如下工具: ConfuserEx.UnConfuserEx.Fixer.ConfuserExStringDecryptor.ConfuserExSwitchKiller.de4dot.ILSp ...
- 用 Google 挖掘赚钱思路
为程序员,如果学了一堆技术却没有用武之地,实在可惜,如何把自己积累的技术利用起来?通俗一点,程序员有哪些赚钱的门路? 比较常见的一种方式是接私活,不过私活的复杂度不一,沟通成本会非常高,另一方面,私活 ...
- 简单理解js闭包
什么是闭包?我们先来看一段代码: function a() { var n = 0; function inc() { n++; console.log(n); } inc(); inc(); } a ...
- 避免循环做SQL操作
经常犯的错误是把一个SQL 操作放置到一个循环中, 这就导致频繁的访问数据库,更重要的是, 这会直接导致脚本的性能低下.以下的例子, 你能够把一个循环操作重置为一个单一的SQL语句. foreach ...
- 【LeetCode】332. Reconstruct Itinerary
题目: Given a list of airline tickets represented by pairs of departure and arrival airports [from, to ...