OBJECT ORIENTED
PROGRAMMING
Content
2
Classes, Objects, Member Functions and Data
Members
Defining a Class with Members
Initializing Objects with Constructors
Class Scope and Accessing Class Members
Objectives
3
By the end you should recognize:
What classes, objects, member functions and data members are
How to define a class and use it to create an object
How to define member functions in a class to implement the class's behaviors
How to declare data members in a class to implement the class's attributes
How to call a member function of an object to make that member function perform its
task
The differences between data members of a class and local variables of a function
How to use a constructor to ensure that an object's data is initialized when the object is
created
Class scope and accessing class members via the name of an object
4
#include<iostream> #include<string> using namespace std;
int makeAppointmnet();
int deleteAppointment();
void addPatient();
void deletePatient;
void addStaff();
double salary();
int main() {
string staffName; int staffID;
string staffPhoneNumber; string patientName;
string patientID;
string patientPhoneNumber; string appointmentDate; return 0; }
• Suppose
we
were
writing
hospital software
• This a bad representation of a
hospital
• We need to separate bits of
information.
• Need a way of referring to
the whole collection with a
5
class appointment {
public:
int makeAppointmnet();
int deleteAppointment();
};
Patient Class
6// C++ code to define a staff class
class staff{ private:
// Data Members
string staffName; int staffID;
string staffPhoneNumber; public:
// Member Fucntions
7 #include <iostream>
#include <string> using namespace std;
class staff{ private:
// Data Members
string staffName; int staffID;
string staffPhoneNumber; public:
// Member Fucntions
int addStaff(); double salary(); }; int main() { staff s1; s1.staffName="customer1"; s1.staffID =4034;
cout << s1.salary();
return 0; }
This program will produce 2 compilation errors:
#1: ‘staff:: staffName ' #2: ‘staff:: staffID ' :
Access Specifiers
Access Specifiers
public
: members of a class accessible outside that class
private:
members of a class not accessible outside that class
Using Access Specifiers
9#include <iostream> #include <string> using namespace std; class staff{
public:
// Data Members
string staffName; int staffID;
string staffPhoneNumber;
// Member Fucntions
int addStaff(); double salary(); }; int main() { staff s1; s1.staffName="customer1"; s1.staffID =4034;
cout << s1.salary(); return 0;
}
Using Access Specifiers (cont.)
10#include <iostream> #include <string> using namespace std; class staff{
private:
// Data Members
string staffName; int staffID;
string staffPhoneNumber; public:
// Member Fucntions
int addStaff(); double salary(); }; int main() { staff s1; s1.staffName="customer1"; s1.staffID =4034;
cout << s1.salary();
return 0; }
Anything following this can be used by anyone outside the class
Variables
Local variables
Variables declared in a function definition’s body
Cannot be used outside of that function body
When a function terminates
The values of its local variables are lost
Data Members
Variables in a class definition
Exist throughout the life of the object
Each object of class maintains its own copy of attributes
Set and Get Functions
public
member functions that allow users of an object to set or
get the values of private data members
Allows the creator of the class to control how clients access
private
data
Using Set and Get Functions
13#include <iostream> #include <string> using namespace std;
class staff{ private:
// Data Members
string staffName; int staffID;
string staffPhoneNumber; public:
// Member Fucntions
int addStaff(); double salary(); };
How would you adjust the class definition to include set and get functions??
How many do you need ??
Should they be private/public ??
Implementing Set & Get Functions
For each data member
Set function that sets its value
Get function that returns its value
Staff Data Members
14
string staffName;
void setStaffName(string);
Get Functions
15string getStaffName() { return staffName; }
int getStaffID() {
return staffID; }
16 #include <iostream>
#include <string> using namespace std; class staff{
private:
// Data Members
string staffName; int staffID;
string staffPhoneNumber; public:
// Member Fucntions
int addStaff(); double salary();
string getStaffName() { return staffName; }
int getStaffID() { return staffID; }
string getStaffPhoneNumber() { return staffPhoneNumber; }
};
int main(){
staff s1;
cout<< s1.getStaffName(); return 0;
Set Functions -1
17void setStaffName(string s) {
staffName=s; }
void setStaffID(int id) {
staffID=id; }
void setStaffPhoneNumber(string pn) {
18 #include <string>
using namespace std; class staff{
private:
// Data Members
string staffName; int staffID;
string staffPhoneNumber; public:
// Member Fucntions
int addStaff(); double salary();
string getStaffName() { return staffName; }
int getStaffID() { return staffID; }
string getStaffPhoneNumber() { return staffPhoneNumber; }
void setStaffName(string s) { staffName=s; }
void setStaffID(int id) { staffID=id; }
void setStaffPhoneNumber(string pn) { staffPhoneNumber = pn; }
};
int main(){
staff s1;
s1.setStaffName(“Maha Q”); cout << s1.getStaffName(); return 0;
Constructors
Functions used to initialize an object’s data when it is created
Call made implicitly when object is created
Must be defined with the same name as the class
Cannot return values
Not even void
Using Constructors -1
20 class staff{ private: string staffName; int staffID; string staffPhoneNumber; public:string getStaffName() { return staffName; }
int getStaffID() { return staffID; }
string getStaffPhoneNumber() { return staffPhoneNumber; }
void setStaffName(string s) { staffName=s; }
void setStaffID(int id) { staffID=id; }
void setStaffPhoneNumber(string pn) { staffPhoneNumber = pn; } staff() {
staffName = “raha”; staffID = 123;
staffPhoneNumber = “4343434”
Exercise - 1
21- Write a WebPage class which includes
The following integer data members
- Number of hits on the page
- Number of messages posted on the page
- Number of downloads from the page
The following member functions
- Default constructor initializes all data members to zero
- An increment function for each data members to increase them by 1
- Activity() function that return total activity of webpage: hits + messages + downloads
- Write a main function that declare and initialize imamUnivPage object.
- Write the statements that increase number of hits by 2, messages by 3, and
downloads by 1.
Included Sections
22