ECE151 - Lecture 9
ECE151 - Lecture 9
Programming
Chapter 13:
Introduction to Classes
Procedural and Object-Oriented
Programming
● A Class is like a blueprint and objects are like houses built from the
blueprint
Object-Oriented Programming
Terminology
● attributes: members of a class
class ClassName
declaration;
declaration;
};
Class Example
Access Specifiers
Private Members
Public Members
More on Access Specifiers
int Rectangle::setWidth(double w)
width = w;
}
Accessors and Mutators
Rectangle *rPtr;
rPtr = &otherRectangle;
rPtr->setLength(12.5);
○ Place class declaration in a header file that serves as the class specification file.
Name the file ClassName.h, for example, Rectangle.h
○ Place member function definitions in ClassName.cpp, for example,
Rectangle.cpp File should #include the class specification file
○ Programs that use the class must #include the class specification file, and be
compiled and linked with the member function definitions
Inline Member Functions
● Code for an inline function is copied into program in place of call – larger
executable program, but no function call overhead, hence faster execution
Constructors
Rectangle(double, double);
Rectangle::Rectangle(double w, double
len)
{
width = w;
length = len;
}
Passing Arguments to Constructors
Rectangle r;
Classes with No Default Constructor
● When all of a class's constructors require arguments, then the class has NO
default constructor.
● When this is the case, you must pass the required arguments to the
constructor when creating an object.
Destructors
void setCost(double);
InventoryItem inventory[40];
InventoryItem inventory[3] =
{ "Hammer", "Wrench", "Pliers" };
Arrays of Objects
● If the constructor requires more than one argument, the initializer must
take the form of a function call:
Arrays of Objects
● It isn't necessary to call the same constructor for each object in an array:
Accessing Objects in an Array
inventory[2].setUnits(30);
cout << inventory[2].getUnits();
Program 13-3 (Continued)
13.15
The Unified Modeling Language
class Rectangle
{
private:
double width;
double length;
public:
bool setWidth(double);
bool setLength(double);
double getWidth() const;
double getLength() const;
double getArea() const;
};
UML Access Specification Notation
● In UML you indicate a private member with a minus (-) and a public
member with a plus(+).
- width : double
- length : double
UML Parameter Type Notation
+ setWidth(w : double)
UML Function Return Type Notation
Constructors
Destructor
Thank You