如何创建简单的OOP程序(分开主程序和头文件)
2015-01-02 15:18
246 查看
参考链接:https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp3_OOP.html 很适合C++新手创建OOP工程的一些例子和指导
我的编译环境: VS 2010
举一个例子:创建一个书的类和作者的类,每个类中包含相关成员变量和成员函数。具体有什么成员函数和变量,以下程序一看便知:
首先是头文件 - Author.h:
然后创建 Author.cpp 实现各个成员函数:
最后编写测试文件 test.cpp:
假设一本书只被一个作者编写,我们可以用类对象定义Book类的成员变量:
编写 Book.h :
再实现Book类的方法Book.cpp:
最后进行测试。把下面的test.cpp换成上面的test.cpp,并且把Book.h和Book.cpp文件添加到原Author工程中,就可以了:
虽然程序很简单,但是的确能帮助我很好的理解OOP编程。这也是C++最大的特色之一,勤加练习,我相信一定能有所成就。
我的编译环境: VS 2010
举一个例子:创建一个书的类和作者的类,每个类中包含相关成员变量和成员函数。具体有什么成员函数和变量,以下程序一看便知:
首先是头文件 - Author.h:
/* Header for the Author class (Author.h) */ #ifndef AUTHOR_H #define AUTHOR_H #include <string> using namespace std; class Author { private: string name; string email; char gender; public: Author(string name, string email, char gender); string getName() const; string getEmail() const; void setEmail(string email); char getGendar() const; void print() const; }; #endif
然后创建 Author.cpp 实现各个成员函数:
#include <iostream> #include "Author.h" //using namespace std; //可以省略,因为Author.h中包含了 Author::Author(string name, string email, char gender) { this->name = name; setEmail(email); // Call setter to check for valid email if(gender == 'm' || gender == 'f') { this->gender = gender; } else { cout << "invalid gender! set to 'u'(unknown)" << endl; this->gender = 'u'; } } string Author::getName() const { return name; } string Author::getEmail() const { return email; } void Author::setEmail(string email) { // Check for valid email. Assume that a valid email contains // a '@' that is not the first nor last character. size_t atIndex = email.find('@'); if(atIndex != string::npos && atIndex != 0 && atIndex != email.length() - 1) { this->email = email; } else { cout << "invalid email! set to empty string!" << endl; this->email = " "; } } char Author::getGendar() const { return gender; } void Author::print() const { cout << name << "(" << gender << ")" << email << endl; }
最后编写测试文件 test.cpp:
#include <iostream> #include "Author.h" //using namespace std; //可以省略,因为Author.h中包含了 int main() { Author peter("Peter Jones","peter@somewhere.com",'m'); peter.print(); peter.setEmail("peter@xyz.com"); peter.print(); Author paul("Paul Jones","@somewhere.com",'n'); paul.setEmail("paul@"); paul.print(); cout << "Sucessfully!" << endl; return 1; }
假设一本书只被一个作者编写,我们可以用类对象定义Book类的成员变量:
编写 Book.h :
/* Header for the class Book (Book.h) */ #ifndef BOOK_H #define BOOK_H #include <string> #include "Author.h" class Book { private: string name; Author author; double price; int qtyInStock; public: Book(string name, Author author, double price, int qtyInStock = 0); string getName() const; Author getAuthor() const; // Returns an instance of the class Author double getPrice() const; void setPrice(double price); int getQtyInStock() const; void setQtyInStock(int qtyInStock); void print() const; string getAuthorName() const; }; #endif
再实现Book类的方法Book.cpp:
#include <iostream> #include "Book.h" Book::Book(string name, Author author, double price, int qtyInStock) :name(name), author(author) { setPrice(price); setQtyInStock(qtyInStock); } string Book::getName() const { return name; } double Book::getPrice() const { return price; } Author Book::getAuthor() const { return author; } void Book::setPrice(double price) { if(price > 0) { this->price = price; } else { cout << "price should be positive! set to 0." << endl; this->price = 0; } } int Book::getQtyInStock() const { return qtyInStock; } void Book::setQtyInStock(int qtyInStock) { if(qtyInStock > 0) { this->qtyInStock = qtyInStock; } else { cout << "qtyInStock cannot be negative! Set to 0." << endl; this->qtyInStock = 0; } } void Book::print() const { cout << "'" << name << "'by"; author.print(); } // Return the author' name for this Book string Book::getAuthorName() const { return author.getName(); // invoke the getName() on instance author }
最后进行测试。把下面的test.cpp换成上面的test.cpp,并且把Book.h和Book.cpp文件添加到原Author工程中,就可以了:
#include <iostream> #include "Book.h" int main() { Author peter("Peter Jones","peter@somewhere.com",'m'); peter.print(); Book cppDummy("C++ for Dummies",peter,19.99); cppDummy.setQtyInStock(88); cout << cppDummy.getQtyInStock() << endl; cout << cppDummy.getPrice() << endl; cout << cppDummy.getAuthor().getName() << endl; cout << cppDummy.getAuthor().getEmail() << endl; cout << cppDummy.getAuthor().getGendar() << endl; cout << cppDummy.getAuthorName() << endl; Book moreCpp("More C++ for Dummies",peter,-199); cout << moreCpp.getPrice() << endl; return 1; }
虽然程序很简单,但是的确能帮助我很好的理解OOP编程。这也是C++最大的特色之一,勤加练习,我相信一定能有所成就。
相关文章推荐
- 如何在linux下创建一个简单的JNI程序HelloWorld
- c++程序中如何简单调用批处理文件
- 如何在C#中用程序执行指定的SQL脚本文件,实现自动安装创建数据库.txt
- 创建一个Android程序(简单介绍工程里面各个文件的作用)
- Linux操作系统的简单指令及如何使用vim编写一个程序,然后使用gcc查看【预处理】、【编译】、【汇编】、【链接】各阶段文件的内容。
- 用UDP实现可靠文件传输,如何利用UDX创建一个简单的WIN32程序
- 在Visual Studio 的 “一般处理程序 ” .ashx 文件中如何创建Session 对象
- 如何创建和读取excel文件-poi的简单使用(一)
- 如何创建自动执行指定文件的压缩程序
- 创建一个Android程序(简单介绍工程里面各个文件的作用)
- 用UDP实现可靠文件传输,如何利用UDX创建一个简单的WIN32程序
- Linux下之使用简单3种创建文件的命令,并实现一个Html和JavaScript小程序
- 如何创建和读取excel文件-poi的简单使用
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
- 如何创建JAR文件?如何运行.jar形式的Java程序?
- 如何简单创建日志文件
- android中如何读取已经在sqlite expert professional等sqlite工具设计好的数据库文件并且在程序中创建数据库
- VB.NET如何在程序中动态创建EXECL文件
- 在装SQL2000时,有是提示"以前的某个程序安装已在安装计算机上创建挂起的文件操作,运行安装程序之前必须重新启动计算机",如何解决,
- 如何在C#中用程序执行指定的SQL脚本文件,实现自动安装创建数据库