C++中的public、private、protected成员继承问题
2013-08-05 17:16 zmkeil 阅读(879) 评论(2) 编辑 收藏 举报我是C++菜鸟,刚学了一点C++。
先看例子1:
/* Item.h */
#include <iostream>
#include <string>
class Item_base {
public:
Item_base(const std::string &book = "",
double sales_price = 0.0) :
isbn(book), price(sales_price) {}
std::string book() const {return isbn;}
bool isPrecious(Item_base &other);
virtual double net_price(std::size_t n) const
{ return n*price; }
virtual ~Item_base() {}
private:
std::string isbn;
double price;
};
/* Item.cc */
#include "Item.h"
bool Item_base::isPrecious(Item_base &other)
{
if(price > other.price)
return true;
return false;
}
/* test.cc */
#include "Item.h"
using std::cout;
using std::endl;
int main()
{
Item_base base("TN-119",12.3);
cout <<"isbn: " <<base.book() <<endl;
double total_price = base.net_price(3);
cout <<"total: " <<total_price <<endl;
Item_base base1("TN-120",15.4);
if(base.isPrecious(base1))
cout <<base.book() <<" is precious than " <<base1.book() <<endl;
else
cout <<base.book() <<" is cheaper than " <<base1.book() <<endl;
return 0;
}
在没有继承的情况下,只要public、private就足够了,private成员不能被用户(test.cc)使用,用户只能通过类的public接口来使用它们,比如用base.book()来获得base.isbn。
在类中,该类(不是该类的一个对象,而是该类的任何对象)的private成员都可以被访问,如bool Item_base::isPrecious(Item_base &other)函数中,就可以访问Item_base类的price成员,当然成员的访问要依赖于具体的对象,如这里的this、other。
再看例子2:
/* Item.h */
#include <iostream>
#include <string>
class Item_base {
public:
Item_base(const std::string &book = "",
double sales_price = 0.0) :
isbn(book), price(sales_price) {}
std::string book() const {return isbn;}
bool isPrecious(Item_base &other);
virtual double net_price(std::size_t n) const
{ return n*price; }
virtual ~Item_base() {}
private:
std::string isbn;
double price;
};
class Bulk_item : public Item_base {
public:
Bulk_item(std::size_t min, double disc_rate) :
min_qty(min), discount(disc_rate) {}
Bulk_item(const std::string &book, double sales_price,
std::size_t min, double dis) :
Item_base(book,sales_price), min_qty(min), discount(dis) {}
private:
std::size_t min_qty;
double discount;
};
/* Item.cc */
#include "Item.h"
bool Item_base::isPrecious(Item_base &other)
{
if(price > other.price)
return true;
return false;
}
/* test.cc */
#include "Item.h"
using std::cout;
using std::endl;
int main()
{
Item_base base("TN-119",12.3);
Bulk_item bulk("TNP-132",13.3,3,0.2);
if(base.isPrecious(bulk))
cout <<base.book() <<" is more expensive than " <<bulk.book() <<endl;
if(bulk.isPrecious(base)) //ok! isPrecious is base's func, it can access base's data
cout <<base.book() <<" is cheaper than " <<bulk.book() <<endl;
return 0;
}
Bulk_item类继承了Item_base类,并从哪里继承了bool isPrecious(Item_base &other)方法,但要注意该方法仍然是基类定义的,只是被子类继承过来用而已。再看test.cc中的base.isPrecious(bulk)调用,该实参被自动转化为Item_base类,isPrecious()函数仍可以访问bulk从基类那继承来的private成员price。
Item_base(const std::string &book = "",
isbn(book), price(sales_price) {}
std::string book() const {return isbn;}
bool isPrecious(Item_base &other);
virtual double net_price(std::size_t n) const
class Bulk_item : public Item_base {
Bulk_item(std::size_t min, double disc_rate) :
min_qty(min), discount(disc_rate) {}
Bulk_item(const std::string &book, double sales_price,
std::size_t min, double dis) :
Item_base(book,sales_price), min_qty(min), discount(dis) {}
double net_price(std::size_t n) const;
bool Item_base::isPrecious(Item_base &other)
double Bulk_item::net_price(std::size_t n) const
Item_base base("TN-119",12.3);
Bulk_item bulk("TNP-132",13.3,3,0.2);
cout <<base.book() <<" 3X total: " <<base.net_price(3) <<endl;
cout <<bulk.book() <<" 3X total: " <<bulk.net_price(3) <<endl;