Chapter Goals

Discovering Classes

Discovering Classes — bestval.cpp

Discovering Classes

Common Error

Mixing >> and getline input

Interfaces

Interfaces

Product class

Interfaces

Parts

The public interface can be divided logically into 3 parts:

Constructors
Initialize new objects
Same name as class
If no parameters, called the default constructor
Mutators
Modify an object (read())
Accessors
Simply query an object, without modifying it (is_better_than() and print())
Tagged as a const method

Interfaces

Public

Revised bestval example — product1.cpp

Is this easier to read, to understand?

Syntax : Class Definition

class ClassName { public: constructor declarations member function declarations private: data fields };

Example:

class Point { public: Point (double xval, double yval); void move(double dx, double dy); double get_x() const; double get_y() const; private: double x; double y; };

Purpose:

Define the interface and data fields of a class.

Common Error

Forgetting a Semicolon

Encapsulation

Each Product object must store data (attributes)

class Product { public: Product(); void read(); bool is_better_than(Product b) const; void print() const; private: string name; double price; int score; };

Encapsulation

Encapsulation

Encapsulation

Benefits

Encapsulation

Encapsulation

Member Functions

Member Functions

Member Functions

Member Functions

Implicit Parameters

Syntax : Member Function Definition

return_type ClassName::function_name(parameter1, ..., parametern) [const]opt { statements }

Example:

void Point::move(double dx, double dy) { x = x + dx; y = y + dy; } double Point::get_x() const { return x; }

Purpose:

Supply the implementation of a member function.

Common Error

const Correctness

Default Constructors

Default Constructors (examples)

Product::Product() { price = 1; score = 0; }

Default Constructors — product2.cpp

Constructors with Parameters

Constructors with Parameters

Constructors with Parameters

Constructors with Parameters

Syntax : Constructor Definition

ClassName::ClassName(parameter1, parameter2, ..., parametern) { statements }

Example:

Point::Point(double xval, double yval) { x = xval; y = yval; }

Purpose:

Supply the implementation of a constructor.

Common Error

Forgetting to Initialize All Fields in a Constructor

Common Error

Trying to Reset an Object by Calling a Constructor

Advanced Topic

Calling Constructors from Constructors

Advanced Topic (cont)

Syntax : Constructor with Field Initializer List

ClassName::ClassName(parameters) : field1(expressions), ..., fieldn(expressions), { statements }

Example:

Point::Point(double xval, double yval) : x(xval), y(yval) { }

Purpose:

Supply the implementation of a constructor, initializing data fields before the body of the constructor.

Advanced Topic

Overloading

Advanced Topic (cont)

Overloading Operators

Accessing Data Fields

Accessing Data Fields

Comparing Member and Nonmember Functions

Comparing Member and Nonmember Functions

Comparing Member and Nonmember Functions

Comparing Member and Nonmember Functions

Value Parameter
(not changed)
Reference Parameter
(can be changed)
Explicit Parameter No modifer
void print(Employee)
Use & modifer
void raise_salary(Employee & e, double p)
Implicit Parameter Use const modifer
void Employee::print()const
No modifer
void Employee::raise_salary(double p)

Quality Tip

File Layout

Keep source files neat and organized:

Separate Compilation

Separate Compilation — prodtest/product.h

Separate Compilation — prodtest/product.h

Separate Compilation — prodtest/product.cpp

Separate Compilation — prodtest/product.cpp

Separate Compilation — prodtest/prodtest.cpp

Separate Compilation — prodtest.cpp

Separate Compilation — prodtest/prodtest.cpp

Compiling

(Note: details depend on your compiler. We demonstrate Gnu's compiler)

Separate Compilation