02-Introduction To Classes and Objects
02-Introduction To Classes and Objects
Objects
Programming Technique II
(SCSJ1023)
Adapted from Tony Gaddis and Barret Krupnow (2016), Starting out with
C++: From Control Structures through Objects
Content
Defining classes
Creating Object
Private Members
- Why have private members ?
- Using private members function
Public Members
More on Access Specifiers
Example
Using const With Member
Functions
const appearing after the parentheses in a member
function declaration specifies that the function will not
change any data in the calling object.
Accessors and Mutators
Rectangle r;
r.setWidth(5.2);
cout << r.getWidth();
class
declaration
Example: Define Class and Object
objects
definition
Program 2-1 (Continued)
Private Members
Private Members
Example
Separating Class Specification
from Implementation
Separating Class Specification from
Implementation
CONCEPT :
// setWidth definition
// setLength definition
// getWidth definition
// getLength definition
// getArea definition
Main Program File
(Contents of useRectangle.cpp)
// This program should be compiled with Rectangle.h file, Rectangle.cpp file
#include “Rectangle.h”
#include <iostream>
{
Rectangle box; //Define an instance
double rectWidth; //Local variable
double rectLength; //Local variable
..
...
}
Separating Class Specification from
Implementation : Example
The implementation file Rectangle.cpp
useRectangle. exe
(Executable file)
Inline
Data appropriate for short
can be accessed function
only throughbodies:
public functions
Example