Title: Basic Classes
1Basic Classes
2Classes are New Typescombining data and functions
class Cat int CatsAge float
CatsWeight void Meow() void
CatMeow() cout ltlt Meow!\n
3ClassSpeak
- A class enables you to encapsulate various data
and functions into one collection, which is
called an object. - The variables in a class are referred to as the
member variables or data members. - The functions in a class typically manipulate the
member variables. They are referred to as member
functions or methods of the class.
4Declaring a Class
- Use the class keyword followed by the name of the
class, - then the opening brace,
- then list the member variables and functions.
- End the declaration with a closing brace and a
semicolon.
5Declaring the class Cat
class Cat int CatsAge float
CatsWeight void Meow()
6Defining an Object
- Cat is a class, which is a new type.
- Frisky is an object of type Cat.
- An object is an individual instance of a class
- Objects are defined just like other variables
int GrossWeight float Temperature Cat
Frisky
7Accessing Class Members
- Once you define an object, use the dot .
operator to access the members
Cat Frisky Frisky.CatsAge 5 Frisky.Meow()
8Assign to Objects, Not to Classes
int 5 // wrong int x x 5 //
right Cat.CatsAge 5 // wrong Cat
Frisky Frisky.CatsAge 5 // right
9Private Versus Public
- All members of a class - data and methods - are
private by default. - Private members can be accessed only within
methods of the class itself. - Public members can be accessed through any object
of the class.
10Private by Default
class Cat int Age float Weight
void Meow() Cat Boots Boots.Age 5 //
error! Private data!
11Public if Stated
class Cat public int Age float
Weight void Meow() Cat
Boots Boots.Age 5 // ok! Public data!
12Make Member Data Private
class Cat public // public accessors int
GetAge() void SetAge(int age) float
GetWeight() void SetWeight(float weight)
// public member functions void Meow()
private // private member data int itsAge
float itsWeight
13Accessors to Private Data
int CatGetAge() return itsAge void
CatSetAge(int age) itsAge age float
CatGetWeight() return itsWeight void
CatSetWeight(float weight) itsWeight
weight void CatMeow() cout ltlt
Meow!\n
14main() Cat
void main() Cat Frisky
Frisky.SetAge(5) Frisky.Meow() cout ltlt
Frisky is a cat who is cout ltlt
Frisky.GetAge() ltlt years old.\n
Frisky.Meow()
Output Meow. Frisky is a cat who is 5
years old. Meow.
15Constructors and Destructors
Class Cat public Cat(int InitAge, float
InitWeight) Cat() int GetAge()
void SetAge(int age) float GetWeight()
void SetWeight(float weight) void Meow()
private int itsAge float itsWeight
16Cat Constructor/Destructor
CatCat(int initAge, float initWeight)
itsAge initAge itsWeight
initWeight CatCat() // destructor does
nothing
17main() Cat
void main() Cat Frisky
Frisky.SetAge(5) Frisky.Meow() cout ltlt
Frisky is a cat who is cout ltlt
Frisky.GetAge() ltlt years old.\n
Frisky.Meow()
Output Meow. Frisky is a cat who is 5
years old. Meow.
18main() Cat
void main() Cat Frisky(5,10.3)
Frisky.Meow() cout ltlt Frisky is a cat who
is cout ltlt Frisky.GetAge() ltlt years
old.\n Frisky.Meow()
Output Meow. Frisky is a cat who is 5
years old. Meow.
19About Constructors/Destructors
- The constructor is a class method with the same
name as the class itself. - The constructor can take parameters,
- but it cannot have a return value,
- not even void.
- A destructor always has the name of the class
preceded by a tilde . - Destructors take no arguments and have no return
value. - Destructors clean up after your object, and free
any memory you might have allocated
20const Member Functions
Class Cat public Cat(int InitAge, float
InitWeight) Cat() int GetAge()
void SetAge(int age) float GetWeight()
void SetWeight(float weight) void Meow()
private int itsAge float itsWeight
21const Member Functions
Class Cat public Cat(int InitAge, float
InitWeight) Cat() int GetAge() const
void SetAge(int age) float GetWeight()
const void SetWeight(float weight) void
Meow() const private int itsAge
float itsWeight
22Inline Methods
inline int CatGetAge() return itsAge
class Cat public Cat(int initAge)
Cat() int GetAge() return itsAge
void SetAge(int age) itsAge age void
Meow() cout ltlt Meow.\n private int
itsAge
23Classes as Member Data
class Point public void SetX(int x) itsX
x void SetY(int y) itsY y int
GetX() const return itsX int GetY()
const return itsY private int itsX
int itsY
24Point in a Rectangle
class Rectangle public Point
GetUpperLeft() const return itsUpperLeft
Point GetLowerLeft() const return
itsLowerLeft Point GetUpperRight() const
return itsUpperRight Point GetLowerRight()
const return itsLowerRight void
SetUpperLeft (Point loc) itsUpperLeft loc
void SetLowerLeft (Point loc)
itsLowerLeft loc void
SetUpperRight(Point loc) itsUpperRight loc
void SetLowerRight(Point loc)
itsLowerRight loc private Point
itsUpperLeft Point itsUpperRight Point
itsLowerLeft Point itsLowerRight int
itsTop int itsLeft int
itsBottom int itsRight
25Rectangle Constructor
RectangleRectangle(int top, int left,
int bottom, int right) itsTop
top itsLeft left itsBottom
bottom itsRight right
itsUpperLeft.SetX(left) itsUpperLeft.SetY(top
) itsUpperRight.SetX(right)
itsUpperRight.SetY(top) itsLowerLeft.SetX(lef
t) itsLowerLeft.SetY(bottom)
itsLowerRight.SetX(right) itsLowerRight.SetY(
bottom)
26main() Rectangle
void main() Rectangle MyRectangle(100, 20,
50, 80) cout ltlt Upper Left X Coordinate
cout ltlt MyRectangle.GetUpperLeft().GetX()
// below is an error cout ltlt Upper Left
X Coordinate cout ltlt MyRectangle.itsUpperL
eft.itsX // error, because data is private