C Primier Plus Class

Functions defined in the class are implicitly inline.
every member function must be declared inside its class.
Member functions access the object on which they were called through an extra, implicit parameter named this.
this is intended to always refer to “this” object(current function), this is a const pointer
const after the parameter list of a member function. A const following the parameter list indicates that this is a pointer to const

1
std::string isbn() const { return this->bookNo; }

The name of a member defined outside the class must include the name of the class of which it is a member
Dedining a function to Return “this” Object

1
2
3
4
5
6
Sales_data& Sales_data::combine(const Sales_data &rhs)
{
units_sold += rhs.units_sold; // add the members of rhs into
revenue += rhs.revenue; // the members of ''this'' object
return *this; // return the object on which the function was called
}

nonmember functions that are part of the interface of a class should be declared in the same header as the class itself.

Constructer

constructors may not be declared as const .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.
The compiler generates a default constructor automatically only if a class
declares no constructors
Class:use class to define the class are similar to struct ,the only difference is the default level.
A class may define members before the first access specifier.
To members before the first access specifier, for struct they are public,for class they are private.
private:accessible to the menber function not accessible to code that use the class.
A class allow another class or function to access its nonpublic menbers by making that class or function a friend.
Each class controls which classes or functions are its friends.friendship is not transitive.
a friend declaration affects access but is not a declaration in an ordinary sense,if we define the function inside the class, we must still provide a declaration outside of the class itself to make that function visible
We must use the constructor initializer list to provide values for members that are const, reference, or of a class type that does not have a default constructor.
Members are initialized in the order in which they appear in the class definition.

  1. It is a good idea to write constructor initializers in the same order as the
    members are declared. Moreover, when possible, avoid using members to initialize other members.
  2. it is a good idea write member initializers to use the constructor’s
    parameters rather than another data member from the same object.

Only One Class-Type Conversion Is Allowed

1
2
3
4
5
6
7
8
9
10
11
12
13
$ Now, neither constructor can be used to implicitly create a Sales_data object.
$ The explicit keyword is meaningful only on constructors that can be called with a single argument.
$ The explicit keyword is used only on the constructor declaration inside
the class.
class Sales_data {
public:
Sales_data() = default;
Sales_data(const std::string &s, unsigned n, double p):
bookNo(s), units_sold(n), revenue(p*n) { }
explicit Sales_data(const std::string &s): bookNo(s) { }
explicit Sales_data(std::istream&);
// remaining members as before
};

* An aggregate class (§ 7.5.5, p. 298) whose data members are all of literal type is a literal class??

static Class Members

a member is associated with the class by adding the keyword static to its
declaration.
Even though static members are not part of the objects of its class, we can use an object, reference, or pointer of the class type to access a static member:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Account {
public:
void calculate() { amount += amount * interestRate; }
static double rate() { return interestRate; }
static void rate(double);
private:
std::string owner;
double amount;
static double interestRate;
static double initRate();
};
Account ac1;
Account *ac2 = &ac1;
// equivalent ways to call the static member rate function
r = ac1.rate(); // through an Account object or reference
r = ac2->rate(); // through a pointer to an Account object

As with any class member, when we refer to a class static member outside
the class body, we must specify the class in which the member is defined.
The static keyword, however, is used only on the declaration inside the
class body.
we must define and initialize each static
data member outside the class body. and a static data member may be defined only once.

  1. a static data member can have incomplete type
  2. a static data member can have the same type as the class type of which
    it is a member.
  3. we can use a static member as a default argument