CS 217 5 Classes
CS 217 5 Classes
(CS 217)
ClassName objectName;
void main()
{
Circle C1;
//C1.radius = 10; can’t access private member outside the class
cout<<“Area of circle = “<<C1.getArea( );
}
Local Classes
• Local classes: A local class is declared within a
function definition.
• Out-of-line functions:
– are declared within the body of the class definition
and defined outside.
Inline/Out-of-Line Member Functions
• If a member function is defined outside the class
– Scope resolution operator (::) and class name are needed
– Defining a function outside a class does not change it being
public or private
returnType ClassName::MemberFunctionName( ){
…
}
Member Functions
Separating Declaration from Implementation
class Circle
{
private: Class must define a
double radius; no-argument
public: constructor too….
Circle(double radius)
{ this->radius = radius; }
double getArea( ); // Not implemented yet
};
double Circle::getArea()
{ return this->radius * radius * 3.14159; }
void main()
{
Circle C1(99.0);
cout<<“Area of circle = “<<C1.getArea();
}
1 // Fig. 6.3: fig06_03.cpp
2 // Time class.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 // Time abstract data type (ADT) definition
9 class Time {
10 public:
11 Time(); // constructor
12 void setTime( int, int, int ); // set hour, minute, second
13 void printMilitary(); // print military time format
14 void printStandard(); // print standard time format
15 private:
16 int hour; // 0 – 23
17 int minute; // 0 – 59
18 int second; // 0 – 59
19 };
20 Note the :: preceding
21 // Time constructor initializes each data member to zero. the function names.
22 // Ensures all Time objects start in a consistent state.
23 Time::Time() { hour = minute = second = 0; }
24
25 // Set a new Time value using military time. Perform validity
26 // checks on the data values. Set invalid values to zero.
27 void Time::setTime( int h, int m, int s )
28 {
29 hour = ( h >= 0 && h < 24 ) ? h : 0;
30 minute = ( m >= 0 && m < 60 ) ? m : 0;
31 second = ( s >= 0 && s < 60 ) ? s : 0;
32 }
33
34 // Print Time in military format
35 void Time::printMilitary()
36 {
37 cout << ( hour < 10 ? "0" : "" ) << hour << ":"
38 << ( minute < 10 ? "0" : "" ) << minute;
39 }
40
41 // Print Time in standard format
42 void Time::printStandard()
43 {
44 cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )
45 << ":" << ( minute < 10 ? "0" : "" ) << minute
46 << ":" << ( second < 10 ? "0" : "" ) << second
47 << ( hour < 12 ? " AM" : " PM" );
48 }
49
Private Member Functions
• Private Member Functions:
– Only accessible (callable) from member functions of
the class
• private
– Default access mode
– Data only accessible to member functions and friends
– private members only accessible through the public
class interface using public member functions
Data Hiding - Data Field Encapsulation
• A key feature of OOP is data hiding
– data is concealed within a class so that it cannot be
accessed mistakenly by functions outside the class.
void main()
{
Circle C1;
//C1.radius = 10; can’t access private member outside the class
cout<<“Area of circle = “<<C1.getArea();
}
A Simple Program – Default Constructor
class Circle
{ C1 Object Instance
private:
double radius;
public:
//Default Constructor : C1
Circle()
//
{ No Constructor
} Here
radius: Any Value
double getArea()
{ return radius * radius * 3.14159; }
};
Allocate memory
for radius
void main()
{
Circle C1;
//C1.radius = 10; can’t access private member outside the class
cout<<“Area of circle = “<<C1.getArea();
}
Object Construction with Arguments
The syntax to declare an object using a constructor with
arguments is:
ClassName objectName(arguments);
Circle circle1(5.5);
A Simple Program – Constructor with Arguments
class Circle
{ C1 Object Instance
private:
double radius;
public:
Circle( ) {} : C1
Circle(double rad)
{ radius = rad; } radius: 9.0
double getArea()
{ return radius * radius * 3.14159; }
}; Allocate memory
for radius
void main()
{
Circle C1(9.0);
//C1.radius = 10; can’t access private member outside the class
cout<<“Area of circle = “<<C1.getArea();
}
Output of the following Program?
class Circle
{
private:
double radius;
public:
Circle( ) { }
Circle(double rad)
{ radius = rad; }
double getArea()
{ return radius * radius * 3.14159; }
};
void main()
{
Circle C1;
cout<<“Area of circle = “<<C1.getArea();
}
const Member Functions
• const Member Functions: Read-only functions cannot
modify object’s data members
Constant Functions
class Circle
{
private:
double radius;
public:
Circle ()
{ radius = 1; }
Circle(double rad)
{ radius = rad; } const member
double getArea() const function cannot
{ return radius * radius * 3.14159; }
update/change
};
object’s data
void main()
{
Circle C2(8.0);
Circle C1;
cout<<“Area of circle = “<<C1.getArea();
}
Accessors and Mutators(Getters & Setters)
• Accessors: member function only reads/gets value
from a class’s member variable but does not change it.
• Mutators: member function that stores a value in
member variable
const Objects
• const Object: Read-only objects
– Header files
• Contains class definitions and function prototypes
– Source-code files
• Contains member function definitions
1 // Fig. 6.5: time1.h
2 // Declaration of the Time class.
3 // Member functions are defined in time1.cpp
4
5 // prevent multiple inclusions of header file Dot ( . ) replaced with underscore ( _ ) in file name.
6 #ifndef TIME1_H
7 #define TIME1_H
If time1.h (TIME1_H) is not defined (#ifndef)
8 then it is loaded (#define TIME1_H). If TIME1_H
9 // Time abstract data type definition is already defined, then everything up to #endif is
ignored.
10 class Time { This prevents loading a header file multiple times.
11 public:
12 Time(); // constructor
13 void setTime( int, int, int ); // set hour, minute, second
14 void printMilitary(); // print military time format
15 void printStandard(); // print standard time format
16 private:
17 int hour; // 0 - 23
18 int minute; // 0 - 59
19 int second; // 0 - 59
20 };
21
22 #endif
23 // Fig. 6.5: time1.cpp
24 // Member function definitions for Time class.
25 #include <iostream>
26
27 using std::cout;
28 Source file uses #include to load the
29 #include "time1.h" header file
30
31 // Time constructor initializes each data member to zero.
32 // Ensures all Time objects start in a consistent state.
33 Time::Time() { hour = minute = second = 0; }
34
35 // Set a new Time value using military time. Perform validity
36 // checks on the data values. Set invalid values to zero.
37 void Time::setTime( int h, int m, int s )
38 {
39 hour = ( h >= 0 && h < 24 ) ? h : 0;
40 minute = ( m >= 0 && m < 60 ) ? m : 0;
41 second = ( s >= 0 && s < 60 ) ? s : 0; Source file contains
42 } function definitions
43
44 // Print Time in military format
45 void Time::printMilitary()
46 {
47 cout << ( hour < 10 ? "0" : "" ) << hour << ":"
48 << ( minute < 10 ? "0" : "" ) << minute;
49 }
50
51 // Print time in standard format
52 void Time::printStandard()
53 {
54 cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )
55 << ":" << ( minute < 10 ? "0" : "" ) << minute
56 << ":" << ( second < 10 ? "0" : "" ) << second
57 << ( hour < 12 ? " AM" : " PM" );
58 }
Constructors
A constructor is a special function used to create an object.
Constructor has exactly the same name as the defining class
Date *dates[31];
Date d1(02,28,2020);
• static Variables
– Default Initialization: 0 or Null (for pointers)
– Initialization: user defined value
– Initialization is made just once, at compile time.
– Accessibility: Private or Public
Public static Class Variables
– Can be accessed using Class name:
cout<<Employee::count;
• Static functions:
– Can access: static data and static functions
– Cannot access: non-static data, non-static functions,
and this pointer
Public static Class Functions
–Can be invoked using class’s any object:
cout<<e1.getCount();
122
123 return 0;
count back to zero.
124 }
128
129 return 0;
130 }
• HR Manager
• Head of Department
• Print spooler
• Window Managers
• Digital Filters
• Accounting Systems
• …
Design Solution
• Defines a getInstance( ) operation that lets clients
access its unique instance
• Responsible for creating its own unique instance
Singleton
-static uniqueinstance
Singleton data
… -Singleton()
return uniqueinstance; +static getInstance()
Singleton methods…
Implementation
• Declare all of class’s constructors private
– prevent other classes from directly creating an
instance of this class
return DB;
}
...
};
Database* Database::DB=NULL;
Singleton Consequences
• Ensures only one (e.g., Database) instance exists in the
system