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
",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
",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
",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
",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("
");
c::PersonVersion();
printf("
");
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("
");
c::PersonVersion();
printf("
");
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("
");
c::PersonVersion();
printf("
");
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("
");
C::printVersion();
printf("
");
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即可