2. Classes in C++
Objects
Creating an object of class
Special member functions
Implementing class methods
Accessing class members
Destructors
Instance variable methods
Class abstraction
3. A class definition begins with the keyword
class.
The body of the class is contained within a
set of braces, { } ; (notice the semi-colon).
class class_name Any valid
{ identifier
….
….
Class body (data member +
….
methods)
};
4. Objects
• Objects have three
responsibilities:
What they know about themselves – (e.g., Attributes)
What they do – (e.g., Operations)
What they know about other objects – (e.g., Relationships)
4
5. Defining Class
A CLASS is a template (specification, blueprint)
for a collection of objects that share a common
set of attributes and operations.
HealthClubMember
attributes
Class operations
Objects
5
6. Member access specifiers
public:
can be accessed outside the class directly.
The public stuff is the interface.
private:
Accessible only to member functions of class
Private members and methods are for internal use
only.
7. Thisclass example shows how we can
encapsulate (gather) a circle information
into one package (unit or class)
No need for others classes to
class Circle access and retrieve its value
directly. The
{
class methods are responsible for
private: that only.
double radius;
public:
void setRadius(double r); They are accessible from outside
double getDiameter(); the class, and they can access the
double getArea(); member (radius)
double getCircumference();
};
8. Declaringa variable of a class type
creates an object. You can have many
variables of the same type (class).
Instantiation
Once an object of a certain class is
instantiated, a new memory location is
created for it to store its data members
and code
You can instantiate many objects from a
class type.
Ex) Circle c; Circle *c;
9. Constructor:
Public function member
called when a new object is created
(instantiated).
Initialize data members.
Same name as class
No return type
Several constructors
Function overloading
10. class Circle
Constructor with no
{
argument
private:
double radius;
public: Constructor with one
Circle(); argument
Circle(int r);
void setRadius(double r);
double getDiameter();
double getArea();
double getCircumference();
};
11. Class implementation: writing the code
of class methods.
There are two ways:
1. Member functions defined outside class
Using Binary scope resolution operator (::)
“Ties” member name to class name
Uniquely identify functions of particular class
Different classes can have member functions with
same name
Format for defining member functions
ReturnType
ClassName::MemberFunctionName( ){
…
}
12. 2. Member functions defined inside class
Do not need scope resolution
operator, class name;
class Circle
{ Defined
private: inside
double radius; class
public:
Circle() { radius = 0.0;}
Circle(int r);
void setRadius(double r){radius = r;}
double getDiameter(){ return radius *2;}
double getArea();
double getCircumference();
};
14. Operators to access class members
Identical to those for structs
Dot member selection operator (.)
Object
Reference to object
Arrow member selection operator (->)
Pointers
15. Destructors
Special member function
Same name as class
Preceded with tilde (~)
No arguments
No return value
Cannot be overloaded
Before system reclaims object’s memory
Reuse memory for new objects
Mainly used to de-allocate dynamic memory locations
16. This class shows how to handle time parts.
class Time
{
private:
int *hour,*minute,*second;
public:
Time();
Time(int h,int m,int s);
void printTime();
void setTime(int h,int m,int s);
int getHour(){return *hour;}
int getMinute(){return *minute;}
Destructor int getSecond(){return *second;}
void setHour(int h){*hour = h;}
void setMinute(int m){*minute = m;}
void setSecond(int s){*second = s;}
~Time();
};
17. Instance variables belong to a specific
instance.
Instance methods are invoked by an instance
of the class.
18. Class variables are shared by all the instances
of the class.
Class methods are not tied to a specific object.
Class constants are final variables shared by all
the instances of the class.
19. The scope of instance and class variables is
the entire class. They can be declared
anywhere inside a class.
The scope of a local variable starts from its
declaration and continues to the end of the
block that contains the variable. A local
variable must be declared before it can be
used.
20. Use this to refer to the current object.
Usethis to invoke other constructors of the
object.
21. Class abstraction means to separate class
implementation from the use of the class. The
creator of the class provides a description of the class
and let the user know how the class can be used. The
user of the class does not need to know how the class
is implemented. The detail of implementation is
encapsulated and hidden from the user.