Basic Classes - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Basic Classes

Description:

A class enables you to encapsulate various data and functions into one ... A destructor always has the name of the class preceded by a tilde ' ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 27
Provided by: tom8
Category:
Tags: basic | classes | tilde

less

Transcript and Presenter's Notes

Title: Basic Classes


1
Basic Classes
  • Day 6

2
Classes are New Typescombining data and functions
class Cat int CatsAge float
CatsWeight void Meow() void
CatMeow() cout ltlt Meow!\n
3
ClassSpeak
  • 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.

4
Declaring 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.

5
Declaring the class Cat
class Cat int CatsAge float
CatsWeight void Meow()
6
Defining 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
7
Accessing Class Members
  • Once you define an object, use the dot .
    operator to access the members

Cat Frisky Frisky.CatsAge 5 Frisky.Meow()
8
Assign to Objects, Not to Classes
int 5 // wrong int x x 5 //
right Cat.CatsAge 5 // wrong Cat
Frisky Frisky.CatsAge 5 // right
9
Private 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.

10
Private by Default
class Cat int Age float Weight
void Meow() Cat Boots Boots.Age 5 //
error! Private data!
11
Public if Stated
class Cat public int Age float
Weight void Meow() Cat
Boots Boots.Age 5 // ok! Public data!
12
Make 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
13
Accessors 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
14
main() 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.
15
Constructors 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
16
Cat Constructor/Destructor
CatCat(int initAge, float initWeight)
itsAge initAge itsWeight
initWeight CatCat() // destructor does
nothing
17
main() 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.
18
main() 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.
19
About 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

20
const 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
21
const 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
22
Inline 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
  • or

23
Classes 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
24
Point 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
25
Rectangle 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)
26
main() 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
Write a Comment
User Comments (0)
About PowerShow.com