Intro to Classes
Intro to Classes
Introduction to
Classes
Private Members
Public Members
int Rectangle::setWidth(double w)
{
width = w;
}
(continued)
Copyright © 2019 Pearson Education Ltd., All rights reserved.
Copyright © 2019 Pearson Education Ltd., All rights reserved.
In-Place Initialization
If you are using C++11 or later, you can initialize a
member variable in its declaration statement, just as you
can with a regular variable.
This is known as in-place initialization. Here is an
example:
class Rectangle
{
private:
double width = 0.0;
double length = 0.0;
public:
Public member functions appear here…
};
Rectangle(double, double);
Rectangle::Rectangle(double w, double
len)
{
width = w;
length = len;
}
Copyright © 2019 Pearson Education Ltd., All rights reserved.
Passing Arguments to
Constructors
You can pass arguments to the constructor
when you create an object:
Rectangle r;
(continued)
InventoryItem inventory[40];
InventoryItem inventory[3] =
{ "Hammer", "Wrench", "Pliers" };
inventory[2].setUnits(30);
cout << inventory[2].getUnits();
- width : double
- length : double
Constructors
Destructor