您的位置:首页 > 运维架构

如何创建简单的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:

/* 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++最大的特色之一,勤加练习,我相信一定能有所成就。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐