The Primer of Classes

It took a long time for me to study classes. 
    The fundamental ideas behind  classes  are  data abstraction  and  encapsulation . Data abstraction is the ability to define both data and function members and encapsulation is the ability to protect class members for general access. Ordinarily, we encapsulation a class by defining its implementation members as  private . Classes can grant access to their nonpublic member by designating another class or function as a  friend . For example:
#ifndef SalesData_H
#define SalesData_H
#include<string>
using namespace std;
struct SalesData{
friend istream &read(istream&, SalesData&);
friend ostream &print(ostream&, const SalesData&);
friend SalesData add(const SalesData&, const SalesData&);
public:
string isbn() const { return bookNo; }
SalesData& combine(const SalesData&);
private:
double avg_price() const;
string bookNo;
unsigned solds = 0;
double revenue = 0.0;
};
istream &read(istream&, SalesData&);
ostream &print(ostream&, const SalesData&);
SalesData add(const SalesData&, const SalesData&);
SalesData& SalesData::combine(const SalesData &rhs) {
   solds += rhs.solds;
revenue += rhs.revenue;
return *this;
}
inline
double SalesData::avg_price() const {
if (solds)
return revenue / solds;
else
return 0.0;
}
istream &read(istream &is, SalesData &item) {
double price = 0.0;
is >> item.bookNo >> item.solds >> price;
item.revenue = item.solds*price;
return is;
}
ostream &print(ostream &os, const SalesData &item) {
os << item.isbn() << " " << item.solds << " "
<< item.revenue << " " << item.avg_price();
return os;
}
SalesData add(const SalesData &lhs, const SalesData &rhs) {
SalesData sum=lhs;
sum.combine(rhs);
return sum;
}
#endif
    Here, we defined a class by the keyword   struct  instead of   class. In fact, both of the keywords can define a class. The only difference between class and struct to define a class is the default level. If we use the struct keyword, the members before the first access specifier are public; if we use class, then the members are private.
    In any C++ class,   constructor is a crucial part. The job of a constructor is to initialize the data members of a class object. A constructor is run whenever an object of a class type is created. Thus, when we create a const object of a class type, the object does not assume its "constness" until after the constructor completes the object's initialization. Constructor has the same name as the class. unlike other functions, constructors have no return type. In contrary, constexpr function's only executable statement is a return statement. So the body of a   constexpr  constructor is typically empty. Like other functions, constructors can be overloaded. For example, we can add constructors to the SalesData like that:
struct SalesData{
//others as before
public:
SalesData()=default;
        constexpr SalesData(const string& s): bookNo(s)  { }
constespr SalesData(const string &s, unsigned n, double p) : bookNo(s), solds(n), revenue(n*p) { }
SalesData(std::istream &is) :SalesData() { read(is, *this); }
//others as before
};
bulalabulala,,,,,,,,,It's so complex that I don't want to write, I want to watch Seven Dargon Ball.  see you.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值