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:
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:
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) { }
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.
bulalabulala,,,,,,,,,It's so complex that I don't want to write, I want to watch Seven Dargon Ball. see you.