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
代码如下:
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类。
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; }
最后的修改如下:
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); }