Home | About | Blog | Projects | Contact

Day 3: C++ Primer - Classes

Posted: November 13, 2025

Continuing with C++ Primer (5th Edition) by Lippman, Lajoie, and Moo. Today covering Chapter 7 (Classes).

Chapter 7: Classes

Notes

data abstraction and encapsulation!!!!!! abstraction is the technique that seperates interface and implementation encapsulation enforces the seperation of a class' Classes that use data abstraction and encapsulation are called abstract data types (ADTs)

introducing this Member functions have an implicit parameter named this that points to the object for which the function is called.

const member functions std::string isbn() const {return this->bookNo; } The purpose of that const is to modify the type of the implicit this pointer. By default, the type of this is a pointer to non-const version of the class type. When we add const after the parameter list, we change the type of this to pointer to const version of the class type. This means that inside a const member function, we cannot modify any data members of the object

member declarations are compiled first, after which the member function bodies are processed. Thus member function bodies my use other members of their class regardless of the order in which those members appear.

Defining a Function to Return "This" Object Sometimes it is useful to define a member function that returns the object for which it is called. Such functions return *this. For example, we might define a member function named combine that adds the sales data from the object passed as an argument to the object on which the function is called and then returns that object.

Defining Nonmember Class-related functions Class authors often define auxiliary functions, such as add, read, print. Functions that are conceptually part of a class but not defined inisde the class are typically declared in the same header as the class itself.

Setting a class constructor = default is a c++11 feature that allows the compiler to generate a default constructor for the class if no user-defined constructor is provided.

Access Control and Encapsulation

Members defined after a public specifier are accessible to all parts of the program Members define after a private specifier are accessible to the member functions of the clas but not the code that uses the class. The private sections encapsulate(hide) the implementation.

The only difference between using class and struct is its default access level

friend keyword is used to grant a non-member function or another class access to the private and protected members of the class in which it is declared.

Key Concept: Benefits of Encapsulation

Even if two classes have the same member list, they are different types.

Say a function in one class wants to access private members of another. You can set member functions of other classes as friends.

Constructors

We must use the constructor initializer list to provide values for members that are const, reference, or of a class ype that does not have a default constructor.

Advice: use constructor initializers:
In many classes, the difference between initialization and assignment is a matter of low level efficiency. Initializing members in the constructor initializer list is often more efficient than assigning values in the constructor. It is a good idea to write initializers in the same order as the members are declared in the class to avoid confusion.

C++11 allows the use of initializers for delegating constructors. A delegating constructor is a constructor that calls another constructor in the same class to perform some or all of the initialization for the object being created.

In practice, it is always right to provide a default constructor if other constructors are being defined

A constructor that can be called with a single argument defines an implicit conversion from the constructor's parameter type to the class type.

explicit: The explicit keyword is used to prevent implicit conversions that can occur with single-argument constructors. When a constructor is declared explicit, it cannot be used for implicit conversions or copy-initialization.

aggregate classes: gives users direct access to its members and has speical initialiation syntax. All data members are public, no constructors, no in-class initializers, no base classes or virtual functions.

Literal Classes: An aggregate clas whose data members are all of literal type

constexpr constructors: A constexpr constructor is a special type of constructor in C++ that allows for the creation of objects that can be evaluated at compile time. This means that if all the arguments passed to the constructor are constant expressions, the resulting object can also be a constant expression.

static Class Members

Example, bank class storing a prime interest rate. All objects of class type Bank don't need to seperately store the interest rate.

you can access a static member directly through the scope operator; you do not have to create an object to access it.

Code Examples


    //Exercise 7.4 and 7.5
    //Write a class named Person that represents the name and address of a person.
    //Provide operations to return the name and address of the person. Should the functions be const?
    class Person {
    public:
        string name;
        string address;

        string show_name(void) const;
        string show_address(void) const;

    };

    string Person::show_name(void) const {

        return this->name;

    }

    string Person::show_address(void) const {

        return this->address;

    }
    //I defined the functions as const because they do not modify the state of the object.
  

Questions / Exercises


← Back to study list